Compare commits
50 Commits
d7fb694626
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1f06e3405 | ||
|
|
f4ebd33329 | ||
|
|
beeace231d | ||
|
|
79e7a4f61f | ||
|
|
618cd4a61c | ||
|
|
66d3cc4da3 | ||
|
|
35138bdc22 | ||
|
|
08f945a029 | ||
|
|
2160ba6ce8 | ||
|
|
a1455aade4 | ||
|
|
0d2e5a592f | ||
|
|
98d345ceee | ||
|
|
ac613e680a | ||
|
|
e67f8a0cda | ||
|
|
7a1d14f23a | ||
|
|
829d43851b | ||
|
|
381b6c8a9c | ||
|
|
7e4c41cffd | ||
|
|
d1c3adddff | ||
|
|
085b2bc2ce | ||
|
|
e77ed38ebf | ||
|
|
eff1ae313f | ||
|
|
47cecbc28b | ||
|
|
d4cb6bc2e8 | ||
|
|
e1db51c461 | ||
|
|
6ed8eb5f06 | ||
|
|
d663f1eeb5 | ||
|
|
2d9cae2624 | ||
|
|
8a0c566a68 | ||
|
|
5226ec4819 | ||
|
|
44202c3679 | ||
|
|
a50ed1e593 | ||
|
|
7441f0550b | ||
|
|
036a5116b8 | ||
|
|
4630813644 | ||
|
|
38271d0d88 | ||
|
|
fff9f05bae | ||
|
|
049b4fd280 | ||
|
|
604b983a51 | ||
|
|
4ca4cacda0 | ||
|
|
3b64635339 | ||
|
|
6de2fcc1bd | ||
|
|
8c1946db8e | ||
|
|
1e4d84017a | ||
|
|
b6e1851b11 | ||
|
|
c205d315b7 | ||
|
|
04463cadcd | ||
|
|
8060852b83 | ||
|
|
40b42b1d40 | ||
|
|
49e284ff56 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -4,6 +4,10 @@ data/
|
|||||||
*.sqlite3-wal
|
*.sqlite3-wal
|
||||||
*.sqlite3-shm
|
*.sqlite3-shm
|
||||||
|
|
||||||
|
# deploy state (stack-local, never committed)
|
||||||
|
.env
|
||||||
|
tailscale-state/
|
||||||
|
|
||||||
# python
|
# python
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
|
|||||||
80
README.md
80
README.md
@@ -1,77 +1,19 @@
|
|||||||
# Bike-Tinder
|
# Bike-Tinder
|
||||||
|
|
||||||
A private, Tinder-style web app for browsing the motorcycle market: swipe real listings (like / nope / maybe), see everything on one editable list, and let the deck learn your taste. Built for Glenn; runs on the Rotarran, published to the tailnet only.
|
Find your next motorcycle without sorting through the same classifieds every night. Bike-Tinder is a private, self-hosted web app that puts motorcycle listings from Kijiji in Nova Scotia and New Brunswick into a photo-first swipe deck.
|
||||||
|
|
||||||
**Status:** Phase 2 (production build) — FastAPI backend + no-build PWA shipped on `dev` (2026-11-10): migrations, API with cursor-paginated deck, swipe persistence, real list/matches screens, image mirror, PWA shell. Deployment (podman stack) is Phase 4 — not started.
|
Swipe right to like a bike, left to pass, or up if you're on the fence. Open a card to check the price, mileage, and photos. The searchable List lets you revisit a bike or change your mind later. As you swipe, the deck learns what you tend to like and brings more of those bikes forward.
|
||||||
|
|
||||||
## How it works
|
## A look at the app
|
||||||
|
|
||||||
```
|
| Swipe deck | Bike details |
|
||||||
[single-source crawler] → [nightly importer] → [app DB] → [API + PWA deck] → Glenn's thumb
|
| :---: | :---: |
|
||||||
↓
|
|  |  |
|
||||||
[swipe history] → [ranking]
|
|
||||||
```
|
|
||||||
|
|
||||||
- **Crawl** — **Kijiji** (NS + NB; decided 2026-11-10 — see `docs/SOURCES.md`) is swept nightly by the existing hunt crawler; a script-only importer folds the results in. No LLM involved, zero token cost.
|
*Screenshots from the app with demo listings.*
|
||||||
- **Import** — the importer upserts the source's catalog into the app database: new listings appear, changes update, vanished listings retire after repeated misses (never a blocked page, never a guess).
|
|
||||||
- **Deck** — a Tinder-like PWA: card stack, drag to swipe, big buttons. Like / Nope / Maybe. "It's a match" moment when you like something.
|
|
||||||
- **List & edit** — every entry in one table; re-file any call later (a nope can become a like).
|
|
||||||
- **Ranking** — cold start uses the hunt's rubric score; a cheap, fully explainable swipe-boost lands first (v1.x), then a small learned model (Phase 3, retrained nightly) orders the deck by *your* taste. "Because you liked X" chips, never a black box.
|
|
||||||
|
|
||||||
## Docs
|
## Documentation
|
||||||
|
|
||||||
- [`docs/SOURCES.md`](docs/SOURCES.md) — the v1 source decision (candidates, evidence, choice)
|
- [How it works](docs/HOW-IT-WORKS.md) — catalog, swipe history, and ranking
|
||||||
- [`docs/DESIGN.md`](docs/DESIGN.md) — architecture, components, data flow, deployment
|
- [Development and deployment](docs/DEVELOPMENT.md) — local setup, tests, and operations
|
||||||
- [`docs/DATA-MODEL.md`](docs/DATA-MODEL.md) — database schema + sync semantics
|
- [Interface and gestures](docs/UI-SPEC.md) · [Roadmap](docs/PLAN.md)
|
||||||
- [`docs/RANKING.md`](docs/RANKING.md) — cold-start + learned ranking design
|
|
||||||
- [`docs/UI-SPEC.md`](docs/UI-SPEC.md) — screens, gestures, Tinder-fidelity goals
|
|
||||||
- [`docs/PLAN.md`](docs/PLAN.md) — phases, acceptance criteria, status
|
|
||||||
|
|
||||||
## Branches
|
|
||||||
|
|
||||||
- `main` — stable trunk (docs + verified code); the default branch.
|
|
||||||
- `dev` — active development; build work lands here first.
|
|
||||||
- `release` — deployable cuts; the Rotarran stack tracks this branch.
|
|
||||||
|
|
||||||
## Run & develop
|
|
||||||
|
|
||||||
Everything runs from the repo root (Python 3.13+, stdlib where possible).
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# one-time setup
|
|
||||||
python3 -m venv .venv
|
|
||||||
.venv/bin/pip install -r requirements.txt # fastapi + uvicorn (+ httpx for tests)
|
|
||||||
|
|
||||||
# import the hunt state into the DB (idempotent; safe to re-run any time)
|
|
||||||
python3 -m app.importer --state-dir ~/.hermes/scripts/motorcycle-hunt.d --db data/bike-tinder.sqlite3
|
|
||||||
|
|
||||||
# mirror card images into data/img/ (polite: ~1 req/s, skip-existing, resumable)
|
|
||||||
.venv/bin/python -m app.imgfetch --db data/bike-tinder.sqlite3
|
|
||||||
.venv/bin/python -m app.imgfetch --limit 15 # small test runs
|
|
||||||
|
|
||||||
# serve the app (127.0.0.1:9453 by default)
|
|
||||||
.venv/bin/python -m app.serve --db data/bike-tinder.sqlite3 --port 9453
|
|
||||||
|
|
||||||
# self-check: 8 P1 spine checks + 6 P2 API checks (on a temp DB copy)
|
|
||||||
.venv/bin/python -m app.selfcheck
|
|
||||||
|
|
||||||
# proof CLI: ranked deck + counts (read-only)
|
|
||||||
python3 -m app.deck --db data/bike-tinder.sqlite3 --top 15
|
|
||||||
```
|
|
||||||
|
|
||||||
Key routes: `/` (PWA), `/api/health`, `/api/stats`, `/api/deck?limit=&cursor=`,
|
|
||||||
`POST /api/swipe` / `DELETE /api/swipe/{id}`, `/api/list?decision=&sort=&q=&limit=&offset=`,
|
|
||||||
`/api/listing/{id}`, `POST /api/import`, `POST /api/badge/seen`, `/img/{id}`
|
|
||||||
(cached file → 302 source CDN → 404 placeholder). Full contract: `docs/UI-SPEC.md` + the task brief.
|
|
||||||
|
|
||||||
`data/` and `.venv/` are gitignored — the DB, image mirror and venv are local state.
|
|
||||||
|
|
||||||
## Deployment (planned)
|
|
||||||
|
|
||||||
A small podman compose stack on the Rotarran under `/var/mnt/DATA/podman-stacks/bike-tinder/`, its own systemd user unit (`Delegate=yes`), SQLite database. The stack ships its own Tailscale node (sidecar container — no host-tailnet coupling) and serves the app privately at `https://biketinder.tailf35a34.ts.net`. Single user; reachable only from Glenn's own devices.
|
|
||||||
|
|
||||||
## Crew
|
|
||||||
|
|
||||||
- **Glenn** — owner; the thumb that swipes.
|
|
||||||
- **Alex** — resident agent; architecture, orchestration, docs.
|
|
||||||
- **Torres** — engineer; builds and maintains this repo.
|
|
||||||
|
|||||||
501
app/agentpass.py
Normal file
501
app/agentpass.py
Normal file
@@ -0,0 +1,501 @@
|
|||||||
|
"""Bike-Tinder P3 agent pass — the ONLY LLM step (docs/DESIGN.md).
|
||||||
|
|
||||||
|
Reads a compact, deterministic strike summary and writes ONE taste_profile
|
||||||
|
row (payload schema {lead, leanings, changes, chips, adjustments,
|
||||||
|
wildcards}) plus ONE agent_runs observability row. Never part of any
|
||||||
|
request path; the deck never waits on it.
|
||||||
|
|
||||||
|
Sources:
|
||||||
|
mock — deterministic payload synthesis from the strike data; zero
|
||||||
|
network; used by tests and offline forge.
|
||||||
|
live — OpenRouter chat completion (requests; env OPENROUTER_API_KEY,
|
||||||
|
BT_AGENT_MODEL; timeout + one retry; malformed -> fallback).
|
||||||
|
The key is never logged or printed.
|
||||||
|
fallback — deterministic synthesis again (source-labeled 'fallback').
|
||||||
|
|
||||||
|
CLI:
|
||||||
|
.venv/bin/python -m app.agentpass --db PATH [--mock|--live] [--model M]
|
||||||
|
[--print-summary] [--dry-run]
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from . import db
|
||||||
|
from . import sections
|
||||||
|
from . import taste as tastemod
|
||||||
|
|
||||||
|
DEFAULT_MODEL = 'openrouter/auto'
|
||||||
|
API_URL = 'https://openrouter.ai/api/v1/chat/completions'
|
||||||
|
TIMEOUT = 45
|
||||||
|
|
||||||
|
FEATURE_DIMS = tastemod.FEATURE_DIMS
|
||||||
|
|
||||||
|
SYSTEM_PROMPT = (
|
||||||
|
"You are the taste-profile writer for a single-user motorcycle-swipe "
|
||||||
|
"app. Reply with ONLY strict JSON, no markdown, matching exactly:\n"
|
||||||
|
'{"lead": "<one line>", "leanings": ["<up to 8 bullets>"], '
|
||||||
|
'"changes": ["<since last read, may be empty>"], '
|
||||||
|
'"chips": ["<up to 6 short explanations>"], '
|
||||||
|
'"adjustments": [{"feature": "<'+ '|'.join(FEATURE_DIMS) +'>", '
|
||||||
|
'"value": "<feature value>", "delta": <number -3..3>, '
|
||||||
|
'"why": "<short reason>"}], '
|
||||||
|
'"wildcards": {"families": ["<family label>"], '
|
||||||
|
'"price_bands": ["<band>"]}}'
|
||||||
|
"Adjustments nudge listing scores; keep them small and few. "
|
||||||
|
"Never invent feature values that are not in the data."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def now_iso():
|
||||||
|
return datetime.datetime.now(datetime.timezone.utc).strftime(
|
||||||
|
'%Y-%m-%dT%H:%M:%SZ')
|
||||||
|
|
||||||
|
|
||||||
|
def _now_dt():
|
||||||
|
return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
|
# --- strike summary (deterministic: a pure function of DB rows) --------------
|
||||||
|
|
||||||
|
def build_summary(conn, now=None):
|
||||||
|
"""Compact text summary of the strikes — exactly what the live pass
|
||||||
|
is sent. Deterministic for identical DB content (no wall clock)."""
|
||||||
|
now = now or _now_dt()
|
||||||
|
engine = sections.build_engine(conn, now=now)
|
||||||
|
lines = []
|
||||||
|
counts = {r['action']: r['n'] for r in conn.execute(
|
||||||
|
'SELECT action, COUNT(*) AS n FROM swipes GROUP BY action')}
|
||||||
|
decided = sum(counts.values())
|
||||||
|
lines.append('decided=%d like=%d maybe=%d nope=%d events=%d' % (
|
||||||
|
decided, counts.get('like', 0), counts.get('maybe', 0),
|
||||||
|
counts.get('nope', 0), engine.n_events))
|
||||||
|
|
||||||
|
prof = sections.latest_profile(conn)
|
||||||
|
if prof:
|
||||||
|
lines.append('profile_version=%d source=%s' % (prof['version'],
|
||||||
|
prof['source']))
|
||||||
|
|
||||||
|
# families: action counts + net weight
|
||||||
|
fam_rows = {}
|
||||||
|
for ev in engine.events:
|
||||||
|
row = engine.by_id.get(ev['listing_id'])
|
||||||
|
fam = row['family'] if row else None
|
||||||
|
if fam:
|
||||||
|
rec = fam_rows.setdefault(fam, {'like': 0, 'maybe': 0, 'nope': 0,
|
||||||
|
'net': 0.0})
|
||||||
|
rec[ev['action']] = rec.get(ev['action'], 0) + 1
|
||||||
|
rec['net'] += tastemod.event_weight(ev['action'], ev['round'],
|
||||||
|
ev['created_at'], now)
|
||||||
|
lines.append('families:')
|
||||||
|
if fam_rows:
|
||||||
|
for fam, rec in sorted(fam_rows.items(),
|
||||||
|
key=lambda kv: -abs(kv[1]['net']))[:10]:
|
||||||
|
lines.append('- %s | like=%d maybe=%d nope=%d | net=%.2f' % (
|
||||||
|
tasname(fam), rec['like'], rec['maybe'], rec['nope'],
|
||||||
|
rec['net']))
|
||||||
|
else:
|
||||||
|
lines.append('- (none matched yet)')
|
||||||
|
|
||||||
|
# feature cells (non-family) with verdicts
|
||||||
|
lines.append('signals:')
|
||||||
|
cells = {}
|
||||||
|
for ev in engine.events:
|
||||||
|
for dim, val in engine.cells.get(ev['listing_id'], []):
|
||||||
|
if dim == 'family':
|
||||||
|
continue
|
||||||
|
rec = cells.setdefault((dim, val), {'n': 0, 'w': 0.0})
|
||||||
|
rec['n'] += 1
|
||||||
|
rec['w'] += tastemod.event_weight(ev['action'], ev['round'],
|
||||||
|
ev['created_at'], now)
|
||||||
|
for (dim, val), rec in sorted(cells.items(),
|
||||||
|
key=lambda kv: -abs(kv[1]['w']))[:10]:
|
||||||
|
wtxt = '%.2f' % rec['w']
|
||||||
|
lines.append('- %s %s | n=%d | w=%s' % (dim, val, rec['n'], wtxt))
|
||||||
|
|
||||||
|
# recent strikes (chronological tail)
|
||||||
|
recent = conn.execute(
|
||||||
|
"""SELECT e.action, e.round, e.created_at, l.title, l.family,
|
||||||
|
e.section_id
|
||||||
|
FROM swipe_events e JOIN listings l ON l.id = e.listing_id
|
||||||
|
ORDER BY e.id DESC LIMIT 10""").fetchall()
|
||||||
|
lines.append('recent:')
|
||||||
|
for r in reversed(recent):
|
||||||
|
lines.append('- %s r%d %s | %s%s' % (
|
||||||
|
r['action'], min(r['round'] or 1, 2), r['created_at'],
|
||||||
|
(r['title'] or '')[:60], (' | %s' % tasname(r['family']))
|
||||||
|
if r['family'] else ''))
|
||||||
|
|
||||||
|
# liked exemplars
|
||||||
|
liked = conn.execute(
|
||||||
|
"""SELECT l.title, l.family, l.price_num, l.year
|
||||||
|
FROM swipes s JOIN listings l ON l.id = s.listing_id
|
||||||
|
WHERE s.action='like' ORDER BY s.updated_at DESC LIMIT 5"""
|
||||||
|
).fetchall()
|
||||||
|
lines.append('liked:' if liked else 'liked: (none yet)')
|
||||||
|
for r in liked:
|
||||||
|
lines.append('- %s | %s | %s | %s' % (
|
||||||
|
(r['title'] or '')[:60], tasname(r['family']), r['price_num'],
|
||||||
|
r['year']))
|
||||||
|
|
||||||
|
# returns / wildcard engagement so far
|
||||||
|
restruck = conn.execute(
|
||||||
|
"""SELECT COUNT(DISTINCT e.listing_id) AS n FROM swipe_events e
|
||||||
|
JOIN section_items si
|
||||||
|
ON si.section_id = e.section_id AND si.listing_id = e.listing_id
|
||||||
|
WHERE si.slot='return'""").fetchone()['n']
|
||||||
|
wild_hits = conn.execute(
|
||||||
|
"""SELECT COUNT(DISTINCT e.listing_id) AS n FROM swipe_events e
|
||||||
|
JOIN section_items si
|
||||||
|
ON si.section_id = e.section_id AND si.listing_id = e.listing_id
|
||||||
|
WHERE si.slot='wildcard' AND e.action IN ('like','maybe')"""
|
||||||
|
).fetchone()['n']
|
||||||
|
lines.append('second_looks_re_struck=%d wildcards_hit=%d' % (restruck,
|
||||||
|
wild_hits))
|
||||||
|
return '\n'.join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def tasname(label):
|
||||||
|
"""Short family name for the summary (trim trailing rationale)."""
|
||||||
|
return (label or '').split(' — ')[0].strip()
|
||||||
|
|
||||||
|
|
||||||
|
# --- deterministic payload synthesis (mock + fallback share this) ------------
|
||||||
|
|
||||||
|
def synthesize_payload(conn, now=None):
|
||||||
|
"""Deterministic {lead, leanings, changes, chips, adjustments,
|
||||||
|
wildcards} derived purely from the strike data. No randomness, no
|
||||||
|
network."""
|
||||||
|
now = now or _now_dt()
|
||||||
|
engine = sections.build_engine(conn, now=now)
|
||||||
|
counts = {r['action']: r['n'] for r in conn.execute(
|
||||||
|
'SELECT action, COUNT(*) AS n FROM swipes GROUP BY action')}
|
||||||
|
decided = sum(counts.values())
|
||||||
|
likes = counts.get('like', 0)
|
||||||
|
|
||||||
|
cells = {} # (dim, val) -> {n, w}
|
||||||
|
for ev in engine.events:
|
||||||
|
for dim, val in engine.cells.get(ev['listing_id'], []):
|
||||||
|
rec = cells.setdefault((dim, val), {'n': 0, 'w': 0.0})
|
||||||
|
rec['n'] += 1
|
||||||
|
rec['w'] += tastemod.event_weight(ev['action'], ev['round'],
|
||||||
|
ev['created_at'], now)
|
||||||
|
ranked = sorted(cells.items(), key=lambda kv: -abs(kv[1]['w']))
|
||||||
|
pos = [(d, v, r) for (d, v), r in ranked if r['w'] > 0][:4]
|
||||||
|
neg = [(d, v, r) for (d, v), r in ranked if r['w'] < 0][:4]
|
||||||
|
|
||||||
|
def lab(dim, val):
|
||||||
|
return tasname(val) if dim == 'family' else ('%s %s' % (dim, val))
|
||||||
|
|
||||||
|
# lead
|
||||||
|
if not engine.events:
|
||||||
|
lead = 'Cold start — rubric order until the first strikes land.'
|
||||||
|
elif likes and pos:
|
||||||
|
d, v, r = pos[0]
|
||||||
|
lead = ('You favor %s — %d of your likes cluster there.'
|
||||||
|
% (lab(d, v), r['n']))
|
||||||
|
elif pos:
|
||||||
|
d, v, r = pos[0]
|
||||||
|
lead = ('Leaning toward %s (%d strike%s).'
|
||||||
|
% (lab(d, v), r['n'], 's' if r['n'] != 1 else ''))
|
||||||
|
else:
|
||||||
|
lead = 'No strong signal yet — keep swiping.'
|
||||||
|
|
||||||
|
# leanings (<=8)
|
||||||
|
leanings = []
|
||||||
|
for d, v, r in pos:
|
||||||
|
leanings.append('%s pulls you in (+%.1f over %d strike%s)' % (
|
||||||
|
lab(d, v), r['w'], r['n'], 's' if r['n'] != 1 else ''))
|
||||||
|
for d, v, r in neg:
|
||||||
|
leanings.append('%s reads as a miss (%+.1f over %d strike%s)' % (
|
||||||
|
lab(d, v), r['w'], r['n'], 's' if r['n'] != 1 else ''))
|
||||||
|
leanings = leanings[:8]
|
||||||
|
if not leanings and engine.events:
|
||||||
|
leanings = ['No feature clusters yet — the ramp keeps the rubric '
|
||||||
|
'in charge.']
|
||||||
|
|
||||||
|
# changes (since the previous profile version)
|
||||||
|
prof = sections.latest_profile(conn)
|
||||||
|
if prof is None:
|
||||||
|
changes = ['first read — %d strikes so far' % engine.n_events]
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
prev_dt = tastemod.parse_ts(prof['created_at'], now)
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
prev_dt = None
|
||||||
|
if prev_dt is None:
|
||||||
|
changes = ['unable to diff against the previous read']
|
||||||
|
else:
|
||||||
|
new_n = sum(1 for ev in engine.events
|
||||||
|
if (tastemod.parse_ts(ev['created_at'], now) or now)
|
||||||
|
> prev_dt)
|
||||||
|
if new_n:
|
||||||
|
deltas = []
|
||||||
|
for (d, v), r in ranked[:3]:
|
||||||
|
sub = 0.0
|
||||||
|
for ev in engine.events:
|
||||||
|
dt = tastemod.parse_ts(ev['created_at'], now)
|
||||||
|
if dt is not None and dt > prev_dt and (
|
||||||
|
d, v) in engine.cells.get(
|
||||||
|
ev['listing_id'], []):
|
||||||
|
sub += tastemod.event_weight(
|
||||||
|
ev['action'], ev['round'],
|
||||||
|
ev['created_at'], now)
|
||||||
|
if abs(sub) >= 0.05:
|
||||||
|
deltas.append('%s %+.1f' % (lab(d, v), sub))
|
||||||
|
head = '%d new strike%s since the last read' % (
|
||||||
|
new_n, 's' if new_n != 1 else '')
|
||||||
|
changes = [head + ('; ' + ', '.join(deltas)
|
||||||
|
if deltas else '')]
|
||||||
|
else:
|
||||||
|
changes = ['no new strikes since the last read']
|
||||||
|
|
||||||
|
# chips (up to 6 short explanations)
|
||||||
|
chips = [lab(d, v) for d, v, r in pos[:6]]
|
||||||
|
if not chips:
|
||||||
|
chips = ['rubric fit']
|
||||||
|
|
||||||
|
# adjustments (small deterministic nudges on the top cells)
|
||||||
|
adjustments = []
|
||||||
|
for (d, v), r in ranked[:8]:
|
||||||
|
if abs(r['w']) < 0.5:
|
||||||
|
continue
|
||||||
|
delta = max(-3.0, min(3.0, round(r['w'] / 2.0, 1)))
|
||||||
|
adjustments.append({
|
||||||
|
'feature': d, 'value': v, 'delta': delta,
|
||||||
|
'why': 'you %s %s %d time%s' % (
|
||||||
|
'favored' if r['w'] > 0 else 'passed on', lab(d, v), r['n'],
|
||||||
|
's' if r['n'] != 1 else ''),
|
||||||
|
})
|
||||||
|
|
||||||
|
# wildcard hints: families without hard nopes, off the beaten likes
|
||||||
|
liked_fams = {ev['listing_id'] for ev in engine.events
|
||||||
|
if ev['action'] == 'like'}
|
||||||
|
fam_nopes_w = {}
|
||||||
|
for ev in engine.events:
|
||||||
|
row = engine.by_id.get(ev['listing_id'])
|
||||||
|
if not row or not row['family']:
|
||||||
|
continue
|
||||||
|
rec = fam_nopes_w.setdefault(row['family'], {'nopes': 0, 'w': 0.0})
|
||||||
|
if ev['action'] == 'nope':
|
||||||
|
rec['nopes'] += 1
|
||||||
|
rec['w'] += tastemod.event_weight(ev['action'], ev['round'],
|
||||||
|
ev['created_at'], now)
|
||||||
|
fam_stretch = [f for f, r in fam_nopes_w.items()
|
||||||
|
if r['nopes'] <= 1 and r['w'] >= 0]
|
||||||
|
fam_stretch = sorted(fam_stretch, key=lambda f: -fam_nopes_w[f]['w'])[:3]
|
||||||
|
band_rows = [(tastemod.price_band(row['price_num']), ev['action'])
|
||||||
|
for ev in engine.events
|
||||||
|
if (row := engine.by_id.get(ev['listing_id'])) is not None]
|
||||||
|
band_counts = {}
|
||||||
|
for b, a in band_rows:
|
||||||
|
if b:
|
||||||
|
rec = band_counts.setdefault(b, {'nopes': 0, 'n': 0})
|
||||||
|
rec['n'] += 1
|
||||||
|
rec['nopes'] += 1 if a == 'nope' else 0
|
||||||
|
band_stretch = sorted(
|
||||||
|
(b for b, r in band_counts.items() if r['nopes'] == 0),
|
||||||
|
key=lambda b: band_counts[b]['n'])[:3]
|
||||||
|
|
||||||
|
wildcards = {'families': fam_stretch, 'price_bands': band_stretch}
|
||||||
|
|
||||||
|
return {'lead': lead, 'leanings': leanings, 'changes': changes,
|
||||||
|
'chips': chips, 'adjustments': adjustments,
|
||||||
|
'wildcards': wildcards}
|
||||||
|
|
||||||
|
|
||||||
|
# --- validation --------------------------------------------------------------
|
||||||
|
|
||||||
|
def validate_payload(payload):
|
||||||
|
"""Raise ValueError on schema violations."""
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
raise ValueError('payload must be an object')
|
||||||
|
for key in ('lead', 'leanings', 'changes', 'chips', 'adjustments',
|
||||||
|
'wildcards'):
|
||||||
|
if key not in payload:
|
||||||
|
raise ValueError('missing key: %s' % key)
|
||||||
|
if not isinstance(payload['lead'], str):
|
||||||
|
raise ValueError('lead must be a string')
|
||||||
|
for key in ('leanings', 'changes', 'chips'):
|
||||||
|
if (not isinstance(payload[key], list)
|
||||||
|
or not all(isinstance(x, str) for x in payload[key])):
|
||||||
|
raise ValueError('%s must be a list of strings' % key)
|
||||||
|
for adj in payload['adjustments']:
|
||||||
|
if not isinstance(adj, dict):
|
||||||
|
raise ValueError('adjustment must be an object')
|
||||||
|
if adj.get('feature') not in FEATURE_DIMS:
|
||||||
|
raise ValueError('bad adjustment feature: %r' % adj.get('feature'))
|
||||||
|
if not isinstance(adj.get('delta'), (int, float)):
|
||||||
|
raise ValueError('adjustment delta must be numeric')
|
||||||
|
wc = payload['wildcards']
|
||||||
|
if not isinstance(wc, dict):
|
||||||
|
raise ValueError('wildcards must be an object')
|
||||||
|
if (not isinstance(wc.get('families', []), list)
|
||||||
|
or not isinstance(wc.get('price_bands', []), list)):
|
||||||
|
raise ValueError('wildcard hints must be lists')
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_json(text):
|
||||||
|
"""Pull the first JSON object out of a model reply (fence-tolerant)."""
|
||||||
|
text = (text or '').strip()
|
||||||
|
if text.startswith('```'):
|
||||||
|
lines = text.splitlines()
|
||||||
|
while lines and lines[0].startswith('```'):
|
||||||
|
lines = lines[1:]
|
||||||
|
while lines and lines[-1].startswith('```'):
|
||||||
|
lines = lines[:-1]
|
||||||
|
text = '\n'.join(lines).strip()
|
||||||
|
start = text.find('{')
|
||||||
|
end = text.rfind('}')
|
||||||
|
if start < 0 or end <= start:
|
||||||
|
raise ValueError('no JSON object in reply')
|
||||||
|
return json.loads(text[start:end + 1])
|
||||||
|
|
||||||
|
|
||||||
|
# --- the pass -----------------------------------------------------------------
|
||||||
|
|
||||||
|
def run_pass(db_path, source='mock', model=None, dry_run=False,
|
||||||
|
print_summary=False, out=sys.stdout):
|
||||||
|
"""Run one agent pass. Returns a result dict; never raises for live
|
||||||
|
failures (falls back to a deterministic profile, source='fallback')."""
|
||||||
|
started = now_iso()
|
||||||
|
conn = db.connect(db_path)
|
||||||
|
db.init_db(conn)
|
||||||
|
try:
|
||||||
|
now = _now_dt()
|
||||||
|
summary = build_summary(conn, now=now)
|
||||||
|
if print_summary:
|
||||||
|
out.write(summary + '\n')
|
||||||
|
model = model or os.environ.get('BT_AGENT_MODEL') or DEFAULT_MODEL
|
||||||
|
if source == 'mock':
|
||||||
|
payload = synthesize_payload(conn, now=now)
|
||||||
|
profile_source = 'mock'
|
||||||
|
error = None
|
||||||
|
cost_note = None
|
||||||
|
else: # live
|
||||||
|
key = os.environ.get('OPENROUTER_API_KEY')
|
||||||
|
if not key:
|
||||||
|
payload = synthesize_payload(conn, now=now)
|
||||||
|
profile_source = 'fallback'
|
||||||
|
error = 'OPENROUTER_API_KEY not configured'
|
||||||
|
cost_note = None
|
||||||
|
else:
|
||||||
|
payload, profile_source, error, cost_note = _live_call(
|
||||||
|
model, summary)
|
||||||
|
if payload is None:
|
||||||
|
payload = synthesize_payload(conn, now=now)
|
||||||
|
profile_source = 'fallback'
|
||||||
|
validate_payload(payload)
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
return {'ok': True, 'dry_run': True, 'source': profile_source,
|
||||||
|
'model': model, 'payload': payload, 'summary': summary,
|
||||||
|
'error': error}
|
||||||
|
|
||||||
|
cur = conn.execute(
|
||||||
|
"""INSERT INTO taste_profile (created_at, source, payload)
|
||||||
|
VALUES (?, ?, ?)""",
|
||||||
|
(started, profile_source, json.dumps(payload)))
|
||||||
|
version = cur.lastrowid
|
||||||
|
finished = now_iso()
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO agent_runs (started_at, finished_at, ok, source,
|
||||||
|
model, cost_note, error,
|
||||||
|
profile_version)
|
||||||
|
VALUES (?,?,?,?,?,?,?,?)""",
|
||||||
|
(started, finished, 1 if error is None else 0, profile_source,
|
||||||
|
model, cost_note, (error or '')[:500], version))
|
||||||
|
conn.commit()
|
||||||
|
return {'ok': error is None, 'source': profile_source,
|
||||||
|
'profile_version': version, 'model': model,
|
||||||
|
'payload': payload, 'error': error, 'cost_note': cost_note}
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _live_call(model, summary):
|
||||||
|
"""OpenRouter call with one retry; returns (payload|None, source,
|
||||||
|
error, cost_note). Never leaks the key into error text or logs."""
|
||||||
|
import requests
|
||||||
|
key = os.environ['OPENROUTER_API_KEY']
|
||||||
|
body = {
|
||||||
|
'model': model,
|
||||||
|
'messages': [
|
||||||
|
{'role': 'system', 'content': SYSTEM_PROMPT},
|
||||||
|
{'role': 'user', 'content': summary},
|
||||||
|
],
|
||||||
|
'temperature': 0,
|
||||||
|
}
|
||||||
|
headers = {'Authorization': 'Bearer %s' % key,
|
||||||
|
'Content-Type': 'application/json'}
|
||||||
|
last_err = None
|
||||||
|
for attempt in (1, 2):
|
||||||
|
try:
|
||||||
|
r = requests.post(API_URL, headers=headers, json=body,
|
||||||
|
timeout=TIMEOUT)
|
||||||
|
if r.status_code == 200:
|
||||||
|
data = r.json()
|
||||||
|
text = (data.get('choices') or [{}])[0].get(
|
||||||
|
'message', {}).get('content', '')
|
||||||
|
try:
|
||||||
|
payload = validate_payload(_extract_json(text))
|
||||||
|
cost_note = _cost_note(data)
|
||||||
|
return payload, 'agent', None, cost_note
|
||||||
|
except (ValueError, json.JSONDecodeError) as e:
|
||||||
|
return None, None, 'malformed reply: %s' % str(e)[:300], None
|
||||||
|
last_err = 'HTTP %d: %s' % (r.status_code, r.text[:200])
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
last_err = 'network error: %s' % type(e).__name__
|
||||||
|
return None, None, (last_err or 'live call failed')[:500], None
|
||||||
|
|
||||||
|
|
||||||
|
def _cost_note(data):
|
||||||
|
usage = data.get('usage') or {}
|
||||||
|
cost = usage.get('cost')
|
||||||
|
note = 'tokens_in=%s tokens_out=%s' % (
|
||||||
|
usage.get('prompt_tokens'), usage.get('completion_tokens'))
|
||||||
|
if cost is not None:
|
||||||
|
note += ' cost=$%s' % cost
|
||||||
|
return note
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
ap = argparse.ArgumentParser(description='Bike-Tinder P3 agent pass')
|
||||||
|
ap.add_argument('--db', default='data/bike-tinder.sqlite3')
|
||||||
|
src = ap.add_mutually_exclusive_group()
|
||||||
|
src.add_argument('--mock', action='store_true',
|
||||||
|
help='deterministic offline pass (default)')
|
||||||
|
src.add_argument('--live', action='store_true',
|
||||||
|
help='OpenRouter pass (needs OPENROUTER_API_KEY)')
|
||||||
|
ap.add_argument('--model', default=None,
|
||||||
|
help='model override (env BT_AGENT_MODEL, else default)')
|
||||||
|
ap.add_argument('--print-summary', action='store_true',
|
||||||
|
help='print exactly the text the live pass would send')
|
||||||
|
ap.add_argument('--dry-run', action='store_true',
|
||||||
|
help='validate + print, write nothing')
|
||||||
|
args = ap.parse_args(argv)
|
||||||
|
result = run_pass(args.db, source='live' if args.live else 'mock',
|
||||||
|
model=args.model, dry_run=args.dry_run,
|
||||||
|
print_summary=args.print_summary)
|
||||||
|
if args.dry_run:
|
||||||
|
print(json.dumps(result['payload'], indent=2))
|
||||||
|
print('dry-run ok; would write source=%s (model=%s)' % (
|
||||||
|
result['source'], result['model']))
|
||||||
|
if result.get('error'):
|
||||||
|
print('note: %s' % result['error'])
|
||||||
|
return 0
|
||||||
|
if result['ok']:
|
||||||
|
print('pass ok: source=%s version=%d model=%s%s' % (
|
||||||
|
result['source'], result['profile_version'], result['model'],
|
||||||
|
' (%s)' % result['cost_note'] if result.get('cost_note') else ''))
|
||||||
|
return 0
|
||||||
|
print('pass fell back: source=fallback version=%d error=%s' % (
|
||||||
|
result['profile_version'], result['error']))
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
409
app/api.py
409
app/api.py
@@ -12,15 +12,21 @@ on; frozen in the task brief):
|
|||||||
url, img ("/img/{id}" if img_url or cached file exists else null), desc,
|
url, img ("/img/{id}" if img_url or cached file exists else null), desc,
|
||||||
family, score, year, seller_type, extras (array of strings),
|
family, score, year, seller_type, extras (array of strings),
|
||||||
decision (null|'like'|'maybe'|'nope'), first_seen}
|
decision (null|'like'|'maybe'|'nope'), first_seen}
|
||||||
|
B1 additions (UI-SPEC §11 / DATA-MODEL "B1 additions"):
|
||||||
|
n_photos (int), last_photo_idx (int|null); GET /img/{id}?i=n serves
|
||||||
|
gallery photo n.
|
||||||
"""
|
"""
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from fastapi import FastAPI, HTTPException, Query
|
from fastapi import FastAPI, HTTPException, Query
|
||||||
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
|
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
|
||||||
|
|
||||||
|
from . import agentpass as agmod
|
||||||
from . import db as dbmod
|
from . import db as dbmod
|
||||||
from . import importer as imp
|
from . import importer as imp
|
||||||
|
from . import sections as secmod
|
||||||
|
|
||||||
ACTIONS = ('like', 'nope', 'maybe')
|
ACTIONS = ('like', 'nope', 'maybe')
|
||||||
|
|
||||||
@@ -39,7 +45,7 @@ DECK_WHERE = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
DECK_SELECT = (
|
DECK_SELECT = (
|
||||||
"SELECT l.*, s.action AS decision"
|
"SELECT l.*, s.action AS decision, s.last_photo_idx AS last_photo_idx"
|
||||||
" FROM listings l LEFT JOIN swipes s ON s.listing_id = l.id"
|
" FROM listings l LEFT JOIN swipes s ON s.listing_id = l.id"
|
||||||
" WHERE " + DECK_WHERE
|
" WHERE " + DECK_WHERE
|
||||||
)
|
)
|
||||||
@@ -73,6 +79,14 @@ def get_conn(app):
|
|||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def utcnow():
|
||||||
|
return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
|
def live_configured():
|
||||||
|
return bool(os.environ.get('OPENROUTER_API_KEY'))
|
||||||
|
|
||||||
|
|
||||||
def set_pref(conn, key, value):
|
def set_pref(conn, key, value):
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO prefs (key, value) VALUES (?, ?) "
|
"INSERT INTO prefs (key, value) VALUES (?, ?) "
|
||||||
@@ -117,10 +131,21 @@ def count_deck_after(app, score, first_seen, listing_id):
|
|||||||
|
|
||||||
def build_payload(img_dir):
|
def build_payload(img_dir):
|
||||||
def _payload(conn, row):
|
def _payload(conn, row):
|
||||||
img = None
|
leg = os.path.join(img_dir, '%d.webp' % row['id'])
|
||||||
if row['img_url'] or os.path.exists(
|
b1 = os.path.join(img_dir, '%d_0.webp' % row['id'])
|
||||||
os.path.join(img_dir, '%d.webp' % row['id'])):
|
has_photo = (bool(row['img_url']) or os.path.exists(leg)
|
||||||
img = '/img/%d' % row['id']
|
or os.path.exists(b1))
|
||||||
|
img = '/img/%d' % row['id'] if has_photo else None
|
||||||
|
# B1: gallery size + swipe-time photo index (UI-SPEC §11 / DATA-MODEL B1)
|
||||||
|
imgs = []
|
||||||
|
if isinstance(row['imgs'], str) and row['imgs']:
|
||||||
|
try:
|
||||||
|
imgs = json.loads(row['imgs'])
|
||||||
|
except ValueError:
|
||||||
|
imgs = []
|
||||||
|
if not isinstance(imgs, list):
|
||||||
|
imgs = []
|
||||||
|
n_photos = len(imgs) if imgs else (1 if has_photo else 0)
|
||||||
extras = row['extras']
|
extras = row['extras']
|
||||||
if isinstance(extras, str):
|
if isinstance(extras, str):
|
||||||
try:
|
try:
|
||||||
@@ -137,6 +162,9 @@ def build_payload(img_dir):
|
|||||||
'region': row['region'],
|
'region': row['region'],
|
||||||
'url': row['url'],
|
'url': row['url'],
|
||||||
'img': img,
|
'img': img,
|
||||||
|
'n_photos': n_photos,
|
||||||
|
'last_photo_idx': (row['last_photo_idx']
|
||||||
|
if 'last_photo_idx' in row.keys() else None),
|
||||||
'desc': row['desc'],
|
'desc': row['desc'],
|
||||||
'family': row['family'],
|
'family': row['family'],
|
||||||
'score': row['score'],
|
'score': row['score'],
|
||||||
@@ -149,6 +177,23 @@ def build_payload(img_dir):
|
|||||||
return _payload
|
return _payload
|
||||||
|
|
||||||
|
|
||||||
|
# --- P3 listing payload (additive over the P2 contract) --------------------
|
||||||
|
|
||||||
|
def build_p3_payload(img_dir):
|
||||||
|
base = build_payload(img_dir)
|
||||||
|
|
||||||
|
def _payload(conn, engine, row, slot=None, include_breakdown=False):
|
||||||
|
p = base(conn, row)
|
||||||
|
p['points'] = engine.points(row['id'])
|
||||||
|
p['tank'] = engine.tank(row['id'])
|
||||||
|
p['slot'] = slot
|
||||||
|
p['round_next'] = engine.round_next(row['id'])
|
||||||
|
p['points_breakdown'] = (engine.breakdown(row['id'])
|
||||||
|
if include_breakdown else None)
|
||||||
|
return p
|
||||||
|
return _payload
|
||||||
|
|
||||||
|
|
||||||
# --- app factory ----------------------------------------------------------
|
# --- app factory ----------------------------------------------------------
|
||||||
|
|
||||||
def create_app(db_path, img_dir, state_dir=None):
|
def create_app(db_path, img_dir, state_dir=None):
|
||||||
@@ -166,6 +211,7 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
app.state.static_dir = static_dir
|
app.state.static_dir = static_dir
|
||||||
app.state.icons_dir = icons_dir
|
app.state.icons_dir = icons_dir
|
||||||
payload = build_payload(app.state.img_dir)
|
payload = build_payload(app.state.img_dir)
|
||||||
|
p3_payload = build_p3_payload(app.state.img_dir)
|
||||||
|
|
||||||
# -- meta -----------------------------------------------------------
|
# -- meta -----------------------------------------------------------
|
||||||
|
|
||||||
@@ -192,6 +238,14 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
live = conn.execute(
|
live = conn.execute(
|
||||||
"SELECT COUNT(*) AS n FROM listings WHERE status='active'"
|
"SELECT COUNT(*) AS n FROM listings WHERE status='active'"
|
||||||
).fetchone()['n']
|
).fetchone()['n']
|
||||||
|
# item 7: fresh = active with no swipe row (the deck's new-pool
|
||||||
|
# predicate, verbatim). A swipe of ANY age removes freshness.
|
||||||
|
fresh = conn.execute(
|
||||||
|
"SELECT COUNT(*) AS n FROM listings l"
|
||||||
|
" WHERE l.status='active'"
|
||||||
|
" AND NOT EXISTS (SELECT 1 FROM swipes s"
|
||||||
|
" WHERE s.listing_id = l.id)"
|
||||||
|
).fetchone()['n']
|
||||||
badge = get_pref(conn, 'badge_seen_at') or ''
|
badge = get_pref(conn, 'badge_seen_at') or ''
|
||||||
new = conn.execute(
|
new = conn.execute(
|
||||||
"SELECT COUNT(*) AS n FROM listings"
|
"SELECT COUNT(*) AS n FROM listings"
|
||||||
@@ -207,6 +261,7 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
' ORDER BY id DESC LIMIT 1').fetchone()
|
' ORDER BY id DESC LIMIT 1').fetchone()
|
||||||
return {
|
return {
|
||||||
'live': live,
|
'live': live,
|
||||||
|
'fresh': fresh,
|
||||||
'new': new,
|
'new': new,
|
||||||
'likes': per['like'],
|
'likes': per['like'],
|
||||||
'maybes': per['maybe'],
|
'maybes': per['maybe'],
|
||||||
@@ -225,6 +280,7 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
cur = parse_cursor(cursor)
|
cur = parse_cursor(cursor)
|
||||||
conn = get_conn(app)
|
conn = get_conn(app)
|
||||||
try:
|
try:
|
||||||
|
engine = secmod.build_engine(conn, now=utcnow())
|
||||||
if cur is None:
|
if cur is None:
|
||||||
rows = conn.execute(DECK_SELECT + DECK_ORDER + ' LIMIT ?',
|
rows = conn.execute(DECK_SELECT + DECK_ORDER + ' LIMIT ?',
|
||||||
(limit,)).fetchall()
|
(limit,)).fetchall()
|
||||||
@@ -232,7 +288,7 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
DECK_SELECT + cursor_clause() + DECK_ORDER + ' LIMIT ?',
|
DECK_SELECT + cursor_clause() + DECK_ORDER + ' LIMIT ?',
|
||||||
(cur[0], cur[0], cur[1], cur[1], cur[2], limit)).fetchall()
|
(cur[0], cur[0], cur[1], cur[1], cur[2], limit)).fetchall()
|
||||||
items = [payload(conn, r) for r in rows]
|
items = [p3_payload(conn, engine, r) for r in rows]
|
||||||
next_cursor = None
|
next_cursor = None
|
||||||
remaining = 0
|
remaining = 0
|
||||||
if rows:
|
if rows:
|
||||||
@@ -256,6 +312,19 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
if not isinstance(listing_id, int) or action not in ACTIONS:
|
if not isinstance(listing_id, int) or action not in ACTIONS:
|
||||||
raise HTTPException(status_code=400,
|
raise HTTPException(status_code=400,
|
||||||
detail='body must be {listing_id:int, action:like|nope|maybe}')
|
detail='body must be {listing_id:int, action:like|nope|maybe}')
|
||||||
|
# optional P3: section this verdict happened in (NULL for list edits)
|
||||||
|
section_id = body.get('section_id')
|
||||||
|
if not isinstance(section_id, int):
|
||||||
|
section_id = None
|
||||||
|
# B1: optional photo index on screen at swipe time (§11.5 rotation).
|
||||||
|
has_photo_idx = 'photo_idx' in body
|
||||||
|
photo_idx = body.get('photo_idx')
|
||||||
|
if has_photo_idx and photo_idx is not None:
|
||||||
|
if (isinstance(photo_idx, bool) or not isinstance(photo_idx, int)
|
||||||
|
or photo_idx < 0):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail='photo_idx must be a non-negative int (or null)')
|
||||||
conn = get_conn(app)
|
conn = get_conn(app)
|
||||||
try:
|
try:
|
||||||
row = conn.execute('SELECT id FROM listings WHERE id=?',
|
row = conn.execute('SELECT id FROM listings WHERE id=?',
|
||||||
@@ -263,17 +332,36 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(status_code=404, detail='unknown listing')
|
raise HTTPException(status_code=404, detail='unknown listing')
|
||||||
now = imp.now_iso()
|
now = imp.now_iso()
|
||||||
|
if has_photo_idx:
|
||||||
|
# explicit photo_idx (int or null) overwrites; an absent key
|
||||||
|
# preserves the stored index (list edits etc.)
|
||||||
|
conn.execute(
|
||||||
|
'''INSERT INTO swipes (listing_id, action, created_at,
|
||||||
|
updated_at, last_photo_idx)
|
||||||
|
VALUES (?,?,?,?,?)
|
||||||
|
ON CONFLICT(listing_id) DO UPDATE SET
|
||||||
|
action=excluded.action, updated_at=excluded.updated_at,
|
||||||
|
last_photo_idx=excluded.last_photo_idx''',
|
||||||
|
(listing_id, action, now, now, photo_idx))
|
||||||
|
else:
|
||||||
conn.execute(
|
conn.execute(
|
||||||
'''INSERT INTO swipes (listing_id, action, created_at, updated_at)
|
'''INSERT INTO swipes (listing_id, action, created_at, updated_at)
|
||||||
VALUES (?,?,?,?)
|
VALUES (?,?,?,?)
|
||||||
ON CONFLICT(listing_id) DO UPDATE SET
|
ON CONFLICT(listing_id) DO UPDATE SET
|
||||||
action=excluded.action, updated_at=excluded.updated_at''',
|
action=excluded.action, updated_at=excluded.updated_at''',
|
||||||
(listing_id, action, now, now))
|
(listing_id, action, now, now))
|
||||||
|
# P3: append the learning event (round = prior events + 1)
|
||||||
|
round_ = dbmod.append_event(conn, listing_id, action, section_id,
|
||||||
|
now)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
stored = conn.execute(
|
stored = conn.execute(
|
||||||
'SELECT listing_id, action, created_at, updated_at'
|
'SELECT listing_id, action, created_at, updated_at,'
|
||||||
' FROM swipes WHERE listing_id=?', (listing_id,)).fetchone()
|
' last_photo_idx FROM swipes WHERE listing_id=?',
|
||||||
return dict(stored)
|
(listing_id,)).fetchone()
|
||||||
|
result = dict(stored)
|
||||||
|
result['round'] = round_
|
||||||
|
result['section_id'] = section_id
|
||||||
|
return result
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
@@ -307,16 +395,19 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
params.append('%' + escaped + '%')
|
params.append('%' + escaped + '%')
|
||||||
conn = get_conn(app)
|
conn = get_conn(app)
|
||||||
try:
|
try:
|
||||||
|
engine = secmod.build_engine(conn, now=utcnow())
|
||||||
total = conn.execute(
|
total = conn.execute(
|
||||||
'SELECT COUNT(*) AS n FROM listings l'
|
'SELECT COUNT(*) AS n FROM listings l'
|
||||||
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
||||||
' WHERE ' + where, params).fetchone()['n']
|
' WHERE ' + where, params).fetchone()['n']
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
'SELECT l.*, s.action AS decision FROM listings l'
|
'SELECT l.*, s.action AS decision,'
|
||||||
|
' s.last_photo_idx AS last_photo_idx FROM listings l'
|
||||||
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
||||||
' WHERE ' + where + ' ORDER BY ' + LIST_SORTS[sort] +
|
' WHERE ' + where + ' ORDER BY ' + LIST_SORTS[sort] +
|
||||||
' LIMIT ? OFFSET ?', params + [limit, offset]).fetchall()
|
' LIMIT ? OFFSET ?', params + [limit, offset]).fetchall()
|
||||||
return {'items': [payload(conn, r) for r in rows], 'total': total}
|
return {'items': [p3_payload(conn, engine, r) for r in rows],
|
||||||
|
'total': total}
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
@@ -324,13 +415,15 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
def listing(listing_id: int):
|
def listing(listing_id: int):
|
||||||
conn = get_conn(app)
|
conn = get_conn(app)
|
||||||
try:
|
try:
|
||||||
|
engine = secmod.build_engine(conn, now=utcnow())
|
||||||
row = conn.execute(
|
row = conn.execute(
|
||||||
'SELECT l.*, s.action AS decision FROM listings l'
|
'SELECT l.*, s.action AS decision,'
|
||||||
|
' s.last_photo_idx AS last_photo_idx FROM listings l'
|
||||||
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
' LEFT JOIN swipes s ON s.listing_id = l.id'
|
||||||
' WHERE l.id=?', (listing_id,)).fetchone()
|
' WHERE l.id=?', (listing_id,)).fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(status_code=404, detail='unknown listing')
|
raise HTTPException(status_code=404, detail='unknown listing')
|
||||||
return payload(conn, row)
|
return p3_payload(conn, engine, row, include_breakdown=True)
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
@@ -357,21 +450,301 @@ def create_app(db_path, img_dir, state_dir=None):
|
|||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
# -- P3: sections ----------------------------------------------------
|
||||||
|
|
||||||
|
def section_state(conn, sec, note=None):
|
||||||
|
"""Shared body for GET /api/section and POST /api/section/forge."""
|
||||||
|
now = utcnow()
|
||||||
|
engine = secmod.build_engine(conn, now=now)
|
||||||
|
data = secmod.read_section(conn, sec, now)
|
||||||
|
sec = conn.execute('SELECT * FROM sections WHERE id=?',
|
||||||
|
(sec['id'],)).fetchone()
|
||||||
|
mode = get_pref(conn, 'forge_mode', 'ondemand')
|
||||||
|
decided_ids = secmod.section_decided_ids(conn, sec)
|
||||||
|
items = [p3_payload(conn, engine, r, slot=r['slot'],
|
||||||
|
include_breakdown=True)
|
||||||
|
for r in data['items']]
|
||||||
|
for p in items:
|
||||||
|
# item 5: decided IN this section (real verdict), not the joined
|
||||||
|
# lifetime swipes decision — present on EVERY item so the FE's
|
||||||
|
# local-intent branch can't misfire
|
||||||
|
p['decided_this_section'] = p['id'] in decided_ids
|
||||||
|
data['items'] = items
|
||||||
|
data['mode'] = mode
|
||||||
|
data['recipe'] = (json.loads(sec['recipe']) if sec['recipe'] else {})
|
||||||
|
data['pool'] = secmod.pool_counts(conn)
|
||||||
|
data['prefs'] = {'rubric_debug': get_pref(conn, 'rubric_debug', 'off')}
|
||||||
|
data['can_forge'] = (mode != 'strict'
|
||||||
|
or secmod.fresh_profile_exists(conn, sec))
|
||||||
|
data['counts'] = secmod.section_counts(conn, sec)
|
||||||
|
prof = secmod.latest_profile(conn)
|
||||||
|
data['read_info'] = {
|
||||||
|
'events': engine.n_events,
|
||||||
|
'profile_version': prof['version'] if prof else None,
|
||||||
|
'profile_source': prof['source'] if prof else None,
|
||||||
|
'profile_at': prof['created_at'] if prof else None,
|
||||||
|
}
|
||||||
|
if note:
|
||||||
|
data['note'] = note
|
||||||
|
return data
|
||||||
|
|
||||||
|
@app.get('/api/section')
|
||||||
|
def get_section():
|
||||||
|
conn = get_conn(app)
|
||||||
|
try:
|
||||||
|
sec = secmod.get_current_section(conn)
|
||||||
|
if sec is None:
|
||||||
|
# first-ever read composes a cold section
|
||||||
|
prof = secmod.latest_profile(conn)
|
||||||
|
engine = secmod.build_engine(conn, prof, now=utcnow())
|
||||||
|
sec = secmod.compose_section(
|
||||||
|
conn, engine, size=25,
|
||||||
|
profile_version=prof['version'] if prof else None,
|
||||||
|
note='cold compose (first read)')
|
||||||
|
else:
|
||||||
|
# item 8: an empty composed section (the catalog was fully
|
||||||
|
# drained) recomposes on the read once stock exists — the
|
||||||
|
# nightly-import recovery path. Zero items + zero pool
|
||||||
|
# keeps serving the empty section (loud empty2).
|
||||||
|
n_items = conn.execute(
|
||||||
|
'SELECT COUNT(*) AS n FROM section_items'
|
||||||
|
' WHERE section_id=?', (sec['id'],)).fetchone()['n']
|
||||||
|
if n_items == 0 and any(secmod.pool_counts(conn).values()):
|
||||||
|
prof = secmod.latest_profile(conn)
|
||||||
|
engine = secmod.build_engine(conn, prof, now=utcnow())
|
||||||
|
secmod.close_section(conn, sec, utcnow(), status='closed')
|
||||||
|
sec = secmod.compose_section(
|
||||||
|
conn, engine, size=25,
|
||||||
|
profile_version=prof['version'] if prof else None,
|
||||||
|
note='refill compose (stock returned)')
|
||||||
|
return section_state(conn, sec)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
@app.post('/api/section/forge')
|
||||||
|
def forge(body: dict):
|
||||||
|
size = body.get('size', 25)
|
||||||
|
if not isinstance(size, int) or not 1 <= size <= 100:
|
||||||
|
raise HTTPException(status_code=400,
|
||||||
|
detail='size must be an int 1..100')
|
||||||
|
conn = get_conn(app)
|
||||||
|
try:
|
||||||
|
mode = get_pref(conn, 'forge_mode', 'ondemand')
|
||||||
|
sec = secmod.get_current_section(conn)
|
||||||
|
note = None
|
||||||
|
if mode == 'strict':
|
||||||
|
if not secmod.fresh_profile_exists(conn, sec):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=409, detail=(
|
||||||
|
'strict mode: no fresh agent pass since this '
|
||||||
|
'section — the nightly read provides it'))
|
||||||
|
note = 'strict: forged on the fresh pass'
|
||||||
|
else:
|
||||||
|
# on-demand: agent pass between sections (mock when the
|
||||||
|
# live key is not configured; live failures fall back)
|
||||||
|
src = 'live' if live_configured() else 'mock'
|
||||||
|
res = agmod.run_pass(app.state.db_path, source=src)
|
||||||
|
note = ('pass %s v%d' % (res['source'],
|
||||||
|
res['profile_version']))
|
||||||
|
if res['source'] == 'fallback':
|
||||||
|
note += ' (agent offline — last read used)'
|
||||||
|
if sec is not None:
|
||||||
|
secmod.close_section(conn, sec, utcnow())
|
||||||
|
prof = secmod.latest_profile(conn)
|
||||||
|
engine = secmod.build_engine(conn, prof, now=utcnow())
|
||||||
|
newsec = secmod.compose_section(
|
||||||
|
conn, engine, size=size,
|
||||||
|
profile_version=prof['version'] if prof else None,
|
||||||
|
note=note)
|
||||||
|
# v1.1 item 6: label the dry-catalog state on the forge note
|
||||||
|
recipe = (json.loads(newsec['recipe'])
|
||||||
|
if newsec['recipe'] else {})
|
||||||
|
if recipe.get('new') == 0:
|
||||||
|
note = '%s · catalog fully reviewed' % (note or '')
|
||||||
|
return section_state(conn, newsec, note=note)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
# -- P3: trends ------------------------------------------------------
|
||||||
|
|
||||||
|
@app.get('/api/trends')
|
||||||
|
def trends():
|
||||||
|
conn = get_conn(app)
|
||||||
|
try:
|
||||||
|
prof = secmod.latest_profile(conn)
|
||||||
|
profile = None
|
||||||
|
if prof:
|
||||||
|
payload_prof = {}
|
||||||
|
try:
|
||||||
|
payload_prof = json.loads(prof['payload'])
|
||||||
|
except ValueError:
|
||||||
|
payload_prof = {}
|
||||||
|
maker = conn.execute(
|
||||||
|
'SELECT * FROM agent_runs WHERE profile_version=?'
|
||||||
|
' ORDER BY id DESC LIMIT 1', (prof['version'],)
|
||||||
|
).fetchone()
|
||||||
|
profile = {
|
||||||
|
'version': prof['version'],
|
||||||
|
'source': prof['source'],
|
||||||
|
'created_at': prof['created_at'],
|
||||||
|
'model': maker['model'] if maker else None,
|
||||||
|
'lead': payload_prof.get('lead', ''),
|
||||||
|
'leanings': payload_prof.get('leanings', [])[:8],
|
||||||
|
'changes': payload_prof.get('changes', []),
|
||||||
|
'chips': payload_prof.get('chips', []),
|
||||||
|
}
|
||||||
|
per = {}
|
||||||
|
for action in ('like', 'maybe', 'nope'):
|
||||||
|
per[action] = conn.execute(
|
||||||
|
'SELECT COUNT(*) AS n FROM swipes WHERE action=?',
|
||||||
|
(action,)).fetchone()['n']
|
||||||
|
decided = per['like'] + per['maybe'] + per['nope']
|
||||||
|
events = conn.execute(
|
||||||
|
'SELECT listing_id, action FROM swipe_events ORDER BY id'
|
||||||
|
).fetchall()
|
||||||
|
prev = {}
|
||||||
|
flips = 0
|
||||||
|
for ev in events:
|
||||||
|
if ev['listing_id'] in prev and prev[ev['listing_id']] != ev[
|
||||||
|
'action']:
|
||||||
|
flips += 1
|
||||||
|
prev[ev['listing_id']] = ev['action']
|
||||||
|
ret_placed = conn.execute(
|
||||||
|
"SELECT COUNT(*) AS n FROM section_items "
|
||||||
|
"WHERE slot='return'").fetchone()['n']
|
||||||
|
ret_struck = conn.execute(
|
||||||
|
"""SELECT COUNT(DISTINCT e.listing_id) AS n
|
||||||
|
FROM swipe_events e
|
||||||
|
JOIN section_items si
|
||||||
|
ON si.section_id = e.section_id
|
||||||
|
AND si.listing_id = e.listing_id
|
||||||
|
WHERE si.slot='return'""").fetchone()['n']
|
||||||
|
wild_placed = conn.execute(
|
||||||
|
"SELECT COUNT(*) AS n FROM section_items "
|
||||||
|
"WHERE slot='wildcard'").fetchone()['n']
|
||||||
|
wild_hits = conn.execute(
|
||||||
|
"""SELECT COUNT(DISTINCT e.listing_id) AS n
|
||||||
|
FROM swipe_events e
|
||||||
|
JOIN section_items si
|
||||||
|
ON si.section_id = e.section_id
|
||||||
|
AND si.listing_id = e.listing_id
|
||||||
|
WHERE si.slot='wildcard' AND e.action IN ('like','maybe')"""
|
||||||
|
).fetchone()['n']
|
||||||
|
last_run = None
|
||||||
|
row = conn.execute(
|
||||||
|
'SELECT * FROM agent_runs ORDER BY id DESC LIMIT 1'
|
||||||
|
).fetchone()
|
||||||
|
if row:
|
||||||
|
last_run = {
|
||||||
|
'started_at': row['started_at'],
|
||||||
|
'finished_at': row['finished_at'],
|
||||||
|
'ok': bool(row['ok']),
|
||||||
|
'source': row['source'],
|
||||||
|
'model': row['model'],
|
||||||
|
'cost_note': row['cost_note'],
|
||||||
|
'profile_version': row['profile_version'],
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
'profile': profile,
|
||||||
|
'stats': {
|
||||||
|
'decided': decided,
|
||||||
|
'likes': per['like'],
|
||||||
|
'maybes': per['maybe'],
|
||||||
|
'nopes': per['nope'],
|
||||||
|
'hit_rate': (per['like'] / decided) if decided else None,
|
||||||
|
'hit_rate_plus_maybe': (
|
||||||
|
(per['like'] + per['maybe']) / decided
|
||||||
|
if decided else None),
|
||||||
|
'returns_placed': ret_placed,
|
||||||
|
'returns_re_struck': ret_struck,
|
||||||
|
'wildcards_placed': wild_placed,
|
||||||
|
'wildcards_hit': wild_hits,
|
||||||
|
'flips': flips,
|
||||||
|
},
|
||||||
|
'last_run': last_run,
|
||||||
|
'cold_start': decided < 10,
|
||||||
|
'forge_mode': get_pref(conn, 'forge_mode', 'ondemand'),
|
||||||
|
}
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
# -- P3: agent pass trigger ------------------------------------------
|
||||||
|
|
||||||
|
@app.post('/api/agent/run')
|
||||||
|
def agent_run(body: dict):
|
||||||
|
source = body.get('source') if isinstance(body, dict) else None
|
||||||
|
if source is None:
|
||||||
|
source = 'live' if live_configured() else 'mock'
|
||||||
|
if source not in ('mock', 'live'):
|
||||||
|
raise HTTPException(status_code=400,
|
||||||
|
detail='source must be mock|live')
|
||||||
|
if source == 'live' and not live_configured():
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail='live pass not configured (OPENROUTER_API_KEY unset)')
|
||||||
|
model = body.get('model') if isinstance(body, dict) else None
|
||||||
|
res = agmod.run_pass(app.state.db_path, source=source, model=model)
|
||||||
|
return res
|
||||||
|
|
||||||
|
# -- prefs (P3 forge_mode toggle persistence) -------------------------
|
||||||
|
|
||||||
|
@app.post('/api/prefs')
|
||||||
|
def set_pref_endpoint(body: dict):
|
||||||
|
key = body.get('key')
|
||||||
|
value = body.get('value')
|
||||||
|
allowed = {'forge_mode': ('ondemand', 'strict'),
|
||||||
|
'rubric_debug': ('off', 'on')}
|
||||||
|
if key not in allowed or value not in allowed[key]:
|
||||||
|
raise HTTPException(status_code=400,
|
||||||
|
detail='unknown or invalid pref')
|
||||||
|
conn = get_conn(app)
|
||||||
|
try:
|
||||||
|
set_pref(conn, key, value)
|
||||||
|
conn.commit()
|
||||||
|
return {'key': key, 'value': value}
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
# -- images + static -------------------------------------------------
|
# -- images + static -------------------------------------------------
|
||||||
|
|
||||||
@app.get('/img/{listing_id}')
|
@app.get('/img/{listing_id}')
|
||||||
def img(listing_id: int):
|
def img(listing_id: int, i: int = Query(0, ge=0)):
|
||||||
fpath = os.path.join(app.state.img_dir, '%d.webp' % listing_id)
|
"""Photo n of a gallery (B1 §11.1): mirrored file -> CDN 302 -> 404.
|
||||||
|
|
||||||
|
i=0 keeps the legacy chain ({id}.webp, with {id}_0.webp accepted as
|
||||||
|
the gallery-mirror name for the primary); i>0 serves {id}_{n}.webp
|
||||||
|
with the CDN fallback taken from imgs[n].
|
||||||
|
"""
|
||||||
|
fpath = os.path.join(
|
||||||
|
app.state.img_dir,
|
||||||
|
'%d.webp' % listing_id if i == 0 else
|
||||||
|
'%d_%d.webp' % (listing_id, i))
|
||||||
|
if i == 0 and not os.path.exists(fpath):
|
||||||
|
alt = os.path.join(app.state.img_dir, '%d_0.webp' % listing_id)
|
||||||
|
if os.path.exists(alt):
|
||||||
|
fpath = alt
|
||||||
if os.path.exists(fpath):
|
if os.path.exists(fpath):
|
||||||
return FileResponse(fpath, media_type='image/webp')
|
return FileResponse(fpath, media_type='image/webp')
|
||||||
conn = get_conn(app)
|
conn = get_conn(app)
|
||||||
try:
|
try:
|
||||||
row = conn.execute('SELECT img_url FROM listings WHERE id=?',
|
row = conn.execute(
|
||||||
|
'SELECT img_url, imgs FROM listings WHERE id=?',
|
||||||
(listing_id,)).fetchone()
|
(listing_id,)).fetchone()
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
if row and row['img_url']:
|
url = None
|
||||||
return RedirectResponse(url=row['img_url'], status_code=302)
|
if row:
|
||||||
|
if i == 0 and row['img_url']:
|
||||||
|
url = row['img_url']
|
||||||
|
elif row['imgs']:
|
||||||
|
try:
|
||||||
|
arr = json.loads(row['imgs'])
|
||||||
|
except ValueError:
|
||||||
|
arr = []
|
||||||
|
if isinstance(arr, list) and len(arr) > i and arr[i]:
|
||||||
|
url = arr[i]
|
||||||
|
if url:
|
||||||
|
return RedirectResponse(url=url, status_code=302)
|
||||||
raise HTTPException(status_code=404, detail='no image')
|
raise HTTPException(status_code=404, detail='no image')
|
||||||
|
|
||||||
@app.get('/')
|
@app.get('/')
|
||||||
|
|||||||
112
app/db.py
112
app/db.py
@@ -2,12 +2,67 @@
|
|||||||
|
|
||||||
Stdlib only. The sqlite3 CLI is not installed on this host, so everything
|
Stdlib only. The sqlite3 CLI is not installed on this host, so everything
|
||||||
goes through the sqlite3 module.
|
goes through the sqlite3 module.
|
||||||
|
|
||||||
|
P3: init_db additionally creates the personalization tables
|
||||||
|
(swipe_events, sections, section_items, taste_profile, agent_runs),
|
||||||
|
migrates pre-P3 DBs in place (CREATE TABLE IF NOT EXISTS), and seeds
|
||||||
|
swipe_events from existing swipes rows exactly once (only-when-empty,
|
||||||
|
hence idempotent — see docs/DATA-MODEL.md "P3 additions").
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema.sql')
|
SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema.sql')
|
||||||
|
|
||||||
|
# P3 tables — guarded/idempotent; the locked v1 schema.sql stays untouched.
|
||||||
|
P3_SCHEMA = """
|
||||||
|
CREATE TABLE IF NOT EXISTS swipe_events (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
listing_id INTEGER NOT NULL REFERENCES listings(id) ON DELETE CASCADE,
|
||||||
|
action TEXT NOT NULL CHECK (action IN ('like','nope','maybe')),
|
||||||
|
round INTEGER NOT NULL DEFAULT 1,
|
||||||
|
section_id INTEGER,
|
||||||
|
created_at TEXT DEFAULT (datetime('now'))
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_events_listing ON swipe_events (listing_id, round);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_events_time ON swipe_events (created_at);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS sections (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open','report','closed')),
|
||||||
|
size INTEGER NOT NULL,
|
||||||
|
profile_version INTEGER,
|
||||||
|
recipe TEXT,
|
||||||
|
created_at TEXT DEFAULT (datetime('now')),
|
||||||
|
closed_at TEXT
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS section_items (
|
||||||
|
section_id INTEGER NOT NULL REFERENCES sections(id) ON DELETE CASCADE,
|
||||||
|
listing_id INTEGER NOT NULL REFERENCES listings(id) ON DELETE CASCADE,
|
||||||
|
slot TEXT NOT NULL CHECK (slot IN ('new','return','wildcard')),
|
||||||
|
position INTEGER NOT NULL,
|
||||||
|
PRIMARY KEY (section_id, listing_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_section_items_section ON section_items (section_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_section_items_listing ON section_items (listing_id);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS taste_profile (
|
||||||
|
version INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
created_at TEXT DEFAULT (datetime('now')),
|
||||||
|
source TEXT NOT NULL CHECK (source IN ('agent','mock','fallback')),
|
||||||
|
payload TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS agent_runs (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
started_at TEXT, finished_at TEXT,
|
||||||
|
ok INTEGER NOT NULL DEFAULT 0,
|
||||||
|
source TEXT, model TEXT, cost_note TEXT, error TEXT,
|
||||||
|
profile_version INTEGER
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def connect(db_path):
|
def connect(db_path):
|
||||||
"""Open a connection with WAL mode and a busy_timeout, then ensure schema."""
|
"""Open a connection with WAL mode and a busy_timeout, then ensure schema."""
|
||||||
@@ -23,11 +78,13 @@ def connect(db_path):
|
|||||||
|
|
||||||
|
|
||||||
def init_db(conn):
|
def init_db(conn):
|
||||||
"""Apply schema.sql (idempotent) + guarded P2 column migrations.
|
"""Apply schema.sql (idempotent) + guarded P2/P3 migrations.
|
||||||
|
|
||||||
P2 additions (docs/DATA-MODEL.md) are added with idempotent
|
P2 additions (docs/DATA-MODEL.md) are added with idempotent
|
||||||
``ALTER TABLE ... ADD COLUMN`` guarded by a PRAGMA check, so the locked
|
``ALTER TABLE ... ADD COLUMN`` guarded by a PRAGMA check, so the locked
|
||||||
v1 schema.sql stays untouched and existing DBs migrate in place.
|
v1 schema.sql stays untouched and existing DBs migrate in place.
|
||||||
|
P3 additions create the personalization tables (idempotent) and seed
|
||||||
|
swipe_events from swipes exactly once (when the events table is empty).
|
||||||
"""
|
"""
|
||||||
with open(SCHEMA_PATH, encoding='utf-8') as fh:
|
with open(SCHEMA_PATH, encoding='utf-8') as fh:
|
||||||
conn.executescript(fh.read())
|
conn.executescript(fh.read())
|
||||||
@@ -38,4 +95,57 @@ def init_db(conn):
|
|||||||
conn.execute('ALTER TABLE listings ADD COLUMN seller_type TEXT')
|
conn.execute('ALTER TABLE listings ADD COLUMN seller_type TEXT')
|
||||||
if 'extras' not in cols:
|
if 'extras' not in cols:
|
||||||
conn.execute('ALTER TABLE listings ADD COLUMN extras TEXT')
|
conn.execute('ALTER TABLE listings ADD COLUMN extras TEXT')
|
||||||
|
# B1 (photo experience v2): gallery URLs + swipe-time photo index.
|
||||||
|
if 'imgs' not in cols:
|
||||||
|
conn.execute('ALTER TABLE listings ADD COLUMN imgs TEXT')
|
||||||
|
sw_cols = {r['name'] for r in conn.execute(
|
||||||
|
"PRAGMA table_info('swipes')")}
|
||||||
|
if 'last_photo_idx' not in sw_cols:
|
||||||
|
conn.execute('ALTER TABLE swipes ADD COLUMN last_photo_idx INTEGER')
|
||||||
|
# P3 tables (idempotent on every call)
|
||||||
|
conn.executescript(P3_SCHEMA)
|
||||||
|
# P3 event seeding: one round-1 event per existing swipe, copied
|
||||||
|
# created_at. Only when swipe_events is empty — hence idempotent.
|
||||||
|
n_events = conn.execute(
|
||||||
|
'SELECT COUNT(*) AS n FROM swipe_events').fetchone()['n']
|
||||||
|
if n_events == 0:
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO swipe_events
|
||||||
|
(listing_id, action, round, section_id, created_at)
|
||||||
|
SELECT listing_id, action, 1, NULL, created_at
|
||||||
|
FROM swipes""")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def append_event(conn, listing_id, action, section_id=None, created_at=None):
|
||||||
|
"""Append one swipe event; round = prior events for this listing + 1.
|
||||||
|
|
||||||
|
Returns the assigned round. Every swipe write path must call this once
|
||||||
|
per verdict change (swipes stays the materialized latest verdict;
|
||||||
|
swipe_events is the append-only learning log).
|
||||||
|
"""
|
||||||
|
prior = conn.execute(
|
||||||
|
'SELECT COUNT(*) AS n FROM swipe_events WHERE listing_id=?',
|
||||||
|
(listing_id,)).fetchone()['n']
|
||||||
|
round_ = prior + 1
|
||||||
|
if created_at is None:
|
||||||
|
created_at = None # let the column default (datetime('now')) apply
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO swipe_events (listing_id, action, round, section_id)
|
||||||
|
VALUES (?,?,?,?)""",
|
||||||
|
(listing_id, action, round_, section_id))
|
||||||
|
else:
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO swipe_events
|
||||||
|
(listing_id, action, round, section_id, created_at)
|
||||||
|
VALUES (?,?,?,?,?)""",
|
||||||
|
(listing_id, action, round_, section_id, created_at))
|
||||||
|
return round_
|
||||||
|
|
||||||
|
|
||||||
|
def next_round(conn, listing_id):
|
||||||
|
"""Round the NEXT event on this listing will get (= prior events + 1)."""
|
||||||
|
prior = conn.execute(
|
||||||
|
'SELECT COUNT(*) AS n FROM swipe_events WHERE listing_id=?',
|
||||||
|
(listing_id,)).fetchone()['n']
|
||||||
|
return min(prior + 1, 2)
|
||||||
519
app/fe_walk_b1.py
Normal file
519
app/fe_walk_b1.py
Normal file
@@ -0,0 +1,519 @@
|
|||||||
|
"""B1 FE walk — headless-chromium proof of UI-SPEC §11 on the real page.
|
||||||
|
|
||||||
|
Not part of `app.selfcheck` (needs playwright + a live server); run it after
|
||||||
|
starting the scratch server (the brief's isolation recipe):
|
||||||
|
|
||||||
|
.venv/bin/python -m app.fe_walk_b1 setup # optional — auto at start
|
||||||
|
|
||||||
|
env -u OPENROUTER_API_KEY .venv/bin/python -m app.serve \
|
||||||
|
--db /var/home/admin/dev/bike-tinder-b1/test-walk.sqlite3 \
|
||||||
|
--img-dir /var/home/admin/dev/bike-tinder-b1/test-img \
|
||||||
|
--host 127.0.0.1 --port 9454 # in another shell (mock forge)
|
||||||
|
|
||||||
|
.venv/bin/python -m app.fe_walk_b1 # walk (this file)
|
||||||
|
|
||||||
|
Setup is deterministic and idempotent: a fresh copy of the pristine B1 copy
|
||||||
|
DB (test-copy.sqlite3) each run, synthetic multi-photo listings (ids 701+)
|
||||||
|
with local image files staged into test-img from media-sample/, and one
|
||||||
|
swiped return listing (705) whose stored last_photo_idx drives §11.5
|
||||||
|
rotation. Everything walks the REAL page: real pointers, real DOM, no
|
||||||
|
stubbed APIs.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
import sqlite3
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
ARENA = '/var/home/admin/dev/bike-tinder-b1'
|
||||||
|
PRISTINE = os.path.join(ARENA, 'test-copy.sqlite3')
|
||||||
|
WALK_DB = os.path.join(ARENA, 'test-walk.sqlite3')
|
||||||
|
IMG_DIR = os.path.join(ARENA, 'test-img')
|
||||||
|
MEDIA = os.path.join(ARENA, 'media-sample')
|
||||||
|
BASE_URL = 'http://127.0.0.1:9454'
|
||||||
|
|
||||||
|
CDN = ('https://media.kijiji.ca/api/v1/ca-prod-dealer-ads/images/x/%s'
|
||||||
|
'?rule=kijijica-640-webp')
|
||||||
|
|
||||||
|
# synthetic fixtures: listing_id -> (n_photos, label)
|
||||||
|
FIX = {
|
||||||
|
701: (8, 'gallery-8'),
|
||||||
|
702: (3, 'gallery-3'),
|
||||||
|
703: (2, 'gallery-2'),
|
||||||
|
704: (1, 'single-photo'),
|
||||||
|
705: (8, 'return-rotation'), # pre-swiped: last_photo_idx=0 -> lead 1
|
||||||
|
706: (0, 'zero-photo'),
|
||||||
|
}
|
||||||
|
SCORE_BASE = 2000
|
||||||
|
|
||||||
|
|
||||||
|
def stage_img_files():
|
||||||
|
"""{id}_0..{id}_{n-1}.webp (and {id}.webp for the legacy single) from
|
||||||
|
media-sample's real webp files — no network needed."""
|
||||||
|
os.makedirs(IMG_DIR, exist_ok=True)
|
||||||
|
names = sorted(m for m in os.listdir(MEDIA) if m.endswith('.webp'))
|
||||||
|
assert len(names) == 12, names
|
||||||
|
k = 0 # cycle through the 12 real files
|
||||||
|
for lid in sorted(FIX):
|
||||||
|
n = FIX[lid][0]
|
||||||
|
if n == 0:
|
||||||
|
continue
|
||||||
|
if n == 1:
|
||||||
|
shutil.copy2(os.path.join(MEDIA, names[k % len(names)]),
|
||||||
|
os.path.join(IMG_DIR, '%d.webp' % lid))
|
||||||
|
k += 1
|
||||||
|
continue
|
||||||
|
for i in range(n):
|
||||||
|
shutil.copy2(os.path.join(MEDIA, names[k % len(names)]),
|
||||||
|
os.path.join(IMG_DIR, '%d_%d.webp' % (lid, i)))
|
||||||
|
k += 1
|
||||||
|
|
||||||
|
|
||||||
|
def setup_db():
|
||||||
|
shutil.copy2(PRISTINE, WALK_DB)
|
||||||
|
from app import db as dbmod
|
||||||
|
conn = dbmod.connect(WALK_DB)
|
||||||
|
dbmod.init_db(conn) # B1 migrations
|
||||||
|
for lid in sorted(FIX):
|
||||||
|
n = FIX[lid][0]
|
||||||
|
imgs = [CDN % ('x%d-%d' % (lid, i)) for i in range(n)]
|
||||||
|
img = imgs[0] if imgs else None
|
||||||
|
score = SCORE_BASE + (750 - lid) # 701 highest -> top of the deck
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM listings WHERE id=?", (lid,))
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO listings (id, region, src_id, title, url, status,"
|
||||||
|
" img_url, imgs, score, first_seen, desc) VALUES"
|
||||||
|
" (?, 'ns', ?, ?, ?, 'active', ?, ?, ?,"
|
||||||
|
" '2026-11-11T00:00:00Z', ?)",
|
||||||
|
(lid, 'b1fix%d' % lid, 'B1 test %s' % FIX[lid][1],
|
||||||
|
'https://www.kijiji.ca/v-motorcycles/b1/%d' % lid,
|
||||||
|
img, json.dumps(imgs) if imgs else None, score, 'B1 fixture'))
|
||||||
|
conn.execute(
|
||||||
|
"INSERT OR REPLACE INTO swipes (listing_id, action, created_at,"
|
||||||
|
" last_photo_idx) VALUES (705, 'like', '2026-11-08T00:00:00Z', 0)")
|
||||||
|
# Close live-copied sections: the first /api/section read then composes
|
||||||
|
# a fresh 25 on the walk DB (deterministic fixture ordering).
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE sections SET status='closed' WHERE status != 'closed'")
|
||||||
|
# A round-1 event per fixture keeps synthetics out of the wildcard
|
||||||
|
# pool (wildcards must be unseen); new-pool eligibility is unaffected.
|
||||||
|
# 705's event matches its stored verdict (round 1, like).
|
||||||
|
for lid in FIX:
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO swipe_events (listing_id, action, round,"
|
||||||
|
" section_id) VALUES (?, ?, 1, NULL)",
|
||||||
|
(lid, 'like' if lid == 705 else 'maybe'))
|
||||||
|
conn.commit()
|
||||||
|
n = conn.execute('SELECT COUNT(*) AS n FROM listings').fetchone()['n']
|
||||||
|
conn.close()
|
||||||
|
print('setup: walk DB at %s (%s listings; 701-706 synthetic, 705 = liked'
|
||||||
|
' return w/ last_photo_idx=0)' % (WALK_DB, n))
|
||||||
|
|
||||||
|
|
||||||
|
def need_server():
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(BASE_URL + '/api/health',
|
||||||
|
timeout=3) as r:
|
||||||
|
return r.status == 200
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# --- walk helpers (module-level `page` assigned inside walk) ----------------
|
||||||
|
|
||||||
|
def wait(selector, timeout=8000):
|
||||||
|
page.wait_for_selector(selector, timeout=timeout)
|
||||||
|
|
||||||
|
|
||||||
|
def card_box():
|
||||||
|
b = page.evaluate("""() => { const el=document.querySelector('.topcard');
|
||||||
|
if(!el) return null; const r=el.getBoundingClientRect();
|
||||||
|
return {x:r.x,y:r.y,w:r.width,h:r.height}; }""")
|
||||||
|
assert b, 'no .topcard in DOM'
|
||||||
|
return b
|
||||||
|
|
||||||
|
|
||||||
|
def top_state():
|
||||||
|
return page.evaluate("""() => {
|
||||||
|
const t=document.querySelector('.topcard');
|
||||||
|
const r={topcards:document.querySelectorAll('.topcard').length,
|
||||||
|
hasSegs:!!t.classList.contains('hasSegs'), segs:0, segOn:-1,
|
||||||
|
photo:null, nophoto:false, band:null,
|
||||||
|
stampLike:0, stampNope:0, stampMaybe:0};
|
||||||
|
const s=t.querySelector('.segments');
|
||||||
|
if(s){ r.segs=s.children.length;
|
||||||
|
[...s.children].forEach((x,k)=>{ if(x.classList.contains('on'))
|
||||||
|
r.segOn=k; }); }
|
||||||
|
const p=t.querySelector('.phwrap img.photo');
|
||||||
|
if(p) r.photo=p.getAttribute('src');
|
||||||
|
else if(t.querySelector('.nophoto')) r.nophoto=true;
|
||||||
|
const b=t.querySelector('.band'); if(b) r.band=b.textContent;
|
||||||
|
const sl=t.querySelector('.stamp.like');
|
||||||
|
if(sl) r.stampLike=sl.style.opacity;
|
||||||
|
const sn=t.querySelector('.stamp.nope');
|
||||||
|
if(sn) r.stampNope=sn.style.opacity;
|
||||||
|
const sm=t.querySelector('.stamp.maybe');
|
||||||
|
if(sm) r.stampMaybe=sm.style.opacity;
|
||||||
|
return r; }""")
|
||||||
|
|
||||||
|
|
||||||
|
def tap_third(which):
|
||||||
|
b = card_box()
|
||||||
|
y = b['y'] + b['h'] * 0.72
|
||||||
|
x = b['x'] + b['w'] * (0.16 if which == 'left'
|
||||||
|
else (0.84 if which == 'right' else 0.5))
|
||||||
|
page.mouse.click(x, y)
|
||||||
|
time.sleep(0.08)
|
||||||
|
|
||||||
|
|
||||||
|
def assert_item(cond, msg):
|
||||||
|
if not cond:
|
||||||
|
print('FAIL ' + msg)
|
||||||
|
sys.exit(1)
|
||||||
|
print('ok ' + msg)
|
||||||
|
|
||||||
|
|
||||||
|
def lead_of(item):
|
||||||
|
n = item.get('n_photos') or 0
|
||||||
|
li = item.get('last_photo_idx')
|
||||||
|
if not n or n < 2 or li is None:
|
||||||
|
return 0
|
||||||
|
return (li + 1) % n
|
||||||
|
|
||||||
|
|
||||||
|
def photo_url(item, i):
|
||||||
|
if not item.get('img'):
|
||||||
|
return None
|
||||||
|
return item['img'] if i == 0 else '/img/%s?i=%s' % (item['id'], i)
|
||||||
|
|
||||||
|
|
||||||
|
def viewer_on():
|
||||||
|
return page.evaluate(
|
||||||
|
"() => document.getElementById('viewer').classList.contains('on')")
|
||||||
|
|
||||||
|
|
||||||
|
def vimg_src():
|
||||||
|
return page.evaluate(
|
||||||
|
"() => document.getElementById('vimg').getAttribute('src')")
|
||||||
|
|
||||||
|
|
||||||
|
def vimg_transform():
|
||||||
|
return page.evaluate(
|
||||||
|
"() => document.getElementById('vimg').style.transform")
|
||||||
|
|
||||||
|
|
||||||
|
# --- the walk ---------------------------------------------------------------
|
||||||
|
|
||||||
|
def walk():
|
||||||
|
from playwright.sync_api import sync_playwright
|
||||||
|
with sync_playwright() as pw:
|
||||||
|
exe = os.path.expanduser(
|
||||||
|
'~/.cache/ms-playwright/chromium-1243/chrome-linux64/chrome')
|
||||||
|
if not os.path.exists(exe):
|
||||||
|
exe = None
|
||||||
|
browser = pw.chromium.launch(headless=True, executable_path=exe,
|
||||||
|
args=['--no-sandbox'])
|
||||||
|
ctx = browser.new_context(viewport={'width': 390, 'height': 844},
|
||||||
|
has_touch=True, is_mobile=True)
|
||||||
|
global page
|
||||||
|
page = ctx.new_page()
|
||||||
|
page.goto(BASE_URL + '/', wait_until='networkidle')
|
||||||
|
wait('.topcard')
|
||||||
|
time.sleep(0.6)
|
||||||
|
|
||||||
|
section = None
|
||||||
|
for _ in range(40):
|
||||||
|
body = page.evaluate(
|
||||||
|
"() => fetch('/api/section').then(r=>r.json())")
|
||||||
|
if body and body.get('items'):
|
||||||
|
section = body
|
||||||
|
break
|
||||||
|
time.sleep(0.5)
|
||||||
|
assert section, 'no section payload'
|
||||||
|
items = list(section['items'])
|
||||||
|
print('section: %d items — recipe %s' % (len(items), json.dumps(
|
||||||
|
section['section']['recipe'], sort_keys=True)))
|
||||||
|
by_id = {i['id']: i for i in items}
|
||||||
|
for lid in (701, 702, 703, 704, 706):
|
||||||
|
assert lid in by_id, 'fixture %s missing from section' % lid
|
||||||
|
assert by_id[lid]['slot'] == 'new', (lid, by_id[lid]['slot'])
|
||||||
|
assert by_id[705]['slot'] == 'return', by_id[705]['slot']
|
||||||
|
|
||||||
|
seen = []
|
||||||
|
round_no = 0
|
||||||
|
while True:
|
||||||
|
remaining = [i['id'] for i in items if i['id'] not in seen]
|
||||||
|
if not remaining:
|
||||||
|
break
|
||||||
|
t = top_state()
|
||||||
|
top_id = None
|
||||||
|
m = re.search(r'/img/(\d+)(?:\?i=\d+)?$', t['photo'] or '')
|
||||||
|
if m:
|
||||||
|
top_id = int(m.group(1))
|
||||||
|
else:
|
||||||
|
for i in items: # zero-photo card: no src to match
|
||||||
|
if i['id'] not in seen and not i.get('img'):
|
||||||
|
top_id = i['id']
|
||||||
|
break
|
||||||
|
assert top_id in by_id, 'cannot identify top card'
|
||||||
|
item = by_id[top_id]
|
||||||
|
seen.append(top_id)
|
||||||
|
round_no += 1
|
||||||
|
n = item.get('n_photos') or 0
|
||||||
|
exp_lead = lead_of(item)
|
||||||
|
exp_src = photo_url(item, exp_lead)
|
||||||
|
print('-- top id=%s [%s] n=%d lead=%d slot=%s --'
|
||||||
|
% (top_id, item['title'][:26], n, exp_lead, item['slot']))
|
||||||
|
assert_item(t['topcards'] == 1,
|
||||||
|
'exactly one .topcard before gestures')
|
||||||
|
if n >= 2:
|
||||||
|
assert_item(t['hasSegs'] and t['segs'] == n,
|
||||||
|
'segments count == n (%d)' % n)
|
||||||
|
assert_item(t['segOn'] == exp_lead,
|
||||||
|
'segment idx == rotation lead (%d)' % exp_lead)
|
||||||
|
assert_item(t['photo'] == exp_src,
|
||||||
|
'photo src == expected lead url')
|
||||||
|
else:
|
||||||
|
assert_item(not t['hasSegs'] and t['segs'] == 0,
|
||||||
|
'no segments for n=%d' % n)
|
||||||
|
if n == 1:
|
||||||
|
assert_item(t['photo'] == photo_url(item, 0),
|
||||||
|
'single photo shows the primary')
|
||||||
|
else:
|
||||||
|
assert_item(t['nophoto'],
|
||||||
|
'zero-photo card shows the placeholder')
|
||||||
|
if item['slot'] == 'return' and item['id'] == 705:
|
||||||
|
assert_item(exp_lead == 1, 'rotation lead: (0+1)%%8 = 1')
|
||||||
|
assert_item(t['band'] and 'SECOND LOOK' in t['band'],
|
||||||
|
'return card carries the SECOND LOOK band '
|
||||||
|
'(with segments)')
|
||||||
|
|
||||||
|
if n >= 2:
|
||||||
|
# --- flip: advance + clamp both ends (no wrap) ----------
|
||||||
|
for i in range(n + 2):
|
||||||
|
tap_third('right')
|
||||||
|
st = top_state()
|
||||||
|
assert_item(st['topcards'] == 1,
|
||||||
|
'tap kept the one-.topcard invariant')
|
||||||
|
want = min(exp_lead + i + 1, n - 1)
|
||||||
|
assert_item(st['segOn'] == want,
|
||||||
|
'right-tap %d -> idx %d (clamp)' % (i + 1,
|
||||||
|
want))
|
||||||
|
for i in range(n + 2):
|
||||||
|
tap_third('left')
|
||||||
|
st = top_state()
|
||||||
|
assert_item(st['topcards'] == 1,
|
||||||
|
'tap kept the one-.topcard invariant')
|
||||||
|
want = max(n - 1 - (i + 1), 0)
|
||||||
|
assert_item(st['segOn'] == want,
|
||||||
|
'left-tap %d -> idx %d (clamp)' % (i + 1,
|
||||||
|
want))
|
||||||
|
st = top_state()
|
||||||
|
assert_item(st['topcards'] == 1
|
||||||
|
and st['stampLike'] in ('0', '')
|
||||||
|
and st['stampNope'] in ('0', '')
|
||||||
|
and st['stampMaybe'] in ('0', ''),
|
||||||
|
'taps never commit: no stamps, no flight')
|
||||||
|
# --- viewer: open on current photo, nav + clamp, zoom,
|
||||||
|
# pull-close restores the same card+photo ------------
|
||||||
|
want_idx = 1
|
||||||
|
while st['segOn'] > want_idx:
|
||||||
|
tap_third('left')
|
||||||
|
st = top_state()
|
||||||
|
if st['segOn'] < want_idx:
|
||||||
|
tap_third('right')
|
||||||
|
st = top_state()
|
||||||
|
before_src = st['photo']
|
||||||
|
tap_third('center')
|
||||||
|
assert_item(viewer_on(), 'center tap opens the viewer')
|
||||||
|
vsegs = page.evaluate(
|
||||||
|
"() => document.getElementById('vsegs')"
|
||||||
|
".querySelectorAll('i').length")
|
||||||
|
assert_item(vsegs == n, 'viewer segments == n (%d)' % n)
|
||||||
|
assert_item(vimg_src() == before_src,
|
||||||
|
'viewer opens on the current photo')
|
||||||
|
vp = page.locator('#vphoto')
|
||||||
|
vb = vp.bounding_box()
|
||||||
|
al, ar = (vb['x'] + vb['width'] * 0.75,
|
||||||
|
vb['x'] + vb['width'] * 0.15)
|
||||||
|
ay = vb['y'] + vb['height'] * 0.5
|
||||||
|
page.mouse.move(al, ay)
|
||||||
|
page.mouse.down()
|
||||||
|
page.mouse.move(ar, ay, steps=6)
|
||||||
|
page.mouse.up()
|
||||||
|
time.sleep(0.1)
|
||||||
|
land = min(want_idx + 1, n - 1)
|
||||||
|
assert_item(vimg_src() == photo_url(item, land),
|
||||||
|
'viewer swipe-left advances + clamps')
|
||||||
|
page.mouse.move(ar, ay)
|
||||||
|
page.mouse.down()
|
||||||
|
page.mouse.move(al, ay, steps=6)
|
||||||
|
page.mouse.up()
|
||||||
|
time.sleep(0.1)
|
||||||
|
back = max(land - 1, 0)
|
||||||
|
assert_item(vimg_src() == photo_url(item, back),
|
||||||
|
'viewer swipe-right steps back (clamped)')
|
||||||
|
page.mouse.dblclick(vb['x'] + vb['width'] * 0.5,
|
||||||
|
vb['y'] + vb['height'] * 0.5)
|
||||||
|
time.sleep(0.1)
|
||||||
|
assert_item('scale(2.5)' in vimg_transform(),
|
||||||
|
'double-tap zooms to 2.5x')
|
||||||
|
page.mouse.dblclick(vb['x'] + vb['width'] * 0.5,
|
||||||
|
vb['y'] + vb['height'] * 0.5)
|
||||||
|
time.sleep(0.1)
|
||||||
|
assert_item('scale(2.5)' not in vimg_transform(),
|
||||||
|
'double-tap again returns to 1x')
|
||||||
|
# pinch via synthetic pointer events (real handler path)
|
||||||
|
page.evaluate("""() => {
|
||||||
|
const v=document.getElementById('vphoto');
|
||||||
|
const r=v.getBoundingClientRect();
|
||||||
|
const mk=(t,id,x,y)=>{ v.dispatchEvent(new PointerEvent(t,
|
||||||
|
{pointerId:id, clientX:x, clientY:y, bubbles:true})); };
|
||||||
|
const x=r.width/2, y=r.height/2;
|
||||||
|
mk('pointerdown',10,x-25,y); mk('pointerdown',11,x+25,y);
|
||||||
|
for(let s=0;s<6;s++){ mk('pointermove',10,x-25-s*22,y);
|
||||||
|
mk('pointermove',11,x+25+s*22,y); }
|
||||||
|
mk('pointerup',10,x-25-6*22,y);
|
||||||
|
mk('pointerup',11,x+25+6*22,y);
|
||||||
|
}""")
|
||||||
|
assert_item('scale(4)' in vimg_transform(),
|
||||||
|
'pinch-zoom clamps at 4x')
|
||||||
|
page.mouse.dblclick(vb['x'] + vb['width'] * 0.5,
|
||||||
|
vb['y'] + vb['height'] * 0.5)
|
||||||
|
time.sleep(0.1) # back to 1x for the pull gesture
|
||||||
|
page.mouse.move(vb['x'] + vb['width'] * 0.5,
|
||||||
|
vb['y'] + vb['height'] * 0.35)
|
||||||
|
page.mouse.down()
|
||||||
|
page.mouse.move(vb['x'] + vb['width'] * 0.5,
|
||||||
|
vb['y'] + vb['height'] * 0.35 + 170, steps=8)
|
||||||
|
page.mouse.up()
|
||||||
|
time.sleep(0.2)
|
||||||
|
assert_item(not viewer_on(),
|
||||||
|
'pull-down (>110px) closes the viewer')
|
||||||
|
st = top_state()
|
||||||
|
assert_item(st['segOn'] == want_idx
|
||||||
|
and st['photo'] == before_src,
|
||||||
|
'close returns the same card + photo position')
|
||||||
|
else:
|
||||||
|
if n == 1:
|
||||||
|
tap_third('center')
|
||||||
|
assert_item(viewer_on(),
|
||||||
|
'center tap opens the viewer (single photo)')
|
||||||
|
page.locator('#vclose').click()
|
||||||
|
time.sleep(0.15)
|
||||||
|
if n == 0:
|
||||||
|
tap_third('center')
|
||||||
|
assert_item(not viewer_on(), 'zero-photo: no viewer')
|
||||||
|
|
||||||
|
# --- ⓘ beats the zones --------------------------------------
|
||||||
|
page.locator('.topcard .ibtn').click()
|
||||||
|
time.sleep(0.15)
|
||||||
|
assert_item(page.evaluate(
|
||||||
|
"() => document.getElementById('detail')"
|
||||||
|
".classList.contains('on')"),
|
||||||
|
'ⓘ opens the dossier (zones do not fire)')
|
||||||
|
pre = top_state()
|
||||||
|
page.locator('#dclose').click()
|
||||||
|
time.sleep(0.2)
|
||||||
|
post = top_state()
|
||||||
|
assert_item(pre['photo'] == post['photo']
|
||||||
|
and pre['segOn'] == post['segOn'],
|
||||||
|
'dossier close leaves the photo position intact')
|
||||||
|
if n >= 1:
|
||||||
|
# dossier hero -> viewer at photo 0 (dossier stays open)
|
||||||
|
page.locator('.topcard .ibtn').click()
|
||||||
|
time.sleep(0.15)
|
||||||
|
hb = page.locator('.dhero').bounding_box()
|
||||||
|
page.mouse.click(hb['x'] + hb['width'] / 2,
|
||||||
|
hb['y'] + hb['height'] / 2)
|
||||||
|
time.sleep(0.15)
|
||||||
|
assert_item(viewer_on() and page.evaluate(
|
||||||
|
"() => document.getElementById('detail')"
|
||||||
|
".classList.contains('on')"),
|
||||||
|
'dossier hero tap opens the viewer over the dossier')
|
||||||
|
assert_item(vimg_src() == photo_url(item, 0),
|
||||||
|
'dossier viewer starts at photo 0')
|
||||||
|
page.locator('#vclose').click()
|
||||||
|
time.sleep(0.15)
|
||||||
|
assert_item(not viewer_on() and page.evaluate(
|
||||||
|
"() => document.getElementById('detail')"
|
||||||
|
".classList.contains('on')"),
|
||||||
|
'viewer ✕ returns to the still-open dossier')
|
||||||
|
page.locator('#dclose').click()
|
||||||
|
time.sleep(0.15)
|
||||||
|
|
||||||
|
# single commit per round (fast chain runs after the report)
|
||||||
|
page.locator('#btnNope').click()
|
||||||
|
time.sleep(0.07)
|
||||||
|
|
||||||
|
# --- P3 regression: report -> undo x3 -> fast chain -> forge -----
|
||||||
|
wait('#report.on', 15000)
|
||||||
|
print('report visible: %s' % page.locator('#repCounts').inner_text())
|
||||||
|
assert_item('Section complete' in page.locator('#report').inner_text(),
|
||||||
|
'report screen after the full section')
|
||||||
|
for _ in range(4):
|
||||||
|
page.locator('#btnUndo').click()
|
||||||
|
time.sleep(0.08)
|
||||||
|
assert_item(page.evaluate(
|
||||||
|
"() => document.querySelectorAll('.topcard').length") == 1,
|
||||||
|
'undo x4 from the report re-opens the deck (one .topcard)')
|
||||||
|
for k in range(3):
|
||||||
|
b = card_box()
|
||||||
|
y = b['y'] + b['h'] * 0.6
|
||||||
|
page.mouse.move(b['x'] + b['w'] * 0.5, y)
|
||||||
|
page.mouse.down()
|
||||||
|
page.mouse.move(b['x'] + b['w'] * 0.5 + 320, y, steps=5)
|
||||||
|
page.mouse.up()
|
||||||
|
if k < 2:
|
||||||
|
time.sleep(0.07)
|
||||||
|
tc = page.evaluate(
|
||||||
|
"() => document.querySelectorAll('.topcard').length")
|
||||||
|
assert tc == 1, 'fast chain: topcard=%s (must be 1)' % tc
|
||||||
|
assert_item(True, '3 like-commits @70ms: one .topcard, no lockout')
|
||||||
|
page.locator('#btnNope').click() # finish the 4th restored card
|
||||||
|
time.sleep(0.1)
|
||||||
|
wait('#report.on', 15000)
|
||||||
|
page.locator('#btnForge').click()
|
||||||
|
wait('.topcard', 15000)
|
||||||
|
time.sleep(0.4)
|
||||||
|
sect = page.evaluate(
|
||||||
|
"() => document.getElementById('sect').textContent.trim()")
|
||||||
|
assert_item(re.match(r'^\d+/25$', sect) is not None,
|
||||||
|
'forge hydrates a fresh section (progress=%s)' % sect)
|
||||||
|
# --- List + Trends tabs render with P3 elements intact ----------
|
||||||
|
page.locator('.tab[data-tab="list"]').click()
|
||||||
|
wait('.row')
|
||||||
|
tanks = page.locator('.row .tank').count()
|
||||||
|
rows = page.locator('.row').count()
|
||||||
|
assert_item(tanks > 0 and rows > 0,
|
||||||
|
'list: %d rows, %d tank gauges (P3 intact)'
|
||||||
|
% (rows, tanks))
|
||||||
|
page.locator('.tab[data-tab="trends"]').click()
|
||||||
|
wait('#tLead')
|
||||||
|
assert_item((page.locator('#tLead').text_content() or '').strip()
|
||||||
|
!= '', 'trends tab renders')
|
||||||
|
print('WALK COMPLETE — all B1 FE checks green')
|
||||||
|
browser.close()
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
if 'setup' in sys.argv or not os.path.exists(WALK_DB):
|
||||||
|
if not os.path.exists(PRISTINE):
|
||||||
|
print('pristine copy DB missing: %s' % PRISTINE)
|
||||||
|
return 1
|
||||||
|
setup_db()
|
||||||
|
stage_img_files()
|
||||||
|
print('fixtures staged in %s' % IMG_DIR)
|
||||||
|
if not need_server():
|
||||||
|
print('NO SERVER on %s — start it (see module docstring).' % BASE_URL)
|
||||||
|
return 1
|
||||||
|
walk()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
876
app/fe_walk_v11.py
Normal file
876
app/fe_walk_v11.py
Normal file
@@ -0,0 +1,876 @@
|
|||||||
|
"""v1.1 FE walk — headless-chromium proof of every v1.1 item on the real page.
|
||||||
|
|
||||||
|
Self-contained: stages its own temp DBs (never the live file), boots the
|
||||||
|
scratch server on 127.0.0.1:9455 (never 9453), runs the walks, kills the
|
||||||
|
server. Mock forge only (OPENROUTER_API_KEY is not consulted — the server
|
||||||
|
is started with the key unset).
|
||||||
|
|
||||||
|
cd /var/home/admin/dev/bike-tinder-v11/build
|
||||||
|
.venv/bin/python -m app.fe_walk_v11 setup # (auto-runs at walk start)
|
||||||
|
.venv/bin/python -m app.fe_walk_v11 # full walk
|
||||||
|
|
||||||
|
Phases:
|
||||||
|
WALK1 (rich: pristine catalog + synthetic multi-photo fixtures 701-706,
|
||||||
|
705 a liked return) — items 1, 2, 3, 4, 7, 10, B1 §11 regressions
|
||||||
|
(flip clamps / one .topcard / no-lock swipe storm), full P3 loop
|
||||||
|
(compose -> strike all -> report -> forge), item 6 non-dry.
|
||||||
|
WALK2 (returns-only: every active pre-swiped maybe) — item 5 counter
|
||||||
|
(0/N -> 1/N -> undo -> sweep -> 25/25 hot -> report counts),
|
||||||
|
item 6 dry caption + dry forge status.
|
||||||
|
WALK3 (0/0/0: returns one-shot-burned) — item 8 loud empty2, nightly-
|
||||||
|
import recovery (a fresh listing re-lights the deck).
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
import sqlite3
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
ARENA = '/var/home/admin/dev/bike-tinder-v11'
|
||||||
|
REPO = os.path.join(ARENA, 'build')
|
||||||
|
PRISTINE = os.path.join(ARENA, 'scratch-v11-pristine.sqlite3')
|
||||||
|
WALK1 = os.path.join(ARENA, 'scratch-v11-walk1.sqlite3')
|
||||||
|
WALK2 = os.path.join(ARENA, 'scratch-v11-walk2.sqlite3')
|
||||||
|
WALK3 = os.path.join(ARENA, 'scratch-v11-walk3.sqlite3')
|
||||||
|
IMG = os.path.join(ARENA, 'img-v11')
|
||||||
|
MEDIA = os.path.join(ARENA, 'media-sample-v11')
|
||||||
|
SHOTS = os.path.join(ARENA, 'shots-v11')
|
||||||
|
BASE_URL = 'http://127.0.0.1:9455'
|
||||||
|
PORT = 9455
|
||||||
|
|
||||||
|
CDN = ('https://media.kijiji.ca/api/v1/ca-prod-dealer-ads/images/x/%s'
|
||||||
|
'?rule=kijijica-640-webp')
|
||||||
|
|
||||||
|
FIX = {
|
||||||
|
701: (8, 'gallery-8'),
|
||||||
|
702: (3, 'gallery-3'),
|
||||||
|
703: (2, 'gallery-2'),
|
||||||
|
704: (1, 'single-photo'),
|
||||||
|
705: (8, 'return-rotation'),
|
||||||
|
706: (0, 'zero-photo'),
|
||||||
|
}
|
||||||
|
SCORE_BASE = 2000
|
||||||
|
|
||||||
|
server_proc = None
|
||||||
|
page = None
|
||||||
|
|
||||||
|
|
||||||
|
# --- staging ---------------------------------------------------------------
|
||||||
|
|
||||||
|
def stage_img_files():
|
||||||
|
os.makedirs(IMG, exist_ok=True)
|
||||||
|
names = sorted(m for m in os.listdir(MEDIA) if m.endswith('.webp'))
|
||||||
|
assert len(names) == 12, names
|
||||||
|
k = 0
|
||||||
|
for lid in sorted(FIX):
|
||||||
|
n = FIX[lid][0]
|
||||||
|
if n == 0:
|
||||||
|
continue
|
||||||
|
if n == 1:
|
||||||
|
shutil.copy2(os.path.join(MEDIA, names[k % len(names)]),
|
||||||
|
os.path.join(IMG, '%d.webp' % lid))
|
||||||
|
k += 1
|
||||||
|
continue
|
||||||
|
for i in range(n):
|
||||||
|
shutil.copy2(os.path.join(MEDIA, names[k % len(names)]),
|
||||||
|
os.path.join(IMG, '%d_%d.webp' % (lid, i)))
|
||||||
|
k += 1
|
||||||
|
|
||||||
|
|
||||||
|
def seeds():
|
||||||
|
"""Fixture rows + return candidates on a temp copy; returns the maybe-pair."""
|
||||||
|
from app import db as dbmod
|
||||||
|
conn = sqlite3.connect(WALK1)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
try:
|
||||||
|
dbmod.init_db(conn) # guarded B1/P3 migrations
|
||||||
|
for lid in sorted(FIX):
|
||||||
|
n = FIX[lid][0]
|
||||||
|
imgs = [CDN % ('x%d-%d' % (lid, i)) for i in range(n)]
|
||||||
|
img = imgs[0] if imgs else None
|
||||||
|
score = SCORE_BASE + (750 - lid)
|
||||||
|
conn.execute("DELETE FROM listings WHERE id=?", (lid,))
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO listings (id, region, src_id, title, url, status,"
|
||||||
|
" img_url, imgs, score, first_seen, desc) VALUES"
|
||||||
|
" (?, 'ns', ?, ?, ?, 'active', ?, ?, ?,"
|
||||||
|
" '2026-11-11T00:00:00Z', ?)",
|
||||||
|
(lid, 'v11fix%d' % lid, 'V1.1 test %s' % FIX[lid][1],
|
||||||
|
'https://www.kijiji.ca/v-motorcycles/v11/%d' % lid,
|
||||||
|
img, json.dumps(imgs) if imgs else None, score,
|
||||||
|
'V1.1 fixture'))
|
||||||
|
conn.execute(
|
||||||
|
"INSERT OR REPLACE INTO swipes (listing_id, action, created_at,"
|
||||||
|
" last_photo_idx) VALUES (705, 'like', '2026-11-08T00:00:00Z', 0)")
|
||||||
|
# fresh fixtures: round-1 events keep them OUT of the wildcard pool
|
||||||
|
for lid in FIX:
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO swipe_events (listing_id, action, round,"
|
||||||
|
" section_id) VALUES (?, ?, 1, NULL)",
|
||||||
|
(lid, 'like' if lid == 705 else 'maybe'))
|
||||||
|
# second reserved maybe-return: the highest-scored existing swiped
|
||||||
|
# active swings to maybe so 705 keeps its reserved return slot
|
||||||
|
x = conn.execute(
|
||||||
|
"SELECT s.listing_id FROM swipes s"
|
||||||
|
" JOIN listings l ON l.id = s.listing_id"
|
||||||
|
" WHERE l.status='active' ORDER BY l.score DESC LIMIT 1"
|
||||||
|
).fetchone()['listing_id']
|
||||||
|
conn.execute("UPDATE swipes SET action='maybe' WHERE listing_id=?", (x,))
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE swipe_events SET action='maybe' WHERE listing_id=?"
|
||||||
|
" AND round=1", (x,))
|
||||||
|
conn.execute("UPDATE sections SET status='closed'"
|
||||||
|
" WHERE status!='closed'")
|
||||||
|
conn.commit()
|
||||||
|
return x
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def setup():
|
||||||
|
assert os.path.exists(PRISTINE), 'pristine copy missing'
|
||||||
|
os.makedirs(SHOTS, exist_ok=True)
|
||||||
|
stage_img_files()
|
||||||
|
shutil.copy2(PRISTINE, WALK1)
|
||||||
|
seeds()
|
||||||
|
shutil.copy2(PRISTINE, WALK2)
|
||||||
|
conn = sqlite3.connect(WALK2)
|
||||||
|
try:
|
||||||
|
conn.execute('DELETE FROM section_items')
|
||||||
|
conn.execute("UPDATE sections SET status='closed'"
|
||||||
|
" WHERE status!='closed'")
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO swipes (listing_id, action, created_at)
|
||||||
|
SELECT id, 'maybe', '2026-11-01T00:00:00Z' FROM listings
|
||||||
|
WHERE status='active'
|
||||||
|
AND id NOT IN (SELECT listing_id FROM swipes)""")
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO swipe_events (listing_id, action, round, section_id)
|
||||||
|
SELECT id, 'maybe', 1, NULL FROM listings
|
||||||
|
WHERE status='active'
|
||||||
|
AND id NOT IN (SELECT listing_id FROM swipe_events)""")
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
print('setup: walk DBs staged (%s, %s)' % (WALK1, WALK2))
|
||||||
|
|
||||||
|
|
||||||
|
# --- server ----------------------------------------------------------------
|
||||||
|
|
||||||
|
def start_server(db_path):
|
||||||
|
global server_proc
|
||||||
|
env = dict(os.environ)
|
||||||
|
env.pop('OPENROUTER_API_KEY', None) # mock forge pass only
|
||||||
|
server_proc = subprocess.Popen(
|
||||||
|
[os.path.join(REPO, '.venv/bin/python'), '-m', 'app.serve',
|
||||||
|
'--db', db_path, '--img-dir', IMG, '--host', '127.0.0.1',
|
||||||
|
'--port', str(PORT), '--state-dir',
|
||||||
|
os.path.join(ARENA, 'state-v11')],
|
||||||
|
cwd=REPO, env=env,
|
||||||
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
for _ in range(60):
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(BASE_URL + '/api/health',
|
||||||
|
timeout=2) as r:
|
||||||
|
if r.status == 200:
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
time.sleep(0.25)
|
||||||
|
raise RuntimeError('scratch server did not come up')
|
||||||
|
|
||||||
|
|
||||||
|
def stop_server():
|
||||||
|
global server_proc
|
||||||
|
if server_proc:
|
||||||
|
server_proc.terminate()
|
||||||
|
try:
|
||||||
|
server_proc.wait(timeout=5)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
server_proc.kill()
|
||||||
|
server_proc = None
|
||||||
|
|
||||||
|
|
||||||
|
# --- walk helpers ----------------------------------------------------------
|
||||||
|
|
||||||
|
CHECKS = [0, 0]
|
||||||
|
|
||||||
|
|
||||||
|
def wait(selector, timeout=10000):
|
||||||
|
page.wait_for_selector(selector, timeout=timeout)
|
||||||
|
|
||||||
|
|
||||||
|
def ok(msg):
|
||||||
|
CHECKS[0] += 1
|
||||||
|
print('ok ' + msg)
|
||||||
|
|
||||||
|
|
||||||
|
def bad(msg):
|
||||||
|
CHECKS[1] += 1
|
||||||
|
print('FAIL ' + msg)
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'fail-%s.png' % int(time.time())))
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def check(cond, msg):
|
||||||
|
if cond:
|
||||||
|
ok(msg)
|
||||||
|
else:
|
||||||
|
bad(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def top_state():
|
||||||
|
return page.evaluate("""() => {
|
||||||
|
const t=document.querySelector('.topcard');
|
||||||
|
const r={topcards:document.querySelectorAll('.topcard').length,
|
||||||
|
photo:null, segOn:-1, segs:0, band:null, bandClass:null};
|
||||||
|
if(!t) return r;
|
||||||
|
const s=t.querySelector('.segments');
|
||||||
|
if(s){ r.segs=s.children.length;
|
||||||
|
[...s.children].forEach((x,k)=>{ if(x.classList.contains('on'))
|
||||||
|
r.segOn=k; }); }
|
||||||
|
const p=t.querySelector('.phwrap img.photo');
|
||||||
|
if(p) r.photo=p.getAttribute('src');
|
||||||
|
const b=t.querySelector('.band');
|
||||||
|
if(b){ r.band=b.textContent; r.bandClass=b.className; }
|
||||||
|
return r; }""")
|
||||||
|
|
||||||
|
|
||||||
|
def top_id():
|
||||||
|
t = top_state()
|
||||||
|
if not t['photo']:
|
||||||
|
return None
|
||||||
|
m = re.search(r'/img/(\d+)(?:\?i=\d+)?$', t['photo'] or '')
|
||||||
|
return int(m.group(1)) if m else None
|
||||||
|
|
||||||
|
|
||||||
|
def sect_pill():
|
||||||
|
return page.evaluate("() => ({txt:document.getElementById('sect').textContent,"
|
||||||
|
" hot:document.getElementById('sect').classList.contains('hot')})")
|
||||||
|
|
||||||
|
|
||||||
|
def pile_len():
|
||||||
|
return page.evaluate("() => document.querySelectorAll('.topcard').length +"
|
||||||
|
" + [...document.querySelectorAll('#stage .card')].filter("
|
||||||
|
" e=>!e.classList.contains('topcard')).length")
|
||||||
|
|
||||||
|
|
||||||
|
def key(k):
|
||||||
|
page.keyboard.press(k)
|
||||||
|
time.sleep(0.08)
|
||||||
|
|
||||||
|
|
||||||
|
def api(path):
|
||||||
|
with urllib.request.urlopen(BASE_URL + path, timeout=10) as r:
|
||||||
|
return json.loads(r.read())
|
||||||
|
|
||||||
|
|
||||||
|
def api_post(path, body):
|
||||||
|
req = urllib.request.Request(BASE_URL + path,
|
||||||
|
data=json.dumps(body).encode(),
|
||||||
|
headers={'Content-Type': 'application/json'})
|
||||||
|
with urllib.request.urlopen(req, timeout=30) as r:
|
||||||
|
return json.loads(r.read())
|
||||||
|
|
||||||
|
|
||||||
|
def section_body():
|
||||||
|
return api('/api/section')
|
||||||
|
|
||||||
|
|
||||||
|
def flick(ms=350):
|
||||||
|
time.sleep(ms / 1000.0)
|
||||||
|
|
||||||
|
|
||||||
|
def tap_center():
|
||||||
|
b = page.evaluate("""() => { const el=document.querySelector('.topcard');
|
||||||
|
const r=el.getBoundingClientRect();
|
||||||
|
return {x:r.x+r.width/2,y:r.y+r.height*0.72}; }""")
|
||||||
|
page.mouse.click(b['x'], b['y'])
|
||||||
|
time.sleep(0.08)
|
||||||
|
|
||||||
|
|
||||||
|
def tap_side(side):
|
||||||
|
b = page.evaluate("""() => { const el=document.querySelector('.topcard');
|
||||||
|
const r=el.getBoundingClientRect();
|
||||||
|
return {x:r.x, w:r.width, y:r.y+r.height*0.72}; }""")
|
||||||
|
x = b['x'] + (0.16 if side == 'left' else 0.84) * b['w']
|
||||||
|
page.mouse.click(x, b['y'])
|
||||||
|
time.sleep(0.08)
|
||||||
|
|
||||||
|
|
||||||
|
def taps(clicks, gap=40):
|
||||||
|
# real commit storm through the like button: the no-lock law must
|
||||||
|
# survive button-speed swipes
|
||||||
|
for _ in range(clicks):
|
||||||
|
page.evaluate("() => document.getElementById('btnLike').click()")
|
||||||
|
time.sleep(gap / 1000.0)
|
||||||
|
|
||||||
|
|
||||||
|
def dossier_on():
|
||||||
|
return page.evaluate(
|
||||||
|
"() => document.getElementById('detail').classList.contains('on')")
|
||||||
|
|
||||||
|
|
||||||
|
def viewer_on():
|
||||||
|
return page.evaluate(
|
||||||
|
"() => document.getElementById('viewer').classList.contains('on')")
|
||||||
|
|
||||||
|
|
||||||
|
def dhero_click():
|
||||||
|
b = page.evaluate("""() => { const el=document.getElementById('dhero')||
|
||||||
|
document.querySelector('.dhero');
|
||||||
|
const r=el.getBoundingClientRect();
|
||||||
|
return {x:r.x+r.width/2,y:r.y+r.height/2}; }""")
|
||||||
|
page.mouse.click(b['x'], b['y'])
|
||||||
|
time.sleep(0.15)
|
||||||
|
|
||||||
|
|
||||||
|
# --- WALK1: rich deck ------------------------------------------------------
|
||||||
|
|
||||||
|
def walk_rich():
|
||||||
|
check(page.url.startswith(BASE_URL), 'page served from the app root')
|
||||||
|
wait('.topcard')
|
||||||
|
flick(800)
|
||||||
|
body = section_body()
|
||||||
|
check(len(body['items']) == 25, 'rich compose serves 25 items')
|
||||||
|
check(body['recipe']['new'] == 21 and body['recipe']['return'] == 2
|
||||||
|
and body['recipe']['wildcard'] == 2,
|
||||||
|
'recipe 21/2/2 on the rich pool (the sign-off shape)')
|
||||||
|
by_id = {i['id']: i for i in body['items']}
|
||||||
|
for lid in (701, 702, 703, 704, 706):
|
||||||
|
check(lid in by_id and by_id[lid]['slot'] == 'new',
|
||||||
|
'fixture %s sits in the fresh slots' % lid)
|
||||||
|
check(by_id[705]['slot'] == 'return', '705 composes into a return slot')
|
||||||
|
|
||||||
|
t = top_state()
|
||||||
|
check(t['topcards'] == 1, 'exactly one .topcard on load')
|
||||||
|
check(top_id() == 701, 'top card is the highest-scored fixture (701)')
|
||||||
|
|
||||||
|
# item 3: computed styles on the top card (fit + backdrop)
|
||||||
|
css = page.evaluate("""() => { const c=document.querySelector('.topcard');
|
||||||
|
const p=c.querySelector('.phwrap img.photo');
|
||||||
|
const b=c.querySelector('.phwrap .phblur');
|
||||||
|
return {fit:getComputedStyle(p).objectFit,
|
||||||
|
blur:!!b, bg:b?b.style.backgroundImage:null,
|
||||||
|
src:p.getAttribute('src')}; }""")
|
||||||
|
check(css['fit'] == 'contain', 'item 3: card photo object-fit == contain')
|
||||||
|
check(bool(css['blur']) and css['src'] in (css['bg'] or ''),
|
||||||
|
'item 3: card backdrop exists and mirrors the photo URL')
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'item3-card-fit.png'))
|
||||||
|
|
||||||
|
# --- item 5+1: strikes through the real key map ------------------------
|
||||||
|
check(sect_pill()['txt'] == '0/25', 'fresh rich section starts 0/25')
|
||||||
|
key('ArrowUp')
|
||||||
|
check(sect_pill()['txt'] == '1/25', 'ArrowUp (maybe) -> 1/25')
|
||||||
|
key('ArrowDown')
|
||||||
|
check(sect_pill()['txt'] == '0/25', 'ArrowDown (undo) -> 0/25')
|
||||||
|
key('ArrowUp')
|
||||||
|
check(sect_pill()['txt'] == '1/25', 're-decide -> 1/25')
|
||||||
|
key('Backspace')
|
||||||
|
check(sect_pill()['txt'] == '0/25', 'Backspace is the undo alias')
|
||||||
|
pill_before = sect_pill()['txt']
|
||||||
|
# scroll prevention spy
|
||||||
|
page.evaluate("""() => { window.__prev=[]; document.addEventListener(
|
||||||
|
'keydown',e=>window.__prev.push([e.key,e.defaultPrevented])); }""")
|
||||||
|
key('ArrowLeft')
|
||||||
|
key('ArrowRight')
|
||||||
|
prevented = page.evaluate("""() => window.__prev.filter(
|
||||||
|
p=>p[0].startsWith('Arrow')).map(p=>p[1])""")
|
||||||
|
check(all(prevented), 'item 1: arrows defaultPrevented (no page scroll)')
|
||||||
|
pn = sect_pill()['txt']
|
||||||
|
check(int(pn.split('/')[0]) == int(pill_before.split('/')[0]) + 2,
|
||||||
|
'ArrowLeft/ArrowRight commit through the normal path (%s -> %s)'
|
||||||
|
% (pill_before, pn))
|
||||||
|
|
||||||
|
# --- item 1: text-input guard (list search) ---------------------------
|
||||||
|
page.evaluate("() => document.querySelector('.tab[data-tab=list]').click()")
|
||||||
|
wait('#lsearch')
|
||||||
|
page.evaluate("""() => { const s=document.getElementById('lsearch');
|
||||||
|
s.focus(); }""")
|
||||||
|
page.keyboard.type('abc')
|
||||||
|
before_search = page.evaluate(
|
||||||
|
"() => document.getElementById('lsearch').value")
|
||||||
|
check(before_search == 'abc', 'search input accepts typing (focus guard on)')
|
||||||
|
for kk in ('ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'):
|
||||||
|
page.keyboard.press(kk)
|
||||||
|
flick(60)
|
||||||
|
check(page.evaluate(
|
||||||
|
"() => document.getElementById('lsearch').value") == 'abc',
|
||||||
|
'arrows inside the search input never swipe (text + value unchanged)')
|
||||||
|
check(sect_pill()['txt'] == pn,
|
||||||
|
'input-focused arrows leave the section counter untouched')
|
||||||
|
page.keyboard.press('Backspace')
|
||||||
|
check(page.evaluate(
|
||||||
|
"() => document.getElementById('lsearch').value") == 'ab',
|
||||||
|
'Backspace edits text while an input owns focus (no undo fired)')
|
||||||
|
page.evaluate("() => document.querySelector('.tab[data-tab=deck]').click()")
|
||||||
|
flick(300)
|
||||||
|
|
||||||
|
# --- item 2: center tap -> dossier -> hero -> viewer; Esc/Es ----------
|
||||||
|
tid_before = top_id()
|
||||||
|
src_before = top_state()['photo']
|
||||||
|
pill_pre_tap = sect_pill()['txt']
|
||||||
|
tap_center()
|
||||||
|
check(sect_pill()['txt'] == pill_pre_tap,
|
||||||
|
'item 2: center tap commits nothing')
|
||||||
|
check(dossier_on(), 'item 2: center tap opens the dossier')
|
||||||
|
check(not viewer_on(), 'item 2: no viewer on center tap')
|
||||||
|
check(bool(page.evaluate(
|
||||||
|
"() => document.getElementById('dtitle').textContent")),
|
||||||
|
'dossier carries the card title')
|
||||||
|
dhero_click()
|
||||||
|
check(viewer_on(), 'dossier hero tap opens the viewer (photo 0)')
|
||||||
|
vidx = page.evaluate("""() => { const im=document.getElementById('vimg');
|
||||||
|
return {src:im.getAttribute('src'),
|
||||||
|
fot:getComputedStyle(im).objectFit}; }""")
|
||||||
|
check(vidx['fot'] == 'contain', 'item 3: viewer vimg stays contain (no diff)')
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'item3-viewer.png'))
|
||||||
|
# arrows are inert while the viewer is open
|
||||||
|
pill_now = sect_pill()['txt']
|
||||||
|
for kk in ('ArrowLeft', 'ArrowRight', 'ArrowUp'):
|
||||||
|
page.keyboard.press(kk)
|
||||||
|
check(sect_pill()['txt'] == pill_now,
|
||||||
|
'arrows are inert while the viewer is open')
|
||||||
|
key('Escape')
|
||||||
|
check(not viewer_on() and dossier_on(),
|
||||||
|
'Esc closes the viewer only (dossier stays open)')
|
||||||
|
key('Escape')
|
||||||
|
check(not dossier_on() and not viewer_on(),
|
||||||
|
'second Esc closes the dossier to the deck')
|
||||||
|
check(top_id() == tid_before and top_state()['photo'] == src_before,
|
||||||
|
'walk-back leaves the same card on the same photo')
|
||||||
|
|
||||||
|
# item 2 A5: list row -> dossier -> viewer -> Esc -> dossier -> Esc -> list
|
||||||
|
page.evaluate("() => document.querySelector('.tab[data-tab=list]').click()")
|
||||||
|
wait('#lrows .row')
|
||||||
|
flick(400)
|
||||||
|
thumb_fit = page.evaluate("""() => { const t=document.querySelector(
|
||||||
|
'.thumb'); return t?getComputedStyle(t).objectFit:null; }""")
|
||||||
|
check(thumb_fit == 'cover', 'item 3: list thumbs stay cover (no diff)')
|
||||||
|
page.evaluate("""() => document.querySelector('#lrows .row').click()""")
|
||||||
|
flick(400)
|
||||||
|
check(dossier_on(), 'list row tap opens the dossier')
|
||||||
|
dhero_click()
|
||||||
|
check(viewer_on(), 'list dossier hero opens the viewer')
|
||||||
|
key('Escape')
|
||||||
|
check(not viewer_on() and dossier_on(), 'Esc: viewer -> dossier (list)')
|
||||||
|
key('Escape')
|
||||||
|
check(not dossier_on()
|
||||||
|
and page.evaluate("() => document.getElementById('listScreen')"
|
||||||
|
".classList.contains('on')"),
|
||||||
|
'second Esc returns to the list screen')
|
||||||
|
page.evaluate("() => document.querySelector('.tab[data-tab=deck]').click()")
|
||||||
|
flick(300)
|
||||||
|
|
||||||
|
# arrows inert while the dossier is open
|
||||||
|
tap_center()
|
||||||
|
check(dossier_on(), 'dossier open for the key-inertness check')
|
||||||
|
before_pill = sect_pill()['txt']
|
||||||
|
for kk in ('ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'):
|
||||||
|
page.keyboard.press(kk)
|
||||||
|
flick(40)
|
||||||
|
check(sect_pill()['txt'] == before_pill,
|
||||||
|
'arrow keys are inert while the dossier is open')
|
||||||
|
|
||||||
|
# item 10+4: dossier band + minimal rubric on the fresh card
|
||||||
|
db = page.evaluate("""() => { const b=document.getElementById('dband');
|
||||||
|
if(b.style.display==='none') return {off:true};
|
||||||
|
return {off:false, cls:b.className}; }""")
|
||||||
|
check(bool(db.get('off')), 'dossier on a fresh card shows no band')
|
||||||
|
drank = page.evaluate("""() => { const d=document.getElementById('drank');
|
||||||
|
return {shown:d.style.display!=='none',
|
||||||
|
pts:document.getElementById('dpts').textContent,
|
||||||
|
lines:document.getElementById('dlines').innerHTML,
|
||||||
|
foot:document.getElementById('dfoot').textContent}; }""")
|
||||||
|
check(re.search(r'^\d+%$', drank['pts']),
|
||||||
|
'item 4: rank block shows gauge + NN%% (%s)' % drank['pts'])
|
||||||
|
check(not drank['lines'] and not drank['foot'],
|
||||||
|
'item 4: breakdown lines + footer hidden with rubric_debug off')
|
||||||
|
dc = page.evaluate("""() => { const p=document.getElementById('dphoto');
|
||||||
|
const b=document.getElementById('dblur');
|
||||||
|
return {fit:getComputedStyle(p).objectFit,
|
||||||
|
bg:b.style.backgroundImage}; }""")
|
||||||
|
check(dc['fit'] == 'contain' and bool(dc['bg']),
|
||||||
|
'item 3: dossier hero fits contain over its backdrop')
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'item3-dossier.png'))
|
||||||
|
key('Escape')
|
||||||
|
|
||||||
|
# item 2: side taps still flip (clamped), never open a sheet
|
||||||
|
before_photo = top_state()['photo']
|
||||||
|
tap_side('left')
|
||||||
|
check(top_state()['photo'] == before_photo, 'left tap clamps at photo 0')
|
||||||
|
check(not dossier_on() and not viewer_on(),
|
||||||
|
'side taps never open sheets (B1 §11.2 regression)')
|
||||||
|
tap_side('right')
|
||||||
|
check(top_state()['photo'] != before_photo, 'right tap flips forward')
|
||||||
|
|
||||||
|
# --- item 4: rubric_debug on -> breakdown appears ----------------------
|
||||||
|
api_post('/api/prefs', {'key': 'rubric_debug', 'value': 'on'})
|
||||||
|
page.reload(wait_until='networkidle')
|
||||||
|
wait('.topcard')
|
||||||
|
flick(400)
|
||||||
|
tap_center()
|
||||||
|
check(dossier_on(), 'dossier open for the debug-mode check')
|
||||||
|
drank = page.evaluate("""() => { const d=document.getElementById('drank');
|
||||||
|
if(d.style.display==='none') return {shown:false};
|
||||||
|
return {shown:true,
|
||||||
|
lines:document.getElementById('dlines').textContent,
|
||||||
|
foot:document.getElementById('dfoot').textContent}; }""")
|
||||||
|
check(bool(drank['shown']) and bool(drank['lines']),
|
||||||
|
'item 4: rubric_debug on -> breakdown lines render')
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'item4-debug-on.png'))
|
||||||
|
key('Escape')
|
||||||
|
page.evaluate("() => document.querySelector('.tab[data-tab=trends]').click()")
|
||||||
|
flick(300)
|
||||||
|
page.evaluate("() => document.getElementById('tRubOff').click()")
|
||||||
|
flick(400)
|
||||||
|
check(api('/api/section').get('prefs', {}).get('rubric_debug') == 'off',
|
||||||
|
'Trends toggle wrote rubric_debug=off to the server')
|
||||||
|
heck = page.evaluate("""() => document.getElementById(
|
||||||
|
'tRubOff').classList.contains('on')""")
|
||||||
|
check(bool(heck), 'toggle UI paints the off state')
|
||||||
|
page.evaluate("() => document.querySelector('.tab[data-tab=deck]').click()")
|
||||||
|
flick(300)
|
||||||
|
|
||||||
|
# --- item 7: header pill
|
||||||
|
stats = api('/api/stats')
|
||||||
|
pill_txt = page.evaluate(
|
||||||
|
"() => document.getElementById('pill').textContent")
|
||||||
|
check(('%d live' % stats['live']) in pill_txt
|
||||||
|
and ('%d fresh' % stats['fresh']) in pill_txt,
|
||||||
|
'item 7: pill reads live + fresh (%s)' % pill_txt.replace('\n', ' '))
|
||||||
|
|
||||||
|
# --- v1.1.1 (issue #2): the header pill keeps itself live --------------
|
||||||
|
pl0 = api('/api/stats')
|
||||||
|
key('ArrowRight') # probe swipe, undone right after
|
||||||
|
flick(300)
|
||||||
|
pl1 = api('/api/stats')
|
||||||
|
dl = time.time() + 5
|
||||||
|
ptx = ''
|
||||||
|
while time.time() < dl:
|
||||||
|
ptx = page.evaluate("() => document.getElementById('pill').textContent")
|
||||||
|
if ('%d fresh' % pl1['fresh']) in ptx:
|
||||||
|
break
|
||||||
|
time.sleep(0.2)
|
||||||
|
check(('%d fresh' % pl1['fresh']) in ptx and ('%d live' % pl1['live']) in ptx,
|
||||||
|
'issue #2: pill refreshes itself after a swipe, no reload (%s -> %s)'
|
||||||
|
% (pl0['fresh'], ptx))
|
||||||
|
key('ArrowDown') # undo the probe swipe (net-zero)
|
||||||
|
flick(300)
|
||||||
|
pl2 = api('/api/stats')
|
||||||
|
dl = time.time() + 5
|
||||||
|
while time.time() < dl:
|
||||||
|
ptx = page.evaluate("() => document.getElementById('pill').textContent")
|
||||||
|
if ('%d fresh' % pl2['fresh']) in ptx:
|
||||||
|
break
|
||||||
|
time.sleep(0.2)
|
||||||
|
check(('%d fresh' % pl2['fresh']) in ptx,
|
||||||
|
'issue #2: pill refreshes itself after an undo, no reload (%s)' % ptx)
|
||||||
|
|
||||||
|
# --- B1 no-lock: rapid commit storm (no sheet, no lock) ----------------
|
||||||
|
before = sect_pill()['txt']
|
||||||
|
taps(2, 40)
|
||||||
|
flick(500)
|
||||||
|
t = top_state()
|
||||||
|
check(t['topcards'] == 1, 'no-lock storm keeps exactly one .topcard')
|
||||||
|
after = sect_pill()['txt']
|
||||||
|
check(int(after.split('/')[0]) - int(before.split('/')[0]) == 2,
|
||||||
|
'both storm swipes landed (%s -> %s)' % (before, after))
|
||||||
|
|
||||||
|
# --- item 10 + P3 loop: sweep to the return band and exhaust ------------
|
||||||
|
bands = {}
|
||||||
|
for _ in range(30):
|
||||||
|
tid = top_id()
|
||||||
|
if tid is not None and tid in by_id:
|
||||||
|
slot = by_id[tid]['slot']
|
||||||
|
if slot in ('return', 'wildcard') and slot not in bands:
|
||||||
|
bands[slot] = page.evaluate("""() => { const b=document.querySelector(
|
||||||
|
'.topcard .band'); if(!b) return null;
|
||||||
|
const s=getComputedStyle(b);
|
||||||
|
return {cls:b.className, color:s.color, bg:s.backgroundColor,
|
||||||
|
text:b.textContent}; }""")
|
||||||
|
if not page.evaluate("() => document.querySelector('.topcard')"):
|
||||||
|
break
|
||||||
|
key('ArrowRight')
|
||||||
|
flick(80)
|
||||||
|
check(bool(bands.get('return')),
|
||||||
|
'item 10: a SECOND LOOK card topped and carried its band')
|
||||||
|
if bands.get('return'):
|
||||||
|
check(bands['return']['color'] == 'rgb(196, 181, 253)',
|
||||||
|
'item 10: SL band text is the violet tint')
|
||||||
|
check(bands['return']['bg'] == 'rgba(124, 58, 237, 0.22)',
|
||||||
|
'item 10: SL band fill is violet 22%')
|
||||||
|
check('SECOND LOOK' in bands['return']['text'],
|
||||||
|
'item 10: SL band label intact')
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'item10-card-band.png'))
|
||||||
|
if bands.get('wildcard'):
|
||||||
|
check('wild' in bands['wildcard']['cls'],
|
||||||
|
'item 10: wildcard band keeps its amber variant')
|
||||||
|
# dossier band on a return item: drive the real openDetail path
|
||||||
|
ret_item = next((i for i in body['items'] if i['slot'] == 'return'), None)
|
||||||
|
db = page.evaluate("""(item) => { openDetail(item, {type:'deck'});
|
||||||
|
const b=document.getElementById('dband');
|
||||||
|
const s=getComputedStyle(b);
|
||||||
|
return {off:b.style.display==='none', color:s.color,
|
||||||
|
bg:s.backgroundColor, text:b.textContent}; }""", ret_item)
|
||||||
|
check(not db['off'] and db['color'] == 'rgb(196, 181, 253)'
|
||||||
|
and 'SECOND LOOK' in db['text'],
|
||||||
|
'item 10: dossier mirrors the violet SECOND LOOK band')
|
||||||
|
key('Escape')
|
||||||
|
|
||||||
|
# finish the section -> report -> forge
|
||||||
|
for _ in range(10):
|
||||||
|
if not page.evaluate("() => document.querySelector('.topcard')"):
|
||||||
|
break
|
||||||
|
key('ArrowRight')
|
||||||
|
flick(80)
|
||||||
|
wait('#report.on, #empty2.on', timeout=10000)
|
||||||
|
rep = page.evaluate("""() => ({report:document.getElementById('report')
|
||||||
|
.classList.contains('on'),
|
||||||
|
dry:document.getElementById('dryCap').style.display!=='none',
|
||||||
|
counts:document.getElementById('repCounts').textContent,
|
||||||
|
empty2:document.getElementById('empty2').classList.contains('on')})""")
|
||||||
|
check(bool(rep['report']), 'P3 loop: report card owns the exhausted deck')
|
||||||
|
check(not rep['dry'], 'item 6: rich batch shows no dry caption')
|
||||||
|
check(not rep['empty2'], 'item 8: empty2 stays hidden while stock remains')
|
||||||
|
check(sect_pill()['hot'], 'pill is hot at the section end')
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'p3-report.png'))
|
||||||
|
sec_before = section_body()['section']['id']
|
||||||
|
page.evaluate("() => document.getElementById('btnForge').click()")
|
||||||
|
wait('.topcard', timeout=20000)
|
||||||
|
check(section_body()['section']['id'] != sec_before,
|
||||||
|
'forge opened a brand-new section')
|
||||||
|
pill = sect_pill()
|
||||||
|
check(re.search(r'^\d+/25$', pill['txt']),
|
||||||
|
'new section pill starts at a plain N/25 (%s)' % pill['txt'])
|
||||||
|
check(not dossier_on() and not viewer_on(), 'forge leaves no sheets open')
|
||||||
|
print('rich walk complete')
|
||||||
|
|
||||||
|
|
||||||
|
# --- WALK2: returns-only (counter fix + dry messaging) ---------------------
|
||||||
|
|
||||||
|
def walk_returns_only():
|
||||||
|
wait('.topcard')
|
||||||
|
flick(800)
|
||||||
|
body = section_body()
|
||||||
|
check(len(body['items']) == 25 and body['recipe']['new'] == 0,
|
||||||
|
'returns-only catalog composes 25 second looks (recipe %s)'
|
||||||
|
% json.dumps(body['recipe']))
|
||||||
|
check(body['progress']['decided'] == 0
|
||||||
|
and body['progress']['total'] == 25,
|
||||||
|
'item 5: fresh batch reads 0/25 (old code painted 25/25)')
|
||||||
|
pill = sect_pill()
|
||||||
|
check(pill['txt'] == '0/25' and not pill['hot'],
|
||||||
|
'pill shows 0/25, no premature hot state')
|
||||||
|
|
||||||
|
# one real strike -> 1/25 (in-section event, not lifetime verdict)
|
||||||
|
key('ArrowRight')
|
||||||
|
pill = sect_pill()
|
||||||
|
check(pill['txt'] == '1/25', 'first strike -> 1/25 (%s)' % pill['txt'])
|
||||||
|
check(section_body()['progress']['decided'] == 1,
|
||||||
|
'server truth agrees: one in-section decision')
|
||||||
|
key('ArrowUp')
|
||||||
|
check(sect_pill()['txt'] == '2/25', 'ArrowUp maybe -> 2/25')
|
||||||
|
key('ArrowDown')
|
||||||
|
check(sect_pill()['txt'] == '1/25', 'ArrowDown undo -> 1/25')
|
||||||
|
key('Backspace')
|
||||||
|
check(sect_pill()['txt'] == '0/25', 'Backspace undo -> 0/25')
|
||||||
|
|
||||||
|
# mixed actions -> report counts proven in-section only
|
||||||
|
key('ArrowLeft')
|
||||||
|
key('ArrowUp')
|
||||||
|
key('ArrowRight')
|
||||||
|
check(sect_pill()['txt'] == '3/25', 'three strikes -> 3/25 (clamped)')
|
||||||
|
|
||||||
|
# sweep to exhaustion
|
||||||
|
for _ in range(30):
|
||||||
|
if not page.evaluate("() => document.querySelector('.topcard')"):
|
||||||
|
break
|
||||||
|
key('ArrowRight')
|
||||||
|
flick(60)
|
||||||
|
wait('#report.on', timeout=10000)
|
||||||
|
pill = sect_pill()
|
||||||
|
check(pill['txt'] == '25/25' and pill['hot'],
|
||||||
|
'pill clamps at 25/25 and goes hot (never 26/25)')
|
||||||
|
counts = api('/api/section')['counts']
|
||||||
|
check(counts['likes'] + counts['maybes'] + counts['nopes'] == 25,
|
||||||
|
'report counts the 25 in-section verdicts (not lifetime)')
|
||||||
|
rep = page.evaluate("""() => ({dry:document.getElementById('dryCap')
|
||||||
|
.style.display!=='none',
|
||||||
|
status:document.getElementById('forgeStatus').textContent})""")
|
||||||
|
check(bool(rep['dry']), 'item 6: dry caption shows on the 0-fresh report')
|
||||||
|
|
||||||
|
# dry forge -> the sentence + a fresh 0/25 returns-only deck
|
||||||
|
page.evaluate("() => document.getElementById('btnForge').click()")
|
||||||
|
for _ in range(40):
|
||||||
|
st = page.evaluate(
|
||||||
|
"() => document.getElementById('forgeStatus').textContent")
|
||||||
|
if 'Catalog fully reviewed' in st:
|
||||||
|
break
|
||||||
|
flick(250)
|
||||||
|
st = page.evaluate(
|
||||||
|
"() => document.getElementById('forgeStatus').textContent")
|
||||||
|
check('Catalog fully reviewed — fresh listings arrive with the next'
|
||||||
|
' 08:30 import; showing second looks' in st,
|
||||||
|
'item 6: dry forge status shows the exact BACKLOG sentence')
|
||||||
|
wait('.topcard', timeout=15000)
|
||||||
|
body = section_body()
|
||||||
|
check(body['recipe']['new'] == 0 and body['progress']['decided'] == 0,
|
||||||
|
'dry forge decks another 25 second looks at 0/25')
|
||||||
|
print('returns-only walk complete')
|
||||||
|
|
||||||
|
|
||||||
|
# --- WALK3: dead catalog (loud empty2) -------------------------------------
|
||||||
|
|
||||||
|
def burn_returns(db_path):
|
||||||
|
conn = sqlite3.connect(db_path)
|
||||||
|
try:
|
||||||
|
conn.execute('DELETE FROM section_items')
|
||||||
|
conn.execute("UPDATE sections SET status='closed'"
|
||||||
|
" WHERE status!='closed'")
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO section_items (section_id, listing_id, slot,
|
||||||
|
position)
|
||||||
|
SELECT (SELECT id FROM sections ORDER BY id DESC LIMIT 1),
|
||||||
|
listing_id, 'return', 1 FROM swipes
|
||||||
|
WHERE listing_id IN (SELECT id FROM listings
|
||||||
|
WHERE status='active')""")
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def walk_dead_catalog():
|
||||||
|
body = section_body()
|
||||||
|
check(body['pool'] == {'fresh': 0, 'returns': 0, 'wildcards': 0},
|
||||||
|
'item 8: drained catalog reports pool 0/0/0')
|
||||||
|
check(body['items'] == [], 'compose emits zero items on a dead pool')
|
||||||
|
wait('#empty2.on', timeout=10000)
|
||||||
|
st = page.evaluate("""() => { const e=document.getElementById('empty2');
|
||||||
|
return {on:e.classList.contains('on'),
|
||||||
|
txt:e.textContent,
|
||||||
|
report:document.getElementById('report').classList.contains('on')}; }""")
|
||||||
|
check(bool(st['on']), 'item 8: loud empty2 owns the stage')
|
||||||
|
check('No more new bikes!' in st['txt'] and '08:30' in st['txt'],
|
||||||
|
'item 8: exact title + next-import line')
|
||||||
|
check(not st['report'], 'item 8: report card masked on the dead catalog')
|
||||||
|
page.screenshot(path=os.path.join(SHOTS, 'item8-empty2.png'))
|
||||||
|
|
||||||
|
# forge on the dead catalog: still empty2, no wasted Forge invitation
|
||||||
|
page.evaluate("() => document.getElementById('btnForge').click()")
|
||||||
|
flick(1500)
|
||||||
|
st = page.evaluate("""() => { const e=document.getElementById('empty2');
|
||||||
|
return {on:e.classList.contains('on'),
|
||||||
|
report:document.getElementById('report').classList.contains('on')}; }""")
|
||||||
|
check(bool(st['on']) and not st['report'],
|
||||||
|
'forge on 0/0/0 leaves empty2 (report never re-invites)')
|
||||||
|
|
||||||
|
# nightly import simulated: a brand-new listing re-lights the deck
|
||||||
|
stop_server()
|
||||||
|
conn = sqlite3.connect(WALK3)
|
||||||
|
try:
|
||||||
|
lid = 990
|
||||||
|
conn.execute("DELETE FROM listings WHERE id=?", (lid,))
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO listings (id, region, src_id, title, url, status,"
|
||||||
|
" img_url, score, first_seen, desc) VALUES"
|
||||||
|
" (?, 'ns', 'v11night', 'Fresh overnight listing',"
|
||||||
|
" 'https://www.kijiji.ca/v-motorcycles/v11/990', 'active',"
|
||||||
|
" NULL, 50, '2026-11-13T08:30:00Z', 'arrived with the import')",
|
||||||
|
(lid,))
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
start_server(WALK3)
|
||||||
|
page.reload(wait_until='networkidle')
|
||||||
|
wait('.topcard', timeout=15000)
|
||||||
|
st = page.evaluate("""() => ({empty2:document.getElementById('empty2')
|
||||||
|
.classList.contains('on'), top:!!document.querySelector('.topcard')})""")
|
||||||
|
check(bool(st['top']) and not st['empty2'],
|
||||||
|
'item 8: one fresh listing clears empty2 and relights the deck')
|
||||||
|
print('dead-catalog walk complete')
|
||||||
|
|
||||||
|
|
||||||
|
# --- WALK0: hand the rich DB a quick dry run through items 1 acceptance ---
|
||||||
|
|
||||||
|
def main():
|
||||||
|
global page
|
||||||
|
setup() # always: fresh staged DBs per run
|
||||||
|
# clear any stray scratch server left by an aborted run
|
||||||
|
import signal
|
||||||
|
for line in subprocess.run(
|
||||||
|
['ps', '-eo', 'pid,args'], capture_output=True,
|
||||||
|
text=True).stdout.splitlines():
|
||||||
|
if 'app.serve' in line and str(PORT) in line:
|
||||||
|
try:
|
||||||
|
os.kill(int(line.split()[0]), signal.SIGTERM)
|
||||||
|
except (ValueError, ProcessLookupError):
|
||||||
|
pass
|
||||||
|
time.sleep(0.5)
|
||||||
|
try:
|
||||||
|
from playwright.sync_api import sync_playwright
|
||||||
|
except ImportError:
|
||||||
|
print('playwright missing — run: .venv/bin/python -m app.fe_walk_v11')
|
||||||
|
return 1
|
||||||
|
|
||||||
|
with sync_playwright() as pw:
|
||||||
|
exe = os.path.expanduser(
|
||||||
|
'~/.cache/ms-playwright/chromium-1243/chrome-linux64/chrome')
|
||||||
|
if not os.path.exists(exe):
|
||||||
|
exe = None
|
||||||
|
|
||||||
|
# phase 1: rich
|
||||||
|
start_server(WALK1)
|
||||||
|
try:
|
||||||
|
browser = pw.chromium.launch(headless=True, executable_path=exe,
|
||||||
|
args=['--no-sandbox'])
|
||||||
|
ctx = browser.new_context(viewport={'width': 390, 'height': 844},
|
||||||
|
has_touch=True, is_mobile=True)
|
||||||
|
page = ctx.new_page()
|
||||||
|
page.goto(BASE_URL + '/', wait_until='networkidle')
|
||||||
|
walk_rich()
|
||||||
|
ctx.close()
|
||||||
|
finally:
|
||||||
|
stop_server()
|
||||||
|
|
||||||
|
# phase 2: returns-only (uses WALK2 fresh from setup)
|
||||||
|
start_server(WALK2)
|
||||||
|
try:
|
||||||
|
browser = pw.chromium.launch(headless=True, executable_path=exe,
|
||||||
|
args=['--no-sandbox'])
|
||||||
|
ctx = browser.new_context(viewport={'width': 390, 'height': 844},
|
||||||
|
has_touch=True, is_mobile=True)
|
||||||
|
page = ctx.new_page()
|
||||||
|
page.goto(BASE_URL + '/', wait_until='networkidle')
|
||||||
|
walk_returns_only()
|
||||||
|
ctx.close()
|
||||||
|
finally:
|
||||||
|
stop_server()
|
||||||
|
|
||||||
|
# phase 3: dead catalog
|
||||||
|
shutil.copy2(WALK2, WALK3)
|
||||||
|
burn_returns(WALK3)
|
||||||
|
start_server(WALK3)
|
||||||
|
try:
|
||||||
|
browser = pw.chromium.launch(headless=True, executable_path=exe,
|
||||||
|
args=['--no-sandbox'])
|
||||||
|
ctx = browser.new_context(viewport={'width': 390, 'height': 844},
|
||||||
|
has_touch=True, is_mobile=True)
|
||||||
|
page = ctx.new_page()
|
||||||
|
page.goto(BASE_URL + '/', wait_until='networkidle')
|
||||||
|
walk_dead_catalog()
|
||||||
|
ctx.close()
|
||||||
|
finally:
|
||||||
|
stop_server()
|
||||||
|
|
||||||
|
print('---')
|
||||||
|
print('%d/%d walk checks passed' % (CHECKS[0], CHECKS[0] + CHECKS[1]))
|
||||||
|
return 1 if CHECKS[1] else 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
@@ -1,18 +1,24 @@
|
|||||||
"""Bike-Tinder local image mirror.
|
"""Bike-Tinder local image mirror.
|
||||||
|
|
||||||
Downloads each active listing's card image once into data/img/{id}.webp.
|
Downloads each active listing's photos once:
|
||||||
|
- gallery listings (B1 §11.1): imgs[n] -> data/img/{id}_{n}.webp for the
|
||||||
|
extra photos; the primary (n=0) lands at the legacy data/img/{id}.webp
|
||||||
|
so the v1 single-image chain is untouched;
|
||||||
|
- legacy listings: img_url -> data/img/{id}.webp (as before).
|
||||||
|
|
||||||
.venv/bin/python -m app.imgfetch --db data/bike-tinder.sqlite3
|
.venv/bin/python -m app.imgfetch --db data/bike-tinder.sqlite3
|
||||||
.venv/bin/python -m app.imgfetch --limit 15
|
.venv/bin/python -m app.imgfetch --limit 15
|
||||||
|
|
||||||
Polite by design: ~1 req/s (sleep between fetches), skip-existing, crash-safe
|
Polite by design: ~1 req/s (sleep between fetches), skip-existing, crash-safe
|
||||||
(partial bytes land in .part and are atomically renamed), per-item failures
|
(partial bytes land in .part and are atomically renamed), per-item failures
|
||||||
never kill the run. Honest end summary: `fetched N · skipped M · failed K`.
|
never kill the run. Per-file CLI progress; honest end summary:
|
||||||
|
`fetched N · skipped M · failed K`.
|
||||||
|
|
||||||
UA + habits match the hunt scanner (reference/motorcycle-hunt-scan.py) so the
|
UA + habits match the hunt scanner (reference/motorcycle-hunt-scan.py) so the
|
||||||
CDN sees the same client as the crawl. Stdlib only — no requests dependency.
|
CDN sees the same client as the crawl. Stdlib only — no requests dependency.
|
||||||
"""
|
"""
|
||||||
import argparse
|
import argparse
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
@@ -44,8 +50,10 @@ def fetch_one(img_url, dst):
|
|||||||
})
|
})
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(req, timeout=TIMEOUT) as resp:
|
with urllib.request.urlopen(req, timeout=TIMEOUT) as resp:
|
||||||
if getattr(resp, 'status', 200) != 200:
|
status = (getattr(resp, 'status', None)
|
||||||
return 'HTTP %s' % resp.status
|
or getattr(resp, 'code', None) or 200)
|
||||||
|
if status != 200:
|
||||||
|
return 'HTTP %s' % status
|
||||||
buf = resp.read()
|
buf = resp.read()
|
||||||
if not is_webp(buf):
|
if not is_webp(buf):
|
||||||
return 'not webp (%d bytes, %r)' % (len(buf), buf[:8])
|
return 'not webp (%d bytes, %r)' % (len(buf), buf[:8])
|
||||||
@@ -65,33 +73,76 @@ def fetch_one(img_url, dst):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def parse_imgs(raw):
|
||||||
|
"""listings.imgs TEXT -> list of non-empty URL strings ([] when unparsable)."""
|
||||||
|
if not raw:
|
||||||
|
return []
|
||||||
|
try:
|
||||||
|
v = json.loads(raw)
|
||||||
|
except ValueError:
|
||||||
|
return []
|
||||||
|
if not isinstance(v, list):
|
||||||
|
return []
|
||||||
|
return [u.strip() for u in v if isinstance(u, str) and u.strip()]
|
||||||
|
|
||||||
|
|
||||||
|
def build_work(rows, img_dir):
|
||||||
|
"""Flatten listings rows into (listing_id, n_or_None, url, dst) downloads.
|
||||||
|
|
||||||
|
Gallery rows fetch every imgs[n]: n=0 -> the legacy {id}.webp (the primary
|
||||||
|
— one canonical file, no duplicate fetch), n>=1 -> {id}_{n}.webp. Legacy
|
||||||
|
rows (no gallery) keep the img_url -> {id}.webp chain. Work is deduped by
|
||||||
|
destination path.
|
||||||
|
"""
|
||||||
|
work = []
|
||||||
|
for r in rows:
|
||||||
|
g = parse_imgs(r['imgs'])
|
||||||
|
if g:
|
||||||
|
for n, url in enumerate(g):
|
||||||
|
dst = os.path.join(img_dir, ('%d.webp' % r['id'] if n == 0
|
||||||
|
else '%d_%d.webp' % (r['id'], n)))
|
||||||
|
work.append((r['id'], n, url, dst))
|
||||||
|
elif r['img_url']:
|
||||||
|
work.append((r['id'], None, r['img_url'],
|
||||||
|
os.path.join(img_dir, '%d.webp' % r['id'])))
|
||||||
|
seen, deduped = set(), []
|
||||||
|
for item in work:
|
||||||
|
if item[3] not in seen:
|
||||||
|
seen.add(item[3])
|
||||||
|
deduped.append(item)
|
||||||
|
return deduped
|
||||||
|
|
||||||
|
|
||||||
def run_fetch(db_path, img_dir, limit=None, sleep=SLEEP, quiet=False):
|
def run_fetch(db_path, img_dir, limit=None, sleep=SLEEP, quiet=False):
|
||||||
conn = db.connect(db_path)
|
conn = db.connect(db_path)
|
||||||
db.init_db(conn)
|
db.init_db(conn)
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
"SELECT id, img_url FROM listings"
|
"SELECT id, img_url, imgs FROM listings"
|
||||||
" WHERE status='active' AND img_url IS NOT NULL"
|
" WHERE status='active' ORDER BY id").fetchall()
|
||||||
" ORDER BY id").fetchall()
|
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
os.makedirs(img_dir, exist_ok=True)
|
os.makedirs(img_dir, exist_ok=True)
|
||||||
|
work = build_work(rows, img_dir)
|
||||||
|
total = len(work)
|
||||||
fetched = skipped = failed = 0
|
fetched = skipped = failed = 0
|
||||||
for row in rows:
|
for idx, (lid, n, url, dst) in enumerate(work, 1):
|
||||||
if limit is not None and fetched >= limit:
|
if limit is not None and fetched >= limit:
|
||||||
break
|
break
|
||||||
dst = os.path.join(img_dir, '%d.webp' % row['id'])
|
|
||||||
if os.path.exists(dst):
|
if os.path.exists(dst):
|
||||||
skipped += 1
|
skipped += 1
|
||||||
continue
|
continue
|
||||||
result = fetch_one(row['img_url'], dst)
|
result = fetch_one(url, dst)
|
||||||
if result == 'fetched':
|
if result == 'fetched':
|
||||||
fetched += 1
|
fetched += 1
|
||||||
|
if not quiet:
|
||||||
|
print(' [%d/%d] id=%d n=%s ok' % (idx, total, lid, n))
|
||||||
else:
|
else:
|
||||||
failed += 1
|
failed += 1
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print(' fail id=%d %s' % (row['id'], result))
|
print(' fail id=%d n=%s %s' % (lid, n, result))
|
||||||
time.sleep(sleep)
|
time.sleep(sleep)
|
||||||
return {'fetched': fetched, 'skipped': skipped, 'failed': failed}
|
return {'fetched': fetched, 'skipped': skipped, 'failed': failed,
|
||||||
|
'total': total}
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
@@ -105,8 +156,8 @@ def main(argv=None):
|
|||||||
args = ap.parse_args(argv)
|
args = ap.parse_args(argv)
|
||||||
|
|
||||||
res = run_fetch(args.db, args.img_dir, limit=args.limit, sleep=args.sleep)
|
res = run_fetch(args.db, args.img_dir, limit=args.limit, sleep=args.sleep)
|
||||||
print('fetched %d · skipped %d · failed %d' % (
|
print('fetched %d · skipped %d · failed %d · total %d' % (
|
||||||
res['fetched'], res['skipped'], res['failed']))
|
res['fetched'], res['skipped'], res['failed'], res['total']))
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -88,6 +88,24 @@ def iter_items(st):
|
|||||||
yield src_id, it
|
yield src_id, it
|
||||||
|
|
||||||
|
|
||||||
|
def imgs_from_item(it):
|
||||||
|
"""Gallery images from a state item: deduplicated, non-empty URL strings
|
||||||
|
in page order, or None. The hunt guarantees imgs[0] equals `img` (the
|
||||||
|
established primary)."""
|
||||||
|
raw = it.get('imgs')
|
||||||
|
if not isinstance(raw, (list, tuple)):
|
||||||
|
return None
|
||||||
|
out, seen = [], set()
|
||||||
|
for u in raw:
|
||||||
|
if not isinstance(u, str):
|
||||||
|
continue
|
||||||
|
u = u.strip()
|
||||||
|
if u and u not in seen:
|
||||||
|
seen.add(u)
|
||||||
|
out.append(u)
|
||||||
|
return out or None
|
||||||
|
|
||||||
|
|
||||||
def normalize(region, src_id, it, now):
|
def normalize(region, src_id, it, now):
|
||||||
"""Normalize one state item into a row dict (strings->ints robustly,
|
"""Normalize one state item into a row dict (strings->ints robustly,
|
||||||
missing fields -> NULL)."""
|
missing fields -> NULL)."""
|
||||||
@@ -102,6 +120,10 @@ def normalize(region, src_id, it, now):
|
|||||||
year = rubric.year_from_title(title)
|
year = rubric.year_from_title(title)
|
||||||
score, family = rubric.score_item(title, it.get('desc') or '', price_num, km, year)
|
score, family = rubric.score_item(title, it.get('desc') or '', price_num, km, year)
|
||||||
extras = rubric.extras_from_text(title, it.get('desc') or '')
|
extras = rubric.extras_from_text(title, it.get('desc') or '')
|
||||||
|
# B1: gallery (page order, entry 0 = the primary). img_url keeps the
|
||||||
|
# established single `img` signal and falls back to imgs[0].
|
||||||
|
imgs = imgs_from_item(it)
|
||||||
|
img = it.get('img') or (imgs[0] if imgs else None)
|
||||||
# seller_type only from a REAL signal in the state item (the hunt image
|
# seller_type only from a REAL signal in the state item (the hunt image
|
||||||
# backfill / enrichment passes) — never inferred from titles.
|
# backfill / enrichment passes) — never inferred from titles.
|
||||||
seller = it.get('seller_type') or it.get('seller')
|
seller = it.get('seller_type') or it.get('seller')
|
||||||
@@ -116,7 +138,8 @@ def normalize(region, src_id, it, now):
|
|||||||
'km': km,
|
'km': km,
|
||||||
'location': (it.get('loc') or it.get('city') or None),
|
'location': (it.get('loc') or it.get('city') or None),
|
||||||
'url': it.get('url') or '',
|
'url': it.get('url') or '',
|
||||||
'img_url': it.get('img') or None,
|
'img_url': img,
|
||||||
|
'imgs': json.dumps(imgs) if imgs else None,
|
||||||
'desc': it.get('desc') or None,
|
'desc': it.get('desc') or None,
|
||||||
'family': family,
|
'family': family,
|
||||||
'score': score,
|
'score': score,
|
||||||
@@ -138,25 +161,25 @@ def upsert(conn, row, now):
|
|||||||
if existing:
|
if existing:
|
||||||
conn.execute(
|
conn.execute(
|
||||||
'''UPDATE listings SET title=?, price_display=?, price_num=?, km=?,
|
'''UPDATE listings SET title=?, price_display=?, price_num=?, km=?,
|
||||||
location=?, url=?, img_url=?, desc=?, family=?, score=?,
|
location=?, url=?, img_url=?, imgs=?, desc=?, family=?, score=?,
|
||||||
year=?, seller_type=?, extras=?,
|
year=?, seller_type=?, extras=?,
|
||||||
status='active', dup_of=NULL, last_seen=?, updated_at=?
|
status='active', dup_of=NULL, last_seen=?, updated_at=?
|
||||||
WHERE id=?''',
|
WHERE id=?''',
|
||||||
(row['title'], row['price_display'], row['price_num'], row['km'],
|
(row['title'], row['price_display'], row['price_num'], row['km'],
|
||||||
row['location'], row['url'], row['img_url'], row['desc'],
|
row['location'], row['url'], row['img_url'], row['imgs'],
|
||||||
row['family'], row['score'], row['year'], row['seller_type'],
|
row['desc'], row['family'], row['score'], row['year'],
|
||||||
row['extras'], now, now, existing['id']))
|
row['seller_type'], row['extras'], now, now, existing['id']))
|
||||||
return existing['id'], False
|
return existing['id'], False
|
||||||
cur = conn.execute(
|
cur = conn.execute(
|
||||||
'''INSERT INTO listings (region, src_id, title, price_display, price_num,
|
'''INSERT INTO listings (region, src_id, title, price_display, price_num,
|
||||||
km, location, url, img_url, desc, family, score, year, seller_type,
|
km, location, url, img_url, imgs, desc, family, score, year,
|
||||||
extras, status, dup_of,
|
seller_type, extras, status, dup_of,
|
||||||
first_seen, last_seen, created_at, updated_at)
|
first_seen, last_seen, created_at, updated_at)
|
||||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
|
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
|
||||||
(row['region'], row['src_id'], row['title'], row['price_display'],
|
(row['region'], row['src_id'], row['title'], row['price_display'],
|
||||||
row['price_num'], row['km'], row['location'], row['url'],
|
row['price_num'], row['km'], row['location'], row['url'],
|
||||||
row['img_url'], row['desc'], row['family'], row['score'], row['year'],
|
row['img_url'], row['imgs'], row['desc'], row['family'], row['score'],
|
||||||
row['seller_type'], row['extras'], 'active',
|
row['year'], row['seller_type'], row['extras'], 'active',
|
||||||
None, row['first_seen'], row['last_seen'], now, now))
|
None, row['first_seen'], row['last_seen'], now, now))
|
||||||
return cur.lastrowid, True
|
return cur.lastrowid, True
|
||||||
|
|
||||||
|
|||||||
576
app/sections.py
Normal file
576
app/sections.py
Normal file
@@ -0,0 +1,576 @@
|
|||||||
|
"""Bike-Tinder P3 section engine — compose / read / exhaust / forge.
|
||||||
|
|
||||||
|
v1.1 batch formula (amends the frozen "<=5 returns" rule — one-shot,
|
||||||
|
re-nope and one-return rules unchanged): every batch targets 25 cards =
|
||||||
|
fresh first (up to 23 slots) + >=2 second looks whenever eligible stock
|
||||||
|
exists + the 2 wildcard slots; when fresh < the remainder, second looks
|
||||||
|
FILL the batch up to 25 (family cap relaxes to 8 for fill returns only).
|
||||||
|
Fresh exhausted -> returns-only batches; returns exhausted -> fresh-only;
|
||||||
|
both -> an empty composed section (the loud empty state, item 8).
|
||||||
|
|
||||||
|
Pinned order within a section (position 1 first):
|
||||||
|
new (points DESC) -> returns (maybe > like > nope, points DESC within
|
||||||
|
role) -> wildcards (wildcard score DESC). "The best suggestions lead"
|
||||||
|
(docs/PLAN.md); returns form their own sub-sequence in role order;
|
||||||
|
wildcard stretches sit at the back. (Wildcard placement inside the deck
|
||||||
|
is not pinned anywhere in the frozen docs — noted in the P3 report.)
|
||||||
|
|
||||||
|
Constraints: <=4 per non-NULL family per section (fill-return slots relax
|
||||||
|
to <=8); >=3 price bands when the pool allows (soft repair swaps new-slot
|
||||||
|
items within the final list). <=2 re-nope returns per batch (reserved
|
||||||
|
nope slots must be "surprising"). A re-nope (round>=2 nope event) rests
|
||||||
|
an item: no more second looks. Rounds cap at 2 for influence (taste.py);
|
||||||
|
returns are offered once.
|
||||||
|
|
||||||
|
Modes: 'ondemand' (default) — forge runs the agent pass then composes;
|
||||||
|
'strict' — forge refuses (409) unless a taste_profile strictly newer than
|
||||||
|
the current section's profile_version exists.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
|
||||||
|
from . import taste as tastemod
|
||||||
|
from .taste import NOPE
|
||||||
|
|
||||||
|
FAMILY_CAP = 4
|
||||||
|
RETURN_FILL_FAMILY_CAP = 8
|
||||||
|
MAX_RETURN_NOPES = 2
|
||||||
|
WILDCARDS = 2
|
||||||
|
FRESH_CAP = 23
|
||||||
|
MIN_RETURNS = 2
|
||||||
|
DEFAULT_SIZE = 25
|
||||||
|
MIN_BANDS = 3
|
||||||
|
|
||||||
|
LAST_TWO_SQL = """
|
||||||
|
SELECT si.listing_id FROM section_items si
|
||||||
|
WHERE si.section_id IN (SELECT id FROM sections ORDER BY id DESC LIMIT 2)
|
||||||
|
"""
|
||||||
|
|
||||||
|
NEW_POOL_SQL = """
|
||||||
|
SELECT l.* FROM listings l
|
||||||
|
WHERE l.status = 'active'
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM swipes s WHERE s.listing_id = l.id)
|
||||||
|
"""
|
||||||
|
|
||||||
|
RETURNS_SQL = """
|
||||||
|
SELECT l.*, s.action AS verdict FROM listings l
|
||||||
|
JOIN swipes s ON s.listing_id = l.id
|
||||||
|
WHERE l.status = 'active'
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM swipe_events e
|
||||||
|
WHERE e.listing_id = l.id AND e.round >= 2
|
||||||
|
AND e.action = 'nope')
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM section_items si
|
||||||
|
WHERE si.listing_id = l.id AND si.slot = 'return')
|
||||||
|
"""
|
||||||
|
|
||||||
|
WILDCARD_SQL = """
|
||||||
|
SELECT l.* FROM listings l
|
||||||
|
WHERE l.status = 'active'
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM swipe_events e WHERE e.listing_id = l.id)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# --- helpers ----------------------------------------------------------------
|
||||||
|
|
||||||
|
def _iso(now):
|
||||||
|
return now.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||||
|
|
||||||
|
|
||||||
|
def last_two_listing_ids(conn):
|
||||||
|
return {r['listing_id'] for r in conn.execute(LAST_TWO_SQL)}
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_section(conn):
|
||||||
|
"""The newest open/report section, or None."""
|
||||||
|
return conn.execute(
|
||||||
|
"SELECT * FROM sections WHERE status IN ('open','report')"
|
||||||
|
' ORDER BY id DESC LIMIT 1').fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
def latest_profile(conn):
|
||||||
|
return conn.execute(
|
||||||
|
'SELECT * FROM taste_profile ORDER BY version DESC LIMIT 1'
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
def build_engine(conn, profile=None, now=None):
|
||||||
|
"""TasteEngine over active listings + every listing an event touches,
|
||||||
|
with the given profile payload (default: the latest version)."""
|
||||||
|
rows = [r for r in conn.execute(
|
||||||
|
"SELECT * FROM listings WHERE status='active'")]
|
||||||
|
ev_ids = {r['listing_id'] for r in conn.execute(
|
||||||
|
'SELECT DISTINCT listing_id FROM swipe_events')}
|
||||||
|
missing = ev_ids - {r['id'] for r in rows}
|
||||||
|
if missing:
|
||||||
|
qmarks = ','.join('?' * len(missing))
|
||||||
|
rows += [r for r in conn.execute(
|
||||||
|
'SELECT * FROM listings WHERE id IN (%s)' % qmarks,
|
||||||
|
tuple(missing))]
|
||||||
|
events = conn.execute('SELECT * FROM swipe_events ORDER BY id').fetchall()
|
||||||
|
payload = None
|
||||||
|
if profile is None:
|
||||||
|
profile = latest_profile(conn)
|
||||||
|
if profile is not None and profile['payload']:
|
||||||
|
try:
|
||||||
|
payload = json.loads(profile['payload'])
|
||||||
|
except ValueError:
|
||||||
|
payload = None
|
||||||
|
return tastemod.TasteEngine(rows, events, payload, now=now)
|
||||||
|
|
||||||
|
|
||||||
|
def fresh_profile_exists(conn, section):
|
||||||
|
"""Strict-mode freshness: a profile version newer than the section's."""
|
||||||
|
cur = section['profile_version'] if section else None
|
||||||
|
if latest_profile(conn) is None:
|
||||||
|
return False
|
||||||
|
if cur is None:
|
||||||
|
return True
|
||||||
|
return latest_profile(conn)['version'] > cur
|
||||||
|
|
||||||
|
|
||||||
|
# --- wildcard novelty --------------------------------------------------------
|
||||||
|
|
||||||
|
def _cosine(a, b):
|
||||||
|
dot = sum(a.get(k, 0.0) * b.get(k, 0.0) for k in (a.keys() | b.keys()))
|
||||||
|
na = sum(x * x for x in a.values()) ** 0.5
|
||||||
|
nb = sum(x * x for x in b.values()) ** 0.5
|
||||||
|
if not na or not nb:
|
||||||
|
return 0.0
|
||||||
|
return dot / (na * nb)
|
||||||
|
|
||||||
|
|
||||||
|
def novelty(rows_by_id, liked_ids, candidate_ids):
|
||||||
|
"""0..1 distance from the liked-profile centroid (1 = very novel)."""
|
||||||
|
if not liked_ids:
|
||||||
|
return {lid: 0.5 for lid in candidate_ids}
|
||||||
|
centroid = {}
|
||||||
|
liked = [l for l in liked_ids if l in rows_by_id]
|
||||||
|
for lid in liked:
|
||||||
|
for d, v in tastemod.extract_features(rows_by_id[lid]):
|
||||||
|
centroid[(d, v)] = centroid.get((d, v), 0.0) + 1.0
|
||||||
|
n = len(liked) or 1
|
||||||
|
centroid = {k: v / n for k, v in centroid.items()}
|
||||||
|
out = {}
|
||||||
|
for lid in candidate_ids:
|
||||||
|
vec = {(d, v): 1.0 for d, v in tastemod.extract_features(rows_by_id[lid])}
|
||||||
|
out[lid] = 1.0 - _cosine(vec, centroid)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
# --- compose -----------------------------------------------------------------
|
||||||
|
|
||||||
|
def _points_key(engine, row):
|
||||||
|
"""Deterministic sort key: points, rubric score, first_seen, id.
|
||||||
|
Use with reverse=True for descending order."""
|
||||||
|
return (engine.points(row['id']), row['score'] or 0,
|
||||||
|
row['first_seen'] or '', row['id'])
|
||||||
|
|
||||||
|
|
||||||
|
def pick_new(engine, candidates, want, fam_count, picked_ids):
|
||||||
|
"""Greedy new picks by points DESC honoring the family cap."""
|
||||||
|
out = []
|
||||||
|
ordered = sorted(candidates, key=lambda r: _points_key(engine, r),
|
||||||
|
reverse=True)
|
||||||
|
for cand in ordered:
|
||||||
|
if len(out) >= want:
|
||||||
|
break
|
||||||
|
lid = cand['id']
|
||||||
|
if lid in picked_ids:
|
||||||
|
continue
|
||||||
|
fam = cand['family']
|
||||||
|
if fam and fam_count.get(fam, 0) >= FAMILY_CAP:
|
||||||
|
continue
|
||||||
|
out.append(cand)
|
||||||
|
picked_ids.add(lid)
|
||||||
|
if fam:
|
||||||
|
fam_count[fam] = fam_count.get(fam, 0) + 1
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def repair_bands(engine, picked, candidates, fam_count, picked_ids):
|
||||||
|
"""Swap new-slot items to reach >=3 price bands when the pool allows.
|
||||||
|
|
||||||
|
Only swaps INTO slots of new items in over-represented bands; returns
|
||||||
|
and wildcard slots are never touched. Deterministic; bounded passes.
|
||||||
|
"""
|
||||||
|
if len(picked) < MIN_BANDS:
|
||||||
|
return picked
|
||||||
|
bands_of = {r['id']: tastemod.price_band(r['price_num']) for r in candidates}
|
||||||
|
bands_of.update({r['id']: tastemod.price_band(r['price_num'])
|
||||||
|
for r in picked})
|
||||||
|
cur_bands = {bands_of[r['id']] for r in picked
|
||||||
|
if bands_of[r['id']] is not None}
|
||||||
|
if len(cur_bands) >= MIN_BANDS:
|
||||||
|
return picked
|
||||||
|
pool_bands = {bands_of[r['id']] for r in candidates
|
||||||
|
if bands_of[r['id']] is not None}
|
||||||
|
if len(pool_bands) < MIN_BANDS:
|
||||||
|
return picked # the pool does not allow 3 bands
|
||||||
|
picked = list(picked)
|
||||||
|
attempts = len(candidates)
|
||||||
|
for _ in range(attempts):
|
||||||
|
cur_bands = {bands_of[r['id']] for r in picked
|
||||||
|
if bands_of[r['id']] is not None}
|
||||||
|
if len(cur_bands) >= MIN_BANDS:
|
||||||
|
break
|
||||||
|
missing = [b for b in pool_bands if b not in cur_bands]
|
||||||
|
if not missing:
|
||||||
|
break
|
||||||
|
want_band = missing[0]
|
||||||
|
best = None
|
||||||
|
best_key = None
|
||||||
|
for cand in candidates:
|
||||||
|
if cand['id'] in picked_ids:
|
||||||
|
continue
|
||||||
|
if bands_of[cand['id']] != want_band:
|
||||||
|
continue
|
||||||
|
fam = cand['family']
|
||||||
|
if fam and fam_count.get(fam, 0) >= FAMILY_CAP:
|
||||||
|
continue
|
||||||
|
key = _points_key(engine, cand)
|
||||||
|
if best_key is None or key > best_key:
|
||||||
|
best, best_key = cand, key
|
||||||
|
if best is None:
|
||||||
|
pool_bands = pool_bands - {want_band}
|
||||||
|
if len(pool_bands) < MIN_BANDS:
|
||||||
|
break
|
||||||
|
continue
|
||||||
|
# victim: lowest-points new-slot item from a band with >=2 reps
|
||||||
|
counts = {}
|
||||||
|
for r in picked:
|
||||||
|
b = bands_of[r['id']]
|
||||||
|
if b is not None:
|
||||||
|
counts[b] = counts.get(b, 0) + 1
|
||||||
|
victim = None
|
||||||
|
worst_first = sorted(picked, key=lambda r: _points_key(engine, r),
|
||||||
|
reverse=True)
|
||||||
|
for band in sorted(counts, key=lambda b: -counts[b]):
|
||||||
|
if counts[band] < 2:
|
||||||
|
continue
|
||||||
|
in_band = [r for r in worst_first if bands_of[r['id']] == band]
|
||||||
|
if in_band:
|
||||||
|
victim = in_band[-1]
|
||||||
|
break
|
||||||
|
if victim is None:
|
||||||
|
break
|
||||||
|
victim_fam = victim['family']
|
||||||
|
idx = next(i for i, r in enumerate(picked) if r['id'] == victim['id'])
|
||||||
|
picked[idx] = best
|
||||||
|
if victim_fam and victim_fam in fam_count:
|
||||||
|
fam_count[victim_fam] = max(0, fam_count[victim_fam] - 1)
|
||||||
|
if best['family']:
|
||||||
|
fam_count[best['family']] = fam_count.get(best['family'], 0) + 1
|
||||||
|
picked_ids.remove(victim['id'])
|
||||||
|
picked_ids.add(best['id'])
|
||||||
|
return picked
|
||||||
|
|
||||||
|
|
||||||
|
def compose_section(conn, engine, size=DEFAULT_SIZE, profile_version=None,
|
||||||
|
note='', now=None):
|
||||||
|
"""Compose and persist a new open section; returns the section row.
|
||||||
|
|
||||||
|
Caller closes/reports any prior section first. `engine` scores the
|
||||||
|
candidates (a TasteEngine over active listings + events + profile).
|
||||||
|
|
||||||
|
v1.1 slot math (see module docstring; this amends the frozen "<=5
|
||||||
|
returns" rule): reserved returns (>=2 when eligible stock exists) ->
|
||||||
|
wildcards (2) -> fresh (capped at 23) -> fill returns to `size`
|
||||||
|
(family cap 4 preferred, 8 rescue; <=2 re-nopes total).
|
||||||
|
"""
|
||||||
|
import datetime
|
||||||
|
now = now or datetime.datetime.now(datetime.timezone.utc).replace(
|
||||||
|
tzinfo=None)
|
||||||
|
size = int(size)
|
||||||
|
rows_by_id = {r['id']: r for r in engine.listings}
|
||||||
|
last_two = last_two_listing_ids(conn)
|
||||||
|
|
||||||
|
new_cands = [r for r in conn.execute(NEW_POOL_SQL)
|
||||||
|
if r['id'] not in last_two]
|
||||||
|
ret_cands = [r for r in conn.execute(RETURNS_SQL)
|
||||||
|
if r['id'] not in last_two]
|
||||||
|
wild_cands = [r for r in conn.execute(WILDCARD_SQL)
|
||||||
|
if r['id'] not in last_two]
|
||||||
|
|
||||||
|
# wildcard family exclusions: >=3 cumulative nopes AND net-negative.
|
||||||
|
# Stats are computed for every family the engine knows (active + event
|
||||||
|
# listings), not just current candidates — a hard-noped family is
|
||||||
|
# excluded even when its rows are gone/cleared.
|
||||||
|
fam_stats = {f: engine.family_net(f)
|
||||||
|
for f in {r['family'] for r in engine.listings if r['family']}}
|
||||||
|
excluded_fams = {f for f, (net, nopes) in fam_stats.items()
|
||||||
|
if nopes >= 3 and net < 0}
|
||||||
|
wild_cands = [r for r in wild_cands
|
||||||
|
if not (r['family'] and r['family'] in excluded_fams)]
|
||||||
|
|
||||||
|
fam_count = {}
|
||||||
|
picked_ids = set()
|
||||||
|
|
||||||
|
# 1) preliminary top-3 new (uncommitted) — keys the wildcard preference
|
||||||
|
top3 = pick_new(engine, new_cands, 3, {}, set())
|
||||||
|
top_fams = {r['family'] for r in top3 if r['family']}
|
||||||
|
top_bands = {tastemod.price_band(r['price_num']) for r in top3
|
||||||
|
if tastemod.price_band(r['price_num']) is not None}
|
||||||
|
|
||||||
|
# 2) reserved returns: the >=2 guarantee (role order maybe > like > nope,
|
||||||
|
# points DESC within role). Nopes must be "surprising" for RESERVED
|
||||||
|
# slots only (points >=55 or net-positive family); <=2 re-nope
|
||||||
|
# returns per batch overall; family cap 4 applies here.
|
||||||
|
role_order = {'maybe': 0, 'like': 1, 'nope': 2}
|
||||||
|
ret_ordered = sorted(ret_cands, reverse=True,
|
||||||
|
key=lambda r: (-role_order.get(r['verdict'], 9),
|
||||||
|
_points_key(engine, r)))
|
||||||
|
rets = []
|
||||||
|
nope_count = 0
|
||||||
|
for cand in ret_ordered:
|
||||||
|
if len(rets) >= min(MIN_RETURNS, size):
|
||||||
|
break
|
||||||
|
fam = cand['family']
|
||||||
|
if fam and fam_count.get(fam, 0) >= FAMILY_CAP:
|
||||||
|
continue
|
||||||
|
if cand['verdict'] == NOPE:
|
||||||
|
if nope_count >= MAX_RETURN_NOPES:
|
||||||
|
continue
|
||||||
|
net, _nopes = fam_stats.get(fam, (0.0, 0))
|
||||||
|
if engine.points(cand['id']) < 55 and not (fam and net > 0):
|
||||||
|
continue
|
||||||
|
nope_count += 1
|
||||||
|
rets.append(cand)
|
||||||
|
picked_ids.add(cand['id'])
|
||||||
|
if fam:
|
||||||
|
fam_count[fam] = fam_count.get(fam, 0) + 1
|
||||||
|
|
||||||
|
# 3) wildcards: min(2, remaining slots), preferred different family AND
|
||||||
|
# price band (existing novel/preferred pick, unchanged)
|
||||||
|
liked_ids = [r['listing_id'] for r in conn.execute(
|
||||||
|
"SELECT listing_id FROM swipes WHERE action='like'")]
|
||||||
|
nov = novelty(rows_by_id, liked_ids,
|
||||||
|
[r['id'] for r in wild_cands])
|
||||||
|
wild_key = lambda r: (
|
||||||
|
-(0.5 * engine.points(r['id']) + 50.0 * nov[r['id']]), r['id'])
|
||||||
|
|
||||||
|
def _wild_preferred(rs):
|
||||||
|
return [r for r in rs
|
||||||
|
if (not r['family'] or r['family'] not in top_fams)
|
||||||
|
and (tastemod.price_band(r['price_num']) is None
|
||||||
|
or tastemod.price_band(r['price_num']) not in top_bands)]
|
||||||
|
|
||||||
|
n_wilds = min(WILDCARDS, max(0, size - len(rets)))
|
||||||
|
wilds = []
|
||||||
|
avail = sorted([r for r in wild_cands if r['id'] not in picked_ids],
|
||||||
|
key=wild_key)
|
||||||
|
preferred = sorted(_wild_preferred(avail), key=wild_key)
|
||||||
|
pref_ids = {r['id'] for r in preferred}
|
||||||
|
for cand in preferred + [r for r in avail if r['id'] not in pref_ids]:
|
||||||
|
if len(wilds) >= n_wilds:
|
||||||
|
break
|
||||||
|
fam = cand['family']
|
||||||
|
if fam and fam_count.get(fam, 0) >= FAMILY_CAP:
|
||||||
|
continue
|
||||||
|
wilds.append(cand)
|
||||||
|
picked_ids.add(cand['id'])
|
||||||
|
if fam:
|
||||||
|
fam_count[fam] = fam_count.get(fam, 0) + 1
|
||||||
|
n_wilds = len(wilds) # pool shortfall reverts to fresh/fill
|
||||||
|
|
||||||
|
# 4) fresh fills the remainder (size - returns - wildcards), never
|
||||||
|
# beyond FRESH_CAP. Wildcards draw from the fresh pool but do NOT
|
||||||
|
# count against the fresh-slot cap (25 - 0 - 2 = 23 new + 2 wild on
|
||||||
|
# a returns-free rich day; 21 new + 2 wild + 2 returns when both
|
||||||
|
# guarantees hold — the sign-off shape). Pool-limited by what the
|
||||||
|
# wildcard pick left in new_cands; a dry wild pool reverts slots.
|
||||||
|
want_new = min(size - len(rets) - len(wilds), FRESH_CAP,
|
||||||
|
max(0, len(new_cands) - len(wilds)))
|
||||||
|
picked_new = pick_new(engine, new_cands, want_new, fam_count, picked_ids)
|
||||||
|
|
||||||
|
# 5) fill returns to size: role order; re-nope cap <=2 total (reserved
|
||||||
|
# included, no surprising gate here); two-pass family cap — prefer
|
||||||
|
# <=4, rescue pass relaxes to <=8 for FILL slots only (fresh picks
|
||||||
|
# and reserved returns keep 4). Deterministic: role order, points
|
||||||
|
# DESC within role.
|
||||||
|
for pass_cap in (FAMILY_CAP, RETURN_FILL_FAMILY_CAP):
|
||||||
|
for cand in ret_ordered:
|
||||||
|
if len(rets) >= size - len(picked_new) - n_wilds:
|
||||||
|
break
|
||||||
|
if cand['id'] in picked_ids:
|
||||||
|
continue
|
||||||
|
fam = cand['family']
|
||||||
|
if fam and fam_count.get(fam, 0) >= pass_cap:
|
||||||
|
continue
|
||||||
|
if cand['verdict'] == NOPE and nope_count >= MAX_RETURN_NOPES:
|
||||||
|
continue
|
||||||
|
rets.append(cand)
|
||||||
|
picked_ids.add(cand['id'])
|
||||||
|
if cand['verdict'] == NOPE:
|
||||||
|
nope_count += 1
|
||||||
|
if fam:
|
||||||
|
fam_count[fam] = fam_count.get(fam, 0) + 1
|
||||||
|
|
||||||
|
# final pinned order: new -> returns -> wildcards
|
||||||
|
ordered = list(picked_new) + rets + wilds
|
||||||
|
|
||||||
|
# price-band repair (new slots only — ordered[:len(picked_new)])
|
||||||
|
n_new = len(picked_new)
|
||||||
|
before = len({tastemod.price_band(r['price_num']) for r in ordered
|
||||||
|
if tastemod.price_band(r['price_num']) is not None})
|
||||||
|
if n_new:
|
||||||
|
ordered[:n_new] = repair_bands(engine, ordered[:n_new], new_cands,
|
||||||
|
fam_count, picked_ids)
|
||||||
|
after = len({tastemod.price_band(r['price_num']) for r in ordered
|
||||||
|
if tastemod.price_band(r['price_num']) is not None})
|
||||||
|
|
||||||
|
recipe = {
|
||||||
|
'new': len(picked_new),
|
||||||
|
'return': len(rets),
|
||||||
|
'wildcard': len(wilds),
|
||||||
|
'bands': before if before == after else after,
|
||||||
|
'caps_applied': {f: fam_count[f] for f in fam_count
|
||||||
|
if fam_count[f] >= FAMILY_CAP},
|
||||||
|
'note': note or ('bands %d->%d repaired'
|
||||||
|
% (before, after) if before != after else ''),
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = conn.execute(
|
||||||
|
"""INSERT INTO sections (status, size, profile_version, recipe,
|
||||||
|
created_at)
|
||||||
|
VALUES ('open', ?, ?, ?, ?)""",
|
||||||
|
(size, profile_version, json.dumps(recipe), _iso(now)))
|
||||||
|
section_id = cur.lastrowid
|
||||||
|
conn.executemany(
|
||||||
|
"""INSERT INTO section_items (section_id, listing_id, slot, position)
|
||||||
|
VALUES (?, ?, ?, ?)""",
|
||||||
|
[(section_id, r['id'], slot, pos)
|
||||||
|
for pos, (r, slot) in enumerate(
|
||||||
|
[(r, 'new') for r in ordered[:n_new]]
|
||||||
|
+ [(r, 'return') for r in rets]
|
||||||
|
+ [(r, 'wildcard') for r in wilds], 1)])
|
||||||
|
conn.commit()
|
||||||
|
return conn.execute('SELECT * FROM sections WHERE id=?',
|
||||||
|
(section_id,)).fetchone()
|
||||||
|
|
||||||
|
|
||||||
|
# --- read / exhaust ----------------------------------------------------------
|
||||||
|
|
||||||
|
def close_section(conn, section, now, status='closed',
|
||||||
|
created_at_col='closed_at'):
|
||||||
|
conn.execute(
|
||||||
|
'UPDATE sections SET status=?, %s=? WHERE id=?' % created_at_col,
|
||||||
|
(status, _iso(now), section['id']))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def section_items(conn, section):
|
||||||
|
return conn.execute(
|
||||||
|
"""SELECT l.*, s.action AS decision, s.last_photo_idx AS last_photo_idx,
|
||||||
|
si.slot, si.position
|
||||||
|
FROM section_items si
|
||||||
|
JOIN listings l ON l.id = si.listing_id
|
||||||
|
LEFT JOIN swipes s ON s.listing_id = l.id
|
||||||
|
WHERE si.section_id = ?
|
||||||
|
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 pool_counts(conn):
|
||||||
|
"""Stock answer to \"can the forge serve anything?\" — fresh / eligible
|
||||||
|
returns / wildcards candidate counts, each from the composer's own
|
||||||
|
candidate SQL. The per-section LAST_TWO exclusion inside compose_section
|
||||||
|
is deliberately NOT applied here: the answer is about stock, not about
|
||||||
|
what one batch will pick (item 8)."""
|
||||||
|
def _count(sql):
|
||||||
|
return conn.execute(
|
||||||
|
'SELECT COUNT(*) AS n FROM (%s)' % sql).fetchone()['n']
|
||||||
|
return {'fresh': _count(NEW_POOL_SQL),
|
||||||
|
'returns': _count(RETURNS_SQL),
|
||||||
|
'wildcards': _count(WILDCARD_SQL)}
|
||||||
|
|
||||||
|
|
||||||
|
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}
|
||||||
|
# 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 action == 'maybe':
|
||||||
|
counts['maybes'] += 1
|
||||||
|
elif action == 'nope':
|
||||||
|
counts['nopes'] += 1
|
||||||
|
restruck = conn.execute(
|
||||||
|
"""SELECT COUNT(DISTINCT e.listing_id) AS n 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 = ? AND si.slot = 'return'""",
|
||||||
|
(section['id'],)).fetchone()['n']
|
||||||
|
wild_hits = conn.execute(
|
||||||
|
"""SELECT COUNT(DISTINCT e.listing_id) AS n 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 = ? AND si.slot = 'wildcard'
|
||||||
|
AND e.action IN ('like','maybe')""",
|
||||||
|
(section['id'],)).fetchone()['n']
|
||||||
|
counts['restruck'] = restruck
|
||||||
|
counts['wildcard_hits'] = wild_hits
|
||||||
|
return counts
|
||||||
|
|
||||||
|
|
||||||
|
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 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()
|
||||||
|
return section
|
||||||
|
|
||||||
|
|
||||||
|
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_ids = section_decided_ids(conn, section)
|
||||||
|
decided = sum(1 for it in items if it['id'] in decided_ids)
|
||||||
|
return {
|
||||||
|
'section': {
|
||||||
|
'id': section['id'],
|
||||||
|
'status': section['status'],
|
||||||
|
'size': section['size'],
|
||||||
|
'profile_version': section['profile_version'],
|
||||||
|
'recipe': json.loads(section['recipe']) if section['recipe']
|
||||||
|
else {},
|
||||||
|
'created_at': section['created_at'],
|
||||||
|
'closed_at': section['closed_at'],
|
||||||
|
},
|
||||||
|
'items': items,
|
||||||
|
'progress': {'decided': decided, 'undecided': len(items) - decided,
|
||||||
|
'total': len(items)},
|
||||||
|
'exhausted': section['status'] != 'open',
|
||||||
|
}
|
||||||
1324
app/selfcheck.py
1324
app/selfcheck.py
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,7 @@
|
|||||||
offline once seen).
|
offline once seen).
|
||||||
- Everything else (shell/navigation): network-first, fall back to cache.
|
- Everything else (shell/navigation): network-first, fall back to cache.
|
||||||
*/
|
*/
|
||||||
const VERSION = 'bike-tinder-v1';
|
const VERSION = 'bike-tinder-v3';
|
||||||
const SHELL = [
|
const SHELL = [
|
||||||
'/',
|
'/',
|
||||||
'/manifest.json',
|
'/manifest.json',
|
||||||
|
|||||||
403
app/taste.py
Normal file
403
app/taste.py
Normal file
@@ -0,0 +1,403 @@
|
|||||||
|
"""Bike-Tinder P3 taste engine — deterministic, pure, stdlib-only.
|
||||||
|
|
||||||
|
Implements docs/RANKING.md "Stage 2 — P3 taste engine":
|
||||||
|
|
||||||
|
- event weights: action (like +1.0 / maybe +0.4 / nope -1.0) x round
|
||||||
|
multiplier (round 1 x1, round >=2 x2) x recency (0.5^(age_days/21)).
|
||||||
|
- feature weights per (feature, value): raw = sum of event weights,
|
||||||
|
shrunk = raw * n/(n+3) with n the number of events in that cell.
|
||||||
|
- taste_raw = sum of shrunk weights over the listing's feature values.
|
||||||
|
- taste_norm = percentile rank of taste_raw among active listings (0-100).
|
||||||
|
- ramp w = min(0.85, N/60) with N = total events; zero events => w = 0.
|
||||||
|
- points = clip(round((1-w)*rubric + w*taste_norm + agent_adj), 0, 100)
|
||||||
|
with agent_adj = sum of matching profile adjustments clamped to +/-10.
|
||||||
|
- band() maps points to a non-numeric tank band: <30 nope / 30-70 maybe / >70 like.
|
||||||
|
|
||||||
|
No randomness, no network. Everything is a pure function of
|
||||||
|
(now, listings, events, profile_payload).
|
||||||
|
"""
|
||||||
|
import bisect
|
||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
import re
|
||||||
|
from collections import Counter
|
||||||
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
|
LIKE, MAYBE, NOPE = 'like', 'maybe', 'nope'
|
||||||
|
ACTIONS = (LIKE, MAYBE, NOPE)
|
||||||
|
|
||||||
|
ACTION_WEIGHT = {LIKE: 1.0, MAYBE: 0.4, NOPE: -1.0}
|
||||||
|
ROUND_CAP = 2 # engine caps round influence at 2
|
||||||
|
HALF_LIFE_DAYS = 21.0
|
||||||
|
RAMP_MAX = 0.85
|
||||||
|
RAMP_DENOM = 60.0
|
||||||
|
ADJ_CLAMP = 10.0
|
||||||
|
|
||||||
|
# --- feature extraction ------------------------------------------------------
|
||||||
|
|
||||||
|
PRICE_BANDS = [
|
||||||
|
(None, 3000, '<3k'),
|
||||||
|
(3000, 5000, '3-5k'),
|
||||||
|
(5000, 8000, '5-8k'),
|
||||||
|
(8000, 12000, '8-12k'),
|
||||||
|
(12000, None, '12k+'),
|
||||||
|
]
|
||||||
|
|
||||||
|
DISP_BANDS = [
|
||||||
|
(None, 400, '<400'),
|
||||||
|
(400, 650, '400-649'),
|
||||||
|
(650, 900, '650-899'),
|
||||||
|
(900, 1250, '900-1249'),
|
||||||
|
(1250, None, '1250+'),
|
||||||
|
]
|
||||||
|
|
||||||
|
YEAR_BANDS = [
|
||||||
|
(None, 2000, '<2000'),
|
||||||
|
(2000, 2010, '2000-2009'),
|
||||||
|
(2010, 2020, '2010-2019'),
|
||||||
|
(2020, None, '2020+'),
|
||||||
|
]
|
||||||
|
|
||||||
|
KM_BANDS = [
|
||||||
|
(None, 10000, '<10k'),
|
||||||
|
(10000, 25000, '10-25k'),
|
||||||
|
(25000, 60000, '25-60k'),
|
||||||
|
(60000, None, '60k+'),
|
||||||
|
]
|
||||||
|
|
||||||
|
SILHOUETTES = ['bobber', 'custom', 'classic', 'cruiser']
|
||||||
|
|
||||||
|
COLORS = [
|
||||||
|
('red', 'red'),
|
||||||
|
('black', 'black'),
|
||||||
|
('white', 'white'),
|
||||||
|
('blue', 'blue'),
|
||||||
|
('green', 'green'),
|
||||||
|
('silver|grey|gray', 'silver'),
|
||||||
|
('orange', 'orange'),
|
||||||
|
('yellow', 'yellow'),
|
||||||
|
('brown|maroon', 'brown'),
|
||||||
|
('tan', 'tan'),
|
||||||
|
('gold', 'gold'),
|
||||||
|
]
|
||||||
|
|
||||||
|
_CC_RE = re.compile(r'(\d[\d,]*)\s*cc\b', re.IGNORECASE)
|
||||||
|
|
||||||
|
|
||||||
|
def band_for(sorted_edges, value):
|
||||||
|
"""Find the containing band (edges [(lo, hi, label)]); value None -> None."""
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
for lo, hi, label in sorted_edges:
|
||||||
|
if (lo is None or value >= lo) and (hi is None or value < hi):
|
||||||
|
return label
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def price_band(price_num):
|
||||||
|
return band_for(PRICE_BANDS, price_num)
|
||||||
|
|
||||||
|
|
||||||
|
def disp_cc(title, desc):
|
||||||
|
m = _CC_RE.search('%s %s' % (title or '', desc or ''))
|
||||||
|
if not m:
|
||||||
|
return None
|
||||||
|
return int(m.group(1).replace(',', ''))
|
||||||
|
|
||||||
|
|
||||||
|
def disp_band(cc):
|
||||||
|
return band_for(DISP_BANDS, cc)
|
||||||
|
|
||||||
|
|
||||||
|
def year_band(year):
|
||||||
|
return band_for(YEAR_BANDS, year)
|
||||||
|
|
||||||
|
|
||||||
|
def km_band(km):
|
||||||
|
return band_for(KM_BANDS, km)
|
||||||
|
|
||||||
|
|
||||||
|
def extract_features(row):
|
||||||
|
"""Return the listing's list of (dimension, value) feature cells.
|
||||||
|
|
||||||
|
Missing values yield no cell (neutral per RANKING.md).
|
||||||
|
"""
|
||||||
|
title = row['title'] or ''
|
||||||
|
desc = (row['desc'] or '').split(' [also listed:')[0]
|
||||||
|
text = (title + ' ' + desc).lower()
|
||||||
|
feats = []
|
||||||
|
|
||||||
|
if row['family']:
|
||||||
|
feats.append(('family', row['family']))
|
||||||
|
|
||||||
|
pb = price_band(row['price_num'])
|
||||||
|
if pb:
|
||||||
|
feats.append(('price_band', pb))
|
||||||
|
|
||||||
|
db_ = disp_band(disp_cc(title, desc))
|
||||||
|
if db_:
|
||||||
|
feats.append(('displacement', db_))
|
||||||
|
|
||||||
|
yb = year_band(row['year'])
|
||||||
|
if yb:
|
||||||
|
feats.append(('year', yb))
|
||||||
|
|
||||||
|
kb = km_band(row['km'])
|
||||||
|
if kb:
|
||||||
|
feats.append(('km', kb))
|
||||||
|
|
||||||
|
extras = row['extras']
|
||||||
|
if isinstance(extras, str):
|
||||||
|
try:
|
||||||
|
extras = json.loads(extras)
|
||||||
|
except ValueError:
|
||||||
|
extras = []
|
||||||
|
for label in extras or []:
|
||||||
|
feats.append(('extras', label))
|
||||||
|
|
||||||
|
if row['seller_type'] in ('dealer', 'private'):
|
||||||
|
feats.append(('seller', row['seller_type']))
|
||||||
|
|
||||||
|
for pat in SILHOUETTES:
|
||||||
|
if re.search(r'\b%s\b' % pat, text):
|
||||||
|
feats.append(('silhouette', pat))
|
||||||
|
|
||||||
|
for pat, label in COLORS:
|
||||||
|
if re.search(r'\b%s\b' % pat, text):
|
||||||
|
feats.append(('color', label))
|
||||||
|
|
||||||
|
if row['region'] in ('ns', 'nb'):
|
||||||
|
feats.append(('region', row['region']))
|
||||||
|
|
||||||
|
return feats
|
||||||
|
|
||||||
|
|
||||||
|
FEATURE_DIMS = ('family', 'price_band', 'displacement', 'year', 'km',
|
||||||
|
'extras', 'seller', 'silhouette', 'color', 'region')
|
||||||
|
|
||||||
|
|
||||||
|
def family_short(family):
|
||||||
|
"""Compact family label for display (' — ' suffix trimmed)."""
|
||||||
|
if not family:
|
||||||
|
return family
|
||||||
|
return family.split(' — ')[0].strip()
|
||||||
|
|
||||||
|
|
||||||
|
# --- datetime handling -------------------------------------------------------
|
||||||
|
|
||||||
|
def parse_ts(value, now):
|
||||||
|
"""Parse an event created_at ('...T...Z' ISO or 'YYYY-MM-DD HH:MM:SS'
|
||||||
|
sqlite default, both UTC) into a naive-UTC datetime; failures -> None."""
|
||||||
|
del now # kept in the signature for call sites
|
||||||
|
base = (value or '').strip()
|
||||||
|
if not base:
|
||||||
|
return None
|
||||||
|
if base.endswith('Z'):
|
||||||
|
base = base[:-1]
|
||||||
|
try:
|
||||||
|
return datetime.datetime.fromisoformat(base)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def event_weight(action, round_, created_at, now):
|
||||||
|
"""Weight of one swipe event: action x round x recency."""
|
||||||
|
w = ACTION_WEIGHT.get(action, 0.0)
|
||||||
|
r = ROUND_CAP if (round_ or 1) >= ROUND_CAP else (round_ or 1)
|
||||||
|
if r >= 2:
|
||||||
|
w *= 2.0
|
||||||
|
ts = parse_ts(created_at, now)
|
||||||
|
if ts is not None:
|
||||||
|
age_days = max(0.0, (now - ts).total_seconds() / 86400.0)
|
||||||
|
w *= math.pow(0.5, age_days / HALF_LIFE_DAYS)
|
||||||
|
return w
|
||||||
|
|
||||||
|
|
||||||
|
# --- the engine --------------------------------------------------------------
|
||||||
|
|
||||||
|
class TasteEngine:
|
||||||
|
"""Compute points / breakdown / tank for each listing from events.
|
||||||
|
|
||||||
|
listings: rows (sqlite.Row or dict) with keys id, title, desc, family,
|
||||||
|
score, price_num, km, year, seller_type, extras, region.
|
||||||
|
events: rows with keys listing_id, action, round, created_at.
|
||||||
|
profile: taste_profile payload dict (adjustments key used) or None.
|
||||||
|
now: naive-UTC datetime for recency (default: datetime.utcnow()).
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
listings: List[Dict[str, Any]],
|
||||||
|
events: List[Dict[str, Any]],
|
||||||
|
profile: Optional[Dict[str, Any]] = None,
|
||||||
|
now: Optional[datetime.datetime] = None,
|
||||||
|
) -> None:
|
||||||
|
self.now = now or datetime.datetime.now(datetime.timezone.utc).replace(
|
||||||
|
tzinfo=None)
|
||||||
|
self.listings: List[Dict[str, Any]] = listings
|
||||||
|
self.profile: Dict[str, Any] = profile or {}
|
||||||
|
self.adjustments: List[Dict[str, Any]] = (
|
||||||
|
self.profile.get('adjustments') or [])
|
||||||
|
# listing_id -> row (also covers listings reachable only via events)
|
||||||
|
self.by_id: Dict[int, Dict[str, Any]] = {
|
||||||
|
r['id']: r for r in self.listings}
|
||||||
|
# feature cells per listing id
|
||||||
|
self.cells: Dict[int, List[Tuple[str, str]]] = {
|
||||||
|
r['id']: extract_features(r) for r in self.listings}
|
||||||
|
# events -> weights, aggregated per (dim, value)
|
||||||
|
self.events = list(events)
|
||||||
|
cells_raw = {}
|
||||||
|
cells_n = {}
|
||||||
|
for ev in self.events:
|
||||||
|
lid = ev['listing_id']
|
||||||
|
w = event_weight(ev['action'], ev['round'], ev['created_at'],
|
||||||
|
self.now)
|
||||||
|
for dim, val in self.cells.get(lid, []):
|
||||||
|
key = (dim, val)
|
||||||
|
cells_raw[key] = cells_raw.get(key, 0.0) + w
|
||||||
|
cells_n[key] = cells_n.get(key, 0) + 1
|
||||||
|
self.cells_raw = cells_raw
|
||||||
|
self.cells_n = cells_n
|
||||||
|
self.event_counts = Counter(ev['listing_id'] for ev in self.events)
|
||||||
|
self._norm_cache = None
|
||||||
|
|
||||||
|
def round_next(self, listing_id):
|
||||||
|
"""Round the NEXT event on this listing would get, capped at 2."""
|
||||||
|
return min(self.event_counts.get(listing_id, 0) + 1, 2)
|
||||||
|
|
||||||
|
# -- aggregates -----------------------------------------------------------
|
||||||
|
|
||||||
|
@property
|
||||||
|
def n_events(self):
|
||||||
|
return len(self.events)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def w(self):
|
||||||
|
"""Taste share of the blend (ramp)."""
|
||||||
|
return min(RAMP_MAX, self.n_events / RAMP_DENOM)
|
||||||
|
|
||||||
|
def shrunk(self, dim, val):
|
||||||
|
raw = self.cells_raw.get((dim, val), 0.0)
|
||||||
|
n = self.cells_n.get((dim, val), 0)
|
||||||
|
return raw * (n / (n + 3.0)) if n else 0.0
|
||||||
|
|
||||||
|
def family_net(self, family):
|
||||||
|
"""Net raw weight + nope count for a family label (all events)."""
|
||||||
|
raw = 0.0
|
||||||
|
nopes = 0
|
||||||
|
for ev in self.events:
|
||||||
|
row = self.by_id.get(ev['listing_id'])
|
||||||
|
if row and row['family'] == family and family:
|
||||||
|
raw += event_weight(ev['action'], ev['round'],
|
||||||
|
ev['created_at'], self.now)
|
||||||
|
if ev['action'] == NOPE:
|
||||||
|
nopes += 1
|
||||||
|
return raw, nopes
|
||||||
|
|
||||||
|
# -- per-listing scores ---------------------------------------------------
|
||||||
|
|
||||||
|
def taste_raw(self, listing_id):
|
||||||
|
return sum(self.shrunk(d, v) for d, v in self.cells.get(listing_id, []))
|
||||||
|
|
||||||
|
def taste_norm(self, listing_id):
|
||||||
|
"""Median-rank percentile (0-100) among active listings."""
|
||||||
|
if self._norm_cache is None:
|
||||||
|
raws = {}
|
||||||
|
for lid, cells in self.cells.items():
|
||||||
|
raws[lid] = sum(self.shrunk(d, v) for d, v in cells)
|
||||||
|
ranked = sorted(raws.values())
|
||||||
|
n = len(ranked)
|
||||||
|
self._norm_cache = {}
|
||||||
|
for lid, r in raws.items():
|
||||||
|
less = bisect.bisect_left(ranked, r)
|
||||||
|
eq = bisect.bisect_right(ranked, r) - less
|
||||||
|
pct = 100.0 * (less + 0.5 * eq) / n if n else 50.0
|
||||||
|
self._norm_cache[lid] = pct
|
||||||
|
return self._norm_cache.get(listing_id, 50.0)
|
||||||
|
|
||||||
|
def agent_adj(self, listing_id):
|
||||||
|
"""Sum of matching profile adjustments, clamped to +/-10."""
|
||||||
|
total = 0.0
|
||||||
|
cells = {d: set() for d, _ in self.cells.get(listing_id, [])}
|
||||||
|
for d, v in self.cells.get(listing_id, []):
|
||||||
|
cells[d].add(v)
|
||||||
|
for adj in self.adjustments:
|
||||||
|
dim = adj.get('feature')
|
||||||
|
val = adj.get('value')
|
||||||
|
delta = adj.get('delta')
|
||||||
|
if dim not in FEATURE_DIMS:
|
||||||
|
continue
|
||||||
|
if not isinstance(delta, (int, float)):
|
||||||
|
continue
|
||||||
|
if val in cells.get(dim, set()):
|
||||||
|
total += float(delta)
|
||||||
|
return max(-ADJ_CLAMP, min(ADJ_CLAMP, total))
|
||||||
|
|
||||||
|
def points(self, listing_id):
|
||||||
|
row = self.by_id.get(listing_id)
|
||||||
|
if row is None:
|
||||||
|
return None
|
||||||
|
pts = (1.0 - self.w) * (row['score'] or 0)
|
||||||
|
pts += self.w * self.taste_norm(listing_id)
|
||||||
|
pts += self.agent_adj(listing_id)
|
||||||
|
return int(max(0, min(100, round(pts))))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def band(points):
|
||||||
|
if points is None:
|
||||||
|
return None
|
||||||
|
if points < 30:
|
||||||
|
return NOPE
|
||||||
|
if points <= 70:
|
||||||
|
return MAYBE
|
||||||
|
return LIKE
|
||||||
|
|
||||||
|
def tank(self, listing_id):
|
||||||
|
pts = self.points(listing_id)
|
||||||
|
band = self.band(pts)
|
||||||
|
return {'value': pts, 'band': band}
|
||||||
|
|
||||||
|
# -- explainability -------------------------------------------------------
|
||||||
|
|
||||||
|
def contributions(self, listing_id):
|
||||||
|
"""[(display_label, shrunk_weight, dim, val)] sorted by |w| DESC."""
|
||||||
|
out = []
|
||||||
|
for d, v in self.cells.get(listing_id, []):
|
||||||
|
s = self.shrunk(d, v)
|
||||||
|
if not s:
|
||||||
|
continue
|
||||||
|
label = ('%s %s' % (d, v))
|
||||||
|
if d == 'family':
|
||||||
|
label = family_short(v) + ' family'
|
||||||
|
out.append((label, s, d, v))
|
||||||
|
out.sort(key=lambda t: abs(t[1]), reverse=True)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def breakdown(self, listing_id):
|
||||||
|
"""Source-labeled breakdown lines for the dossier."""
|
||||||
|
row = self.by_id.get(listing_id)
|
||||||
|
lines = []
|
||||||
|
# issue #3: a section can outlive an ad. A listing that left the
|
||||||
|
# active pool mid-batch (and has no swipe history) is not in
|
||||||
|
# by_id; its breakdown degrades to no-signal lines instead of a
|
||||||
|
# TypeError that would 500 the whole section render.
|
||||||
|
if row is not None:
|
||||||
|
lines.append({'source': 'rubric', 'label': 'rubric fit',
|
||||||
|
'value': int(row['score'] or 0)})
|
||||||
|
for label, s, _d, _v in self.contributions(listing_id)[:3]:
|
||||||
|
lines.append({'source': 'taste', 'label': label,
|
||||||
|
'value': round(s, 1)})
|
||||||
|
for adj in self.adjustments:
|
||||||
|
dim = adj.get('feature')
|
||||||
|
val = adj.get('value')
|
||||||
|
delta = adj.get('delta')
|
||||||
|
if dim not in FEATURE_DIMS or not isinstance(delta, (int, float)):
|
||||||
|
continue
|
||||||
|
if val in {v for d, v in self.cells.get(listing_id, [])
|
||||||
|
if d == dim}:
|
||||||
|
why = adj.get('why') or ('%s %s' % (dim, val))
|
||||||
|
lines.append({'source': 'agent', 'label': why,
|
||||||
|
'value': round(float(delta), 1)})
|
||||||
|
return lines
|
||||||
@@ -1,15 +1,18 @@
|
|||||||
# Bike-Tinder app container (Phase 2 build).
|
# Bike-Tinder app container (Phase 4 deploy).
|
||||||
# The compose stack, systemd unit and Tailscale sidecar belong to the parent —
|
# The compose stack, systemd unit and Tailscale sidecar live beside this file —
|
||||||
# this file is only the app image contract.
|
# this file is only the app image contract.
|
||||||
FROM python:3.13-slim
|
FROM python:3.14-slim
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY requirements.txt .
|
COPY requirements.txt ./
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
COPY app/ ./app/
|
COPY app/ ./app/
|
||||||
|
|
||||||
# Data lands on the stack volume: /data/bike-tinder.sqlite3 and /data/img/
|
# Data lands on the stack volume: /data/bike-tinder.sqlite3 and /data/img/.
|
||||||
|
# /state is the hunt state dir (bind-mounted read-only) used by POST /api/import.
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
CMD ["python", "-m", "app.serve", "--host", "0.0.0.0", "--port", "8000", "--db", "/data/bike-tinder.sqlite3", "--img-dir", "/data/img"]
|
CMD ["python", "-m", "app.serve", "--host", "0.0.0.0", "--port", "8000", \
|
||||||
|
"--db", "/data/bike-tinder.sqlite3", "--img-dir", "/data/img", \
|
||||||
|
"--state-dir", "/state"]
|
||||||
|
|||||||
64
deploy/docker-compose.yml
Normal file
64
deploy/docker-compose.yml
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
# Bike-Tinder stack — deck app + Tailscale sidecar.
|
||||||
|
#
|
||||||
|
# Deployed on Rotarran at /var/mnt/DATA/podman-stacks/bike-tinder/ (a checkout
|
||||||
|
# of the `release` branch). Run via the user unit podman-stack-bike-tinder.service
|
||||||
|
# (Delegate=yes) or manually:
|
||||||
|
# podman-compose -f deploy/docker-compose.yml --env-file .env up -d
|
||||||
|
#
|
||||||
|
# Networking:
|
||||||
|
# - app: published on 127.0.0.1:9453 only — Hermes cron triggers (import /
|
||||||
|
# agent read) and local debugging. The LAN sees nothing.
|
||||||
|
# - tailscale (userspace mode, no tun/caps): joins the tailnet as `biketinder`
|
||||||
|
# and serves https://biketinder.tailf35a34.ts.net -> http://app:8000 (443).
|
||||||
|
# Secrets: TS_AUTHKEY + OPENROUTER_API_KEY come from the stack-local .env
|
||||||
|
# (never committed). Before the authkey is placed, the sidecar simply reports
|
||||||
|
# "no authkey" and exits — the app runs fine on its own.
|
||||||
|
|
||||||
|
x-logging: &default-logging
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-size: "50m"
|
||||||
|
max-file: "5"
|
||||||
|
compress: "true"
|
||||||
|
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: ..
|
||||||
|
dockerfile: deploy/Dockerfile
|
||||||
|
image: localhost/bike-tinder:latest
|
||||||
|
container_name: bike-tinder-app
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
- TZ=America/Halifax
|
||||||
|
- OPENROUTER_API_KEY=${OPENROUTER_API_KEY:-}
|
||||||
|
volumes:
|
||||||
|
- /var/mnt/DATA/podman-stacks/bike-tinder/data:/data
|
||||||
|
- /var/home/admin/.hermes/scripts/motorcycle-hunt.d:/state:ro,z
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:9453:8000"
|
||||||
|
stop_grace_period: 15s
|
||||||
|
security_opt:
|
||||||
|
- no-new-privileges:true
|
||||||
|
logging:
|
||||||
|
<<: *default-logging
|
||||||
|
|
||||||
|
tailscale:
|
||||||
|
image: docker.io/tailscale/tailscale:latest
|
||||||
|
container_name: bike-tinder-ts
|
||||||
|
hostname: biketinder
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
- app
|
||||||
|
environment:
|
||||||
|
- TS_AUTHKEY=${TS_AUTHKEY:-}
|
||||||
|
- TS_HOSTNAME=biketinder
|
||||||
|
- TS_STATE_DIR=/var/lib/tailscale
|
||||||
|
- TS_SERVE_CONFIG=/config/serve.json
|
||||||
|
- TS_USERSPACE=true
|
||||||
|
- TZ=America/Halifax
|
||||||
|
volumes:
|
||||||
|
- /var/mnt/DATA/podman-stacks/bike-tinder/tailscale-state:/var/lib/tailscale
|
||||||
|
- /var/mnt/DATA/podman-stacks/bike-tinder/deploy/tailscale:/config:ro
|
||||||
|
logging:
|
||||||
|
<<: *default-logging
|
||||||
18
deploy/podman-stack-bike-tinder.service
Normal file
18
deploy/podman-stack-bike-tinder.service
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# /var/home/admin/.config/systemd/user/podman-stack-bike-tinder.service
|
||||||
|
# (installed from the repo: deploy/podman-stack-bike-tinder.service)
|
||||||
|
[Unit]
|
||||||
|
Description=Bike-Tinder podman-compose stack (deck app + Tailscale sidecar)
|
||||||
|
Wants=network-online.target
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
RemainAfterExit=yes
|
||||||
|
Delegate=yes
|
||||||
|
WorkingDirectory=/var/mnt/DATA/podman-stacks/bike-tinder
|
||||||
|
ExecStart=/var/home/admin/.local/bin/podman-compose -f deploy/docker-compose.yml --env-file .env up -d
|
||||||
|
ExecStop=/var/home/admin/.local/bin/podman-compose -f deploy/docker-compose.yml --env-file .env down
|
||||||
|
TimeoutStartSec=600
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
10
deploy/tailscale/serve.json
Normal file
10
deploy/tailscale/serve.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"TCP": { "443": { "HTTPS": true } },
|
||||||
|
"Web": {
|
||||||
|
"${TS_CERT_DOMAIN}:443": {
|
||||||
|
"Handlers": {
|
||||||
|
"/": { "Proxy": "http://app:8000" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
80
docs/BACKLOG.md
Normal file
80
docs/BACKLOG.md
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
# BACKLOG — Bike-Tinder
|
||||||
|
|
||||||
|
Glenn's "later versions" idea queue. He drops ideas in conversation; they land here (near-verbatim + a scope sketch + open questions), and get built in a future version — never mid-phase. Newest first.
|
||||||
|
|
||||||
|
## v1.1 — QOL wishlist (drafted 2026-11-12; feasibility triaged same day; build awaits Glenn's go-ahead)
|
||||||
|
|
||||||
|
Glenn's words: *"Going to go through a laundry list of fixes before we do lets say 1.1?"* — nothing here is started.
|
||||||
|
|
||||||
|
### Bugs & dry-catalog messaging (reported live 2026-11-12 evening)
|
||||||
|
|
||||||
|
Glenn's words: *"I just got a new batch and its marked as 2/2. When I go to the next, its 3/2… I don't believe it's counting the entries correctly. Furthermore, I'm only again getting 2 bikes at a time… I feel I should be getting batches of 25 at a time?"*
|
||||||
|
|
||||||
|
- **Counter bug (diagnosed in code 2026-11-12):** the pill shows `decided/total` for the current section. **Returns arrive wearing their previous decision** (the section payload joins `swipes.action`), so a fresh second-looks-only batch instantly counts as fully decided — a new batch paints "2/2" before it is touched, and the hot/complete state fires prematurely. Swiping then increments optimistically on top (`sectDecided++`), so the count runs past the total — "3/2", "4/2". **Fix sketch:** count a card as decided only if decided *during this section* (server per-item flag from events stamped with this section_id — the source `section_counts` already uses); clamp to total; keep the hot state off until a real in-section decision exists.
|
||||||
|
- **Dry-catalog messaging (pulled forward from the Dry-pool UX note below):** when the fresh pool is 0 the deck silently degrades to returns-only sections (2–3 cards: ≤5 returns budget, one-shot per item, family caps) — by design but confusing. v1.1: surface the state ("Catalog fully reviewed — fresh listings arrive with the next 08:30 import; showing second looks") whenever a composed/forged batch reports `new: 0`, and label forge results accordingly so it never silently drips 2 cards again.
|
||||||
|
|
||||||
|
### Batch formula v2 + fresh counter + empty state + Second Look pop (requested live 2026-11-12 evening, round 2)
|
||||||
|
|
||||||
|
Glenn's words: *"Alright lets add a new counter to the top of 'bikes lef' or something to indicate how many bikes are fresh. I think if there is no more new bikes, the empty page (all bikes gone through) should say 'no more new bikes!'. Maybe there is already but I didn't read it. Make the UI more pronounced so I can see it clearly. One more thing, I think there should be 25 bikes at a time, min 2 of second look. So say there is 5 bikes left, those 5 fresh bikes are listed, with the rest being second looks. Ok last thing, the Second Look indicator, make it more pronounced visually. Change the color a bit so it pops a bit more."*
|
||||||
|
|
||||||
|
- **Fresh counter (header):** new chip in the top header showing **fresh (undecided) bikes left** — extend `/api/stats` with `fresh` (active listings with no swipe — the exact NEW_POOL predicate). Header pill gains `<b>K</b> fresh`. When 0, the state of the hunt reads at a glance.
|
||||||
|
- **Empty state, loud:** the exhausted screen becomes prominent **"No more new bikes!"** (today `.empty` says a quiet "You're all caught up" and the report screen can mask it in section mode). Show whenever the deck can serve *nothing* at all (no fresh, no eligible second looks, no wildcards); keep "New bikes arrive nightly — next import 08:30".
|
||||||
|
- **Batch formula v2** *(amends the frozen ≤5-returns rule — build-time UI-SPEC/P3 note):* every batch targets **25 cards**. Fresh first (up to 23 slots); **≥2 second looks per batch whenever eligible stock exists** *(reading Glenn's "min 2": a full-fresh day = 23 fresh + 2 second looks; veto line if he wants pure-fresh instead)*; when fresh < 23, second looks **fill the remainder up to 25** (his example: 5 fresh + 20 second looks); leftover fresh rolls to the next batch. Limits kept: ≤2 re-noped returns per batch; family cap relaxes for fill-returns when needed (build detail); returns stay one-shot. Fresh exhausted → second-looks-only batches; returns exhausted → fresh-only batches; both → the loud empty state.
|
||||||
|
- *Noted consequence:* with fill-to-25, the one-shot second-look pool (~30 remaining as of tonight) burns through in a batch or two — after that only nightly imports feed the deck. Re-offer policy (e.g. re-eligible after everything's had its turn) stays queued (option (c) below); revisit after living with v2.
|
||||||
|
- **Second Look band pops:** `.band.sl` is currently near-invisible (white at ~10% on dark); re-tint to a distinct **violet accent** (bold fill + border) on card face + info view. Wildcard stays amber for contrast.
|
||||||
|
- **UI-SPEC amendments at build:** §11 tap flow (already queued) + `.band.sl` palette + empty-state copy + header fresh counter.
|
||||||
|
- **Glenn's sign-offs (2026-11-12, live):** ≥2-second-looks default CONFIRMED (rich-day deck = 21 fresh + 2 second looks + 2 wildcards) · violet band CONFIRMED · Backspace kept as undo alias CONFIRMED. **Build phase AUTHORIZED** the same day — implementation spec = `docs/V1.1-BUILD.md`.
|
||||||
|
|
||||||
|
### Shortcuts
|
||||||
|
- Arrow keys: Left = Nope, Right = Like, Up = Maybe, **Down = back one** *(read as: undo the last swipe — confirm at spec time)*. **Esc** = exit fullscreen views.
|
||||||
|
- Feasibility: trivial — keyboard invokes the exact actions gestures/buttons already call (undo exists). Care: ignore keys while a text input has focus; prevent arrow-key page scroll. Desktop/Bluetooth-keyboard benefit only; the app is reachable from any tailnet device (MacBook included).
|
||||||
|
|
||||||
|
### UI changes
|
||||||
|
1. **Tap flow — info first, then photos.** Center tap opens the fullscreen **bike data view** (today it opens the photo); tapping the photo there opens the fullscreen **image viewer**. Side taps keep flipping photos on the card. Esc/back pops viewer → data → deck. Resolves "can't get info unless I hit the ⓘ button." Revisit `UI-SPEC.md` §11 tap map before build (v1.1 spec bump).
|
||||||
|
2. **Photo fit: fill → contain, option C chosen.** Show photos uncropped — no "zoomed" crops. **Decided 2026-11-12 (Glenn: "OH C! I love C!"): option C — fit + blurred backdrop (whole photo over a soft blur of the same image) on the card AND the info-page hero.** Fullscreen viewer already fits (unchanged); list thumbs stay fill (56px, irrelevant). Demo reference: `~/dev/bike-tinder-demos/fit-vs-fill.html` (panel C).
|
||||||
|
3. **Rubric display goes minimal.** Show ONLY the gas-tank gauge + percent full (e.g. "76%"); drop the rest of the rubric-fit display in bike views — **plus a setting that re-enables the full "why" breakdown for debugging** (Glenn, 2026-11-12; wire via the existing `prefs` key/value API — same pattern as `forge_mode`).
|
||||||
|
|
||||||
|
## v1.2 — Analytics suite (version split set with Glenn 2026-11-12)
|
||||||
|
|
||||||
|
Glenn's words: *"take in just about all interactions… how long I have looked at each picture, how many pictures I flipped through, how quickly I swiped… kept on an individual bike perspective and fed to the model for further accuracy"* + *"start the database from scratch, giving myself a new start."*
|
||||||
|
|
||||||
|
- **Capture (local-only, per bike):** dwell time per photo, flips (count/order), decision latency (card visible → swipe), maybe/undo/list-edit events, session id; card-dwell vs fullscreen-dwell as separate signals.
|
||||||
|
- **Store:** new append-only `interaction_events` table (same pattern as `swipe_events`); batched client flush (`sendBeacon`/pagehide); iOS PWA: pause dwell clocks on `visibilitychange` so background time can't pollute.
|
||||||
|
- **Feed the model:** nightly agent pass consumes per-bike aggregates as extra taste features. HARD LAW unchanged: no LLM in the swipe path — capture is write-only until the nightly read.
|
||||||
|
- **Reset:** ships with v1.2, paired with a from-scratch DB reset so telemetry starts clean on the new ledger. Scope to confirm: wipe swipes + events + profile only (listings/catalog stay — re-import is idempotent); legacy data stays in `data/backup/` archives. (v1.1 does NOT reset anything — swipe history survives it untouched.)
|
||||||
|
- **Open questions:** reset scope; raw events forever vs aggregate-and-prune; include list-browsing events; surface any of it in the UI (probably not).
|
||||||
|
|
||||||
|
## Gallery mirror gap (found 2026-11-12, filing for a later phase)
|
||||||
|
|
||||||
|
Verified while preparing the fit/fill demo: the local image mirror (`data/img/{id}.webp`) holds **primaries only**. Gallery photos beyond the first (`/img/{id}?i≥1`) serve a **302 redirect to the Kijiji CDN** — not hedged locally. If an ad is deleted we keep photo 1, but the gallery (often where the good shots are) is gone. Fix options for a later phase: mirror galleries on import/first-touch (estimates in the B1 notes: roughly +350–600 MB for the full catalog — fine for the mirror volume), keep the redirect as fallback. Not urgent; filed so it is not forgotten.
|
||||||
|
|
||||||
|
## Dry-pool UX + agent feedback (noted 2026-11-12)
|
||||||
|
|
||||||
|
Glenn hit a fully-reviewed catalog: 563/563 actives decided → fresh pool = 0 → forges returned 2-card returns-only sections ("stuck on a couple bikes"), and "Run agent now" looked inert (a pass ran, but the deck can't change without fresh listings). Possible polish: (a) report/forge state says "catalog fully reviewed — fresh listings arrive with the next import" instead of silently dripping 2 cards; (b) agent button shows a richer confirmation (profile version + lean delta); (c) re-open "second look" eligibility or widen the wildcard pool when the catalog is dry; (d) a "review your Maybes" shortcut at exhaustion. Related: the nightly import + nightly agent read make the pool self-refreshing (Phase 4).
|
||||||
|
|
||||||
|
**Pulled into v1.1 (2026-11-12):** option (a) — dry-state messaging — is now part of the v1.1 scope (see Bugs & dry-catalog messaging above); options (b)/(c)/(d) remain queued for a later version.
|
||||||
|
|
||||||
|
## B1 · Photo experience v2 — multi-image galleries, photo swipe, fullscreen viewer
|
||||||
|
|
||||||
|
**Queued 2026-11-10** (during the P3 build; for the version after it). **PULLED FORWARD 2026-11-10; SHIPPED 2026-11-11 — built, verified (walk 390/390, selfcheck 31/31), LIVE on :9453; see `PLAN.md`.** Spec frozen in `UI-SPEC.md` §11 + `DATA-MODEL.md` B1 additions.
|
||||||
|
|
||||||
|
Glenn's words: *"more pictures! As many pictures as we can get honestly. I'd like to swipe through them left and right like tinder does. One repeats, the image should change as to not tip me off."* · *"I'd like to tap the image to 'full screen' it so I can see it better."*
|
||||||
|
|
||||||
|
Scope sketch:
|
||||||
|
|
||||||
|
1. **Capture every gallery photo.** Today the pipeline keeps ONE image per listing. Verified 2026-11-10: Kijiji detail pages expose the full gallery (probe — 2026 Sport Scout page: 8 unique images; Scout Sixty Bobber: 3). The image tool already opens each listing's page when filling photos — it just keeps the first image. Extension = grab all unique gallery images (dedupe by image uuid, keep page order). State gains `imgs: [...]` beside `img`; DB carries the photo list; the mirror downloads all (sizing rule unchanged: `?rule=kijijica-640-webp`). Storage estimate: ~640 listings × ~5–10 photos × ~70–120 KB ≈ **350–600 MB** — fine for the mirror.
|
||||||
|
2. **Photo swipe on the card** — Tinder-style: tap-left / tap-right zones advance photos (segment indicators on the photo's top edge); horizontal drag stays LIKE/NOPE — the no-lock law outranks everything.
|
||||||
|
3. **Fullscreen viewer** — tap the image → fullscreen photo (pinch-zoom, swipe through the gallery, pull/✕ to close); works from the deck card and the dossier hero.
|
||||||
|
4. **Repeat handling — "the image should change so it doesn't tip me off."** Two readings to settle at spec time with Glenn:
|
||||||
|
- (a) when a card repeats (second look / re-listed ad), lead with a *different* photo than the one previously shown, so the repeat doesn't read as déjà vu;
|
||||||
|
- (b) dedupe/rotate so the same photo never repeats twice in a row within or across a swipe session.
|
||||||
|
Both are cheap once galleries exist; ask which was meant at spec time.
|
||||||
|
5. **Gesture-map tension to resolve at spec:** today a tap on the card opens the dossier, and Glenn now wants tap = fullscreen; Tinder's model taps to advance photos. The exact zone map (tap-left/right = photo, center/ⓘ = dossier vs fullscreen) is a spec-time design session with Glenn. Dossier remains reachable from ⓘ and list rows either way.
|
||||||
|
|
||||||
|
> **Settled 2026-11-10 (Glenn, in chat):** gesture map = tap sides flip photos · tap center fullscreen · ⓘ details. Repeat handling = lead with a *different* photo each time (rotation). Frozen in `UI-SPEC.md` §11.
|
||||||
|
|
||||||
|
Not for P3 — nothing here blocks or alters the current phase. Forward-compatible: gallery segment indicators and the P3 card bands (§10.3) must share the card's top area; the stack/drag engine is untouched.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*Other v2+ seeds already live where they were planted:* distance feature (`RANKING.md` open decisions) · `AUTH_TOKEN` hardening (`DESIGN.md` security) · Facebook paste-the-link box (`SOURCES.md`) · weekly digest, "hide this seller" (`RANKING.md` / `DESIGN.md` risk notes).
|
||||||
@@ -105,3 +105,74 @@ LIMIT :deck_size;
|
|||||||
- **Badge:** `prefs['badge_seen_at']` (ISO). `new` = active rows with `first_seen` > COALESCE(badge_seen_at,'0'); `POST /api/badge/seen` stamps now.
|
- **Badge:** `prefs['badge_seen_at']` (ISO). `new` = active rows with `first_seen` > COALESCE(badge_seen_at,'0'); `POST /api/badge/seen` stamps now.
|
||||||
- **Media mirror:** files at `data/img/{listing_id}.webp`; `GET /img/{id}` → file (200) → `img_url` (302) → 404 (UI placeholder). Downloads via `app/imgfetch.py` (polite: ~1 req/s, skip-existing, resumable). `img_url` enrichment happens upstream in the hunt state (`motorcycle-hunt-img.py`; atomic writes; rescans keep curated imgs) and lands via import.
|
- **Media mirror:** files at `data/img/{listing_id}.webp`; `GET /img/{id}` → file (200) → `img_url` (302) → 404 (UI placeholder). Downloads via `app/imgfetch.py` (polite: ~1 req/s, skip-existing, resumable). `img_url` enrichment happens upstream in the hunt state (`motorcycle-hunt-img.py`; atomic writes; rescans keep curated imgs) and lands via import.
|
||||||
- **Dedupe refinement (stretch, targeted):** for cross-province groups with >2 members, replace collapse-to-one with `min(ns,nb)` completeness pairing; surplus siblings stay active. Expected dup ≈ 77 (vs 80). Verify counts and report.
|
- **Dedupe refinement (stretch, targeted):** for cross-province groups with >2 members, replace collapse-to-one with `min(ns,nb)` completeness pairing; surplus siblings stay active. Expected dup ≈ 77 (vs 80). Verify counts and report.
|
||||||
|
|
||||||
|
## P3 additions — frozen 2026-11-10 (personalization)
|
||||||
|
|
||||||
|
New tables (guarded, idempotent creation; fresh AND existing DBs migrate in place):
|
||||||
|
|
||||||
|
```sql
|
||||||
|
swipe_events ( -- append-only strike log; the learning source of truth
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
listing_id INTEGER NOT NULL REFERENCES listings(id) ON DELETE CASCADE,
|
||||||
|
action TEXT NOT NULL CHECK (action IN ('like','nope','maybe')),
|
||||||
|
round INTEGER NOT NULL DEFAULT 1, -- 1 = first verdict; 2+ = second look (engine caps influence at round 2)
|
||||||
|
section_id INTEGER, -- section it happened in (NULL for list edits / legacy)
|
||||||
|
created_at TEXT DEFAULT (datetime('now'))
|
||||||
|
);
|
||||||
|
CREATE INDEX idx_events_listing ON swipe_events (listing_id, round);
|
||||||
|
CREATE INDEX idx_events_time ON swipe_events (created_at);
|
||||||
|
|
||||||
|
sections (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open','report','closed')),
|
||||||
|
size INTEGER NOT NULL,
|
||||||
|
profile_version INTEGER, -- taste_profile.version used at compose
|
||||||
|
recipe TEXT, -- JSON summary {new, return, wildcard, caps_applied, note}
|
||||||
|
created_at TEXT DEFAULT (datetime('now')),
|
||||||
|
closed_at TEXT
|
||||||
|
);
|
||||||
|
|
||||||
|
section_items (
|
||||||
|
section_id INTEGER NOT NULL REFERENCES sections(id) ON DELETE CASCADE,
|
||||||
|
listing_id INTEGER NOT NULL REFERENCES listings(id) ON DELETE CASCADE,
|
||||||
|
slot TEXT NOT NULL CHECK (slot IN ('new','return','wildcard')),
|
||||||
|
position INTEGER NOT NULL,
|
||||||
|
PRIMARY KEY (section_id, listing_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
taste_profile (
|
||||||
|
version INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
created_at TEXT DEFAULT (datetime('now')),
|
||||||
|
source TEXT NOT NULL CHECK (source IN ('agent','mock','fallback')),
|
||||||
|
payload TEXT NOT NULL -- JSON: lead, leanings, changes, chips, adjustments, wildcards
|
||||||
|
);
|
||||||
|
|
||||||
|
agent_runs (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
started_at TEXT, finished_at TEXT,
|
||||||
|
ok INTEGER NOT NULL DEFAULT 0,
|
||||||
|
source TEXT, model TEXT, cost_note TEXT, error TEXT,
|
||||||
|
profile_version INTEGER
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
Semantics:
|
||||||
|
|
||||||
|
- **Learning = `swipe_events` only** (append-only). A cleared swipe keeps its history — clearing removes the *UI verdict*, not the record. **UI truth = `swipes`** (latest verdict / cleared), unchanged from P1–P2.
|
||||||
|
- Round = prior events for that listing + 1, assigned at write time. Rounds cap at 2 for engine influence (any later events weigh as round 2; v1 returns never produce rounds >2 by design).
|
||||||
|
- **Migration seeding:** when `swipe_events` is empty and `swipes` has rows → insert one round-1 event per swipe row (created_at copied). Only-when-empty, hence idempotent.
|
||||||
|
- **Sections:** at most ONE `open` section at a time. The deck = that section's `section_items` in `position` order, filtered to items undecided at read. Exhausted = no item undecided → `status='report'`. Forge = close current + open new. First-ever read composes a cold section (rubric-weighted, w=0).
|
||||||
|
- **Returns eligibility (v1.1 contract, amends the frozen ≤5 rule):** a verdict exists (like/maybe/nope in `swipes`); listing `active`; NOT in either of the previous two sections; no round≥2 `nope` ever (rested); not already returned once (round cap). Selection prefers maybe > like > nope; nope candidates must be "surprising" (points ≥55 or net-positive family) for the RESERVED slots only. Every batch carries ≥2 returns whenever eligible stock exists and fills the remainder up to 25 when fresh < 23; ≤2 nope-verdict returns per batch; fill returns relax the per-family cap from 4 to 8 (fill slots only).
|
||||||
|
- **`points` is computed, not stored** (deterministic from events + profile; a cache is allowed only if it never changes semantics).
|
||||||
|
- **prefs:** `forge_mode` ('ondemand' default | 'strict') · `rubric_debug` ('off' default | 'on' — dossier "why" breakdown visibility; v1.1). Existing keys unchanged.
|
||||||
|
- **Legacy deck semantics:** `/api/deck` (cursor paging; 7-day maybe re-admit) stays as a read-only fallback for debugging; sections are the production flow.
|
||||||
|
|
||||||
|
## B1 additions — frozen 2026-11-10 (photo experience v2)
|
||||||
|
|
||||||
|
- **New columns** (guarded, idempotent migrations in `db.init_db`):
|
||||||
|
- `listings.imgs TEXT` — JSON array of gallery image URLs (page order, deduped by image id; entry 0 = the established primary). Absent/empty → single-image fallback to `img_url`.
|
||||||
|
- `swipes.last_photo_idx INTEGER` — the photo index on screen when the card was struck (set by `POST /api/swipe`'s optional `photo_idx`; NULL when unknown). Drives UI-SPEC §11.5 rotation: a re-shown listing leads at `(idx+1) % n`.
|
||||||
|
- **Importer:** carries `imgs` from hunt state (state gains `imgs[]` beside `img`; the hunt image tool fills both in the ONE page fetch it already makes — `img` = established primary, `imgs[0]` = that primary, remainder = gallery in page order).
|
||||||
|
- **Media mirror:** `data/img/{id}_{n}.webp` per `imgs[n]` (polite ~1 req/s, skip-existing, resumable — same as P2); `GET /img/{id}?i=n` → mirrored file → CDN 302 → 404. Legacy `/img/{id}` unchanged.
|
||||||
|
- **API payload additions:** deck/section items and list rows carry `n_photos` (int) and `last_photo_idx` (int|null); `POST /api/swipe` accepts optional `photo_idx` (int) and persists it.
|
||||||
|
- **Rotation is deck-only** (list thumbs + dossier hero = photo 0).
|
||||||
|
|||||||
@@ -62,6 +62,22 @@ The nightly import is triggered by its own Hermes cron job — `bike-tinder-impo
|
|||||||
|
|
||||||
No-build SPA — vanilla JS + CSS, pointer-events drag, keyboard arrows on desktop (~300 lines). A small service worker caches the shell; the API is always live. Screens, gestures, and visual anatomy: see `UI-SPEC.md`.
|
No-build SPA — vanilla JS + CSS, pointer-events drag, keyboard arrows on desktop (~300 lines). A small service worker caches the shell; the API is always live. Screens, gestures, and visual anatomy: see `UI-SPEC.md`.
|
||||||
|
|
||||||
|
## Personalization architecture (Phase 3) — frozen 2026-11-10
|
||||||
|
|
||||||
|
| piece | what | where |
|
||||||
|
|---|---|---|
|
||||||
|
| Taste engine | deterministic points + breakdown from `swipe_events` + rubric | `app/taste.py` |
|
||||||
|
| Section engine | compose/forge/read sections, returns, wildcards, modes | `app/sections.py` |
|
||||||
|
| Agent pass | the ONLY LLM step — reads strikes, writes the profile; never in a request path | `app/agentpass.py` |
|
||||||
|
|
||||||
|
**The law:** no LLM in the swipe path, ever. The deck renders and the engine scores with zero network; the agent pass runs between sections (forge) or on schedule (P4 nightly), and can fail without breaking anything — compose falls back to the last profile.
|
||||||
|
|
||||||
|
**Forge flow (on-demand):** section exhausted → `POST /api/section/forge` → agent pass (mock/fallback when unconfigured; live when the key exists) writes `taste_profile` → section engine composes the next 25. **Strict mode:** forge refuses (409) until a fresh pass exists post-exhaustion — the nightly cron provides it at P4.
|
||||||
|
|
||||||
|
**API additions:** `GET /api/section` · `POST /api/section/forge` · `GET /api/trends` · `POST /api/agent/run`. Listing payloads gain `points`, `points_breakdown`, `tank`, `slot`, `round_next` (see UI-SPEC §10 and DATA-MODEL P3).
|
||||||
|
|
||||||
|
**Cost & keys:** each pass ≈ pennies (one compact prompt; house model default via `OPENROUTER_API_KEY`). The key lives in the host/stack env only — never the repo, never logs. "Pause" stops passes with all other spend.
|
||||||
|
|
||||||
## Deployment (house constraints)
|
## Deployment (house constraints)
|
||||||
|
|
||||||
- **Stack dir:** `/var/mnt/DATA/podman-stacks/bike-tinder/` — `docker-compose.yml` + `Dockerfile` (`python:3.14-slim` + fastapi/uvicorn/requests), volume `./data:/data`, `restart: unless-stopped`.
|
- **Stack dir:** `/var/mnt/DATA/podman-stacks/bike-tinder/` — `docker-compose.yml` + `Dockerfile` (`python:3.14-slim` + fastapi/uvicorn/requests), volume `./data:/data`, `restart: unless-stopped`.
|
||||||
@@ -100,6 +116,29 @@ data/ runtime state (gitignored)
|
|||||||
docs/ this design set
|
docs/ this design set
|
||||||
```
|
```
|
||||||
|
|
||||||
## Runbook (completed at Phase 4)
|
## Runbook (Phase 4)
|
||||||
|
|
||||||
Deploy / update / rollback steps, log locations, import troubleshooting (gates), backup-restore drill, phone verification checklist.
|
**Stack home:** `/var/mnt/DATA/podman-stacks/bike-tinder/` — a checkout of the `release` branch, plus stack-local `data/` (DB + image mirror + backups), `.env` (secrets, never committed) and `tailscale-state/` (node identity). Unit: `podman-stack-bike-tinder.service` (installed from `deploy/`).
|
||||||
|
|
||||||
|
**Deploy / update a cut:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# from the repo: merge dev -> main, then fast-forward release
|
||||||
|
git -C /var/mnt/DATA/podman-stacks/bike-tinder pull --ff-only
|
||||||
|
podman-compose -f deploy/docker-compose.yml --env-file .env build
|
||||||
|
systemctl --user restart podman-stack-bike-tinder.service
|
||||||
|
```
|
||||||
|
|
||||||
|
Docs-only cuts stop after the `pull`. The unit is `oneshot` + `RemainAfterExit` with `Delegate=yes`, so the stack survives `hermes gateway restart` and reboots. The historical dev service (`bike-tinder-dev.service`) is retired at deploy; rollback happens through the stack, not that unit.
|
||||||
|
|
||||||
|
**Rollback:** check out the previous release SHA in the stack dir, rebuild, restart. The DB is never touched by code rollbacks (migrations are guarded and idempotent); if a migration ever needs unwinding, restore the last backup (below).
|
||||||
|
|
||||||
|
**Logs:** `podman logs bike-tinder-app`, `podman logs bike-tinder-ts`, `journalctl --user -u podman-stack-bike-tinder.service`.
|
||||||
|
|
||||||
|
**Import troubleshooting:** `POST /api/import` refuses when the hunt state is older than 26 h or missing — check `~/.hermes/scripts/motorcycle-hunt.d/*.json`. The nightly chain is the Hermes cron job `bike-tinder-import` (08:30 Atlantic: import → agent read → DB backup); it is silent on success by design — inspect its last run in the cron list if a morning looks stale. The container also runs an on-boot catch-up import when the last run is >26 h old.
|
||||||
|
|
||||||
|
**Backups:** nightly `data/backup/bike-tinder-YYYYMMDD-HHMM.sqlite3`, last 14 kept. Restore drill: stop the unit → remove `data/bike-tinder.sqlite3-wal`/`-shm` → copy the chosen backup over `data/bike-tinder.sqlite3` → start the unit → check `GET /api/stats`.
|
||||||
|
|
||||||
|
**Phone checklist:** open `https://biketinder.tailf35a34.ts.net` from a tailnet device → expect the deck; Add to Home Screen (PWA installs via the manifest); swipe once and confirm the decision shows in List. If the URL fails: check the sidecar (`podman logs bike-tinder-ts`), confirm `TS_AUTHKEY` in `.env`, and re-run `podman-compose -f deploy/docker-compose.yml --env-file .env up -d`.
|
||||||
|
|
||||||
|
**First-run sidecar (one-time):** mint a Tailscale auth key (admin console → Settings → Keys), put `TS_AUTHKEY=<key>` into the stack `.env`, then `podman-compose -f deploy/docker-compose.yml --env-file .env up -d tailscale`.
|
||||||
|
|||||||
42
docs/DEVELOPMENT.md
Normal file
42
docs/DEVELOPMENT.md
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Development and deployment
|
||||||
|
|
||||||
|
Run these commands from the repo root. The container uses Python 3.14. Use a scratch database and a different port from any running instance; the example binds only to localhost.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python3 -m venv .venv
|
||||||
|
.venv/bin/python -m pip install -r requirements.txt
|
||||||
|
mkdir -p data/dev-img
|
||||||
|
|
||||||
|
# Optional: seed a development catalog from motorcycle-hunt snapshots.
|
||||||
|
.venv/bin/python -m app.importer --state-dir /path/to/hunt-snapshots --db data/dev.sqlite3
|
||||||
|
|
||||||
|
# Local app and read-only deck preview.
|
||||||
|
.venv/bin/python -m app.serve --host 127.0.0.1 --port 9454 --db data/dev.sqlite3 --img-dir data/dev-img
|
||||||
|
.venv/bin/python -m app.deck --db data/dev.sqlite3 --top 15
|
||||||
|
|
||||||
|
# Run separately from the dev server.
|
||||||
|
.venv/bin/python -m app.selfcheck
|
||||||
|
```
|
||||||
|
|
||||||
|
`data/` and `.venv/` are local and ignored by Git. The `app.imgfetch` CLI can cache images for a development catalog; use `--help` for options. The nightly process in the live stack imports the catalog, refreshes the taste profile, and backs up SQLite.
|
||||||
|
|
||||||
|
For an offline taste-profile pass, run `app.agentpass --mock` against a development database with swipe history. To inspect the inputs without writing a profile, use `--print-summary --dry-run`. The optional live provider is configured outside the repo. Never use the running instance's DB for test writes; use SQLite's backup API if you need a consistent copy of a database in use. The legacy browser walks (`app.fe_walk_b1`, `app.fe_walk_v11`) need Playwright, Chromium, and staged scratch fixtures; inspect their fixture paths before running them.
|
||||||
|
|
||||||
|
## API entry points
|
||||||
|
|
||||||
|
| Area | Routes |
|
||||||
|
| --- | --- |
|
||||||
|
| Status | `GET /api/health`, `GET /api/stats` |
|
||||||
|
| Browse | `GET /api/section`, `GET /api/list`, `GET /api/listing/{id}` |
|
||||||
|
| Decide | `POST /api/swipe`, `DELETE /api/swipe/{id}` |
|
||||||
|
| Refresh | `POST /api/import`, `POST /api/section/forge`, `POST /api/agent/run` |
|
||||||
|
| Preferences | `POST /api/prefs`, `POST /api/badge/seen` |
|
||||||
|
| Display | `GET /api/trends`, `GET /img/{id}?i=n` |
|
||||||
|
|
||||||
|
For data contracts, see [Data model](DATA-MODEL.md), [Ranking](RANKING.md), and the [UI spec](UI-SPEC.md).
|
||||||
|
|
||||||
|
## Deployment
|
||||||
|
|
||||||
|
The [Podman Compose definition](../deploy/docker-compose.yml) runs the app and a Tailscale sidecar for private access. The [systemd user unit](../deploy/podman-stack-bike-tinder.service) owns the running containers. Keep deployment-specific configuration outside Git and retain independent backups of the SQLite data. Do not expose the single-user app directly to the public internet.
|
||||||
|
|
||||||
|
Development lands on `dev`, verified changes move to `main`, and the running stack follows `release`. Build the candidate image before restarting the service. After cutover, check `/api/health` and confirm that the HTTP shell matches the intended revision. Keep the previous image for rollback.
|
||||||
17
docs/HOW-IT-WORKS.md
Normal file
17
docs/HOW-IT-WORKS.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# How Bike-Tinder works
|
||||||
|
|
||||||
|
## From classifieds to cards
|
||||||
|
|
||||||
|
The motorcycle-hunt crawler collects Kijiji listings in Nova Scotia and New Brunswick. The importer reads the crawler's snapshots, adds new listings, updates existing ones, and retires listings only after repeated misses. A blocked or failed fetch isn't proof that a listing is gone. See [Sources](SOURCES.md) and the [data model](DATA-MODEL.md).
|
||||||
|
|
||||||
|
The web app reads that catalog from SQLite and presents listings in a swipe deck or a searchable List. Your current verdict is editable; the app also records swipe events for ranking. Tap the sides of a deck card to browse its photos, the center to open the details, or the photo in the details view to open the fullscreen gallery. See the [interface spec](UI-SPEC.md) for gesture and keyboard behavior.
|
||||||
|
|
||||||
|
## Ranking and photos
|
||||||
|
|
||||||
|
With no swipe history, the deck starts with the motorcycle hunt's scoring rubric. Once there is history, the app builds a taste profile and uses it when ordering future batches. Batches can mix fresh bikes, wildcards, and second looks. A model runs outside the swipe path: the swipe itself never waits for a model response. See [Ranking](RANKING.md) and [Design](DESIGN.md).
|
||||||
|
|
||||||
|
The importer retains gallery URLs in source order. The running stack keeps primary photos locally; when a requested gallery photo isn't cached, the app redirects to the source image. A missing photo uses a placeholder. See [the gallery mirror backlog](BACKLOG.md#gallery-mirror-gap-found-2026-11-12-filing-for-a-later-phase).
|
||||||
|
|
||||||
|
## More detail
|
||||||
|
|
||||||
|
[Development and deployment](DEVELOPMENT.md) covers setup and testing. The [roadmap](PLAN.md) and [backlog](BACKLOG.md) cover planned work.
|
||||||
60
docs/PLAN.md
60
docs/PLAN.md
@@ -1,6 +1,12 @@
|
|||||||
# PLAN — Bike-Tinder
|
# PLAN — Bike-Tinder
|
||||||
|
|
||||||
**Status:** Build phase — Phase 1 complete (2026-11-10); Phase 2 not yet started. Phases are gated: each lands, is verified, then the next begins. Glenn's standing rule applies — "pause" stops all spend immediately.
|
**Status:** **v1.0 SHIPPED (2026-11-12)** — Phases 1–4 complete: stack live under its unit, nightly chain armed, tailnet URL in daily use, Gitea release `v1.0.0` cut. **v1.1** (QOL batch) is spec'd and awaits Glenn's build order; **v1.2** (analytics suite + fresh-ledger reset) queued. Phases are gated: each lands, is verified, then the next begins. Glenn's standing rule applies — "pause" stops all spend immediately.
|
||||||
|
|
||||||
|
## Version map
|
||||||
|
|
||||||
|
- **v1.0** — shipped 2026-11-12: the working loop (Phases 1–4 — catalog, swipe deck, personalization, deployed stack + nightly automation). Gitea release `v1.0.0`.
|
||||||
|
- **v1.1** — QOL batch, spec'd 2026-11-12 (`BACKLOG.md`): keyboard shortcuts · info-first tap flow · option C photos (fit + blurred backdrop) · minimal rubric + debug "why" toggle · section-counter fix + dry-catalog messaging · fresh counter + loud "No more new bikes!" empty state · batch formula v2 (25-target, second-look fill, ≥2 per batch) + Second Look band pop. No data reset. **Build authorized 2026-11-12** — sign-offs: ≥2-SL default (rich day 21 fresh/2 SL/2 wild) · violet band · Backspace alias; spec = `docs/V1.1-BUILD.md`; build phase in progress.
|
||||||
|
- **v1.2** — analytics suite (`BACKLOG.md`): full interaction capture + from-scratch ledger reset ("fresh start").
|
||||||
|
|
||||||
## v1 goal
|
## v1 goal
|
||||||
|
|
||||||
@@ -28,33 +34,51 @@ A single-source, single-user, tailnet-only Tinder-style PWA on the Rotarran: nig
|
|||||||
- **Exit criteria:** importer runs clean twice in a row (done — idempotent); counts reconcile against the source (done — 560+0+80=640); no schema surprises (done).
|
- **Exit criteria:** importer runs clean twice in a row (done — idempotent); counts reconcile against the source (done — 560+0+80=640); no schema surprises (done).
|
||||||
|
|
||||||
**Carried into Phase 2 from P1 findings:**
|
**Carried into Phase 2 from P1 findings:**
|
||||||
- Dedupe pairing refinement — multi-unit dealer groups (BMW G 310 R, 2 groups) over-merge under the v1 collapse-to-one rule; `min(ns,nb)` pairing → 77 dup, keeps sibling rows visible. Revisit before launch.
|
- Dedupe pairing refinement — **DONE (P2, 2026-11-10):** `min(ns,nb)` completeness pairing landed; 640 rows = 563 active + 77 dup, sibling rows stay visible.
|
||||||
- Image backfill is priority — only ~20/640 rows carry an image in the state; the DESIGN bootstrap backfill is essential for the deck.
|
- Image backfill — **DONE (P2, 2026-11-10):** hunt backfill filled 619/620 Kijiji listings; app mirror `app/imgfetch.py` → 559 local webp (3 CDN 404s fall back to placeholder).
|
||||||
- Dealer-blast mitigation + Stage 1.5 swipe boost matter immediately — only ~67/560 active rows are family-scored; the rest sort below.
|
- Dealer-blast mitigation + Stage 1.5 swipe boost matter immediately — only ~67/560 active rows are family-scored; the rest sort below.
|
||||||
|
|
||||||
## Phase 2 — The swipe app (core)
|
## Phase 2 — The swipe app (core)
|
||||||
|
|
||||||
**Status: in build (Torres, 2026-11-10) on `dev`.** Contract frozen: `UI-SPEC.md` v2 + `reference/mockup-001-fidelity.html` (Variant A "Pure", approved by Glenn; 5-deep live stack, no-lock commit, dossier sheet). Image backfill underway at freeze (hunt `motorcycle-hunt-img.py`, 620 listings) + app-side mirror (`app/imgfetch.py`).
|
**Status: built by Torres + verified by Alex (2026-11-10) on `dev`; phone pass LIVE (swiping over the tailnet).** Contract frozen: `UI-SPEC.md` v2 + `reference/mockup-001-fidelity.html` (Variant A "Pure", approved by Glenn; 5-deep live stack, no-lock commit, dossier sheet). Image backfill + app-side mirror (`app/imgfetch.py`) done: 619/620 listings enriched, 559 local webp.
|
||||||
|
|
||||||
- [x] 2–3 UI mock variants → Glenn picked **Variant A "Pure"** (approved 2026-11-10 after two fix rounds)
|
- [x] 2–3 UI mock variants → Glenn picked **Variant A "Pure"** (approved 2026-11-10 after two fix rounds)
|
||||||
- [ ] API (deck cursor, swipe upsert/clear, list + filters, stats, media, import) + PWA frontend (faithful mockup port: stack engine, drag, Like / Nope / Maybe, dossier, empty/undo) — no match overlay (removed by Glenn's call)
|
- [x] API (deck cursor, swipe upsert/clear, list + filters, stats, media, import) + PWA frontend (faithful mockup port: stack engine, drag, Like / Nope / Maybe, dossier, empty/undo) — no match overlay (removed by Glenn's call)
|
||||||
- [ ] Full list view with inline edit; basic filters (decision + sort + search)
|
- [x] Full list view with inline edit; basic filters (decision + sort + search)
|
||||||
- [ ] Proof: runs locally on :9453; exercised on Glenn's phone over the tailnet
|
- [x] Proof: runs locally on :9453 (systemd dev unit) + tailnet `http://rotarran.tailf35a34.ts.net:9453`; phone-viewport browser pass green; **phone pass live 2026-11-10** — Glenn swiping the real deck over the tailnet (24 swipes recorded + verified from the DB; list-edit tap still unexercised by Glenn — automation-covered)
|
||||||
- **Exit criteria:** Glenn can swipe a real deck on his phone and edit results in the list.
|
- **Exit criteria:** Glenn can swipe a real deck on his phone and edit results in the list — swiping proven; edit-tap pending.
|
||||||
|
|
||||||
## Phase 3 — Personalization
|
## Phase 3 — Personalization ("Trends")
|
||||||
|
|
||||||
- Swipe history → nightly retrain of explainable ranking
|
**Status: BUILT → VERIFIED → LIVE (2026-11-11).** Contract frozen 2026-11-10; built by Torres in ~45 min ($3.80, 128 calls, 25/25 selfcheck; commits `04463ca`, `c205d31`, `b6e1851`); independently verified by Alex on DB copies — selfcheck 25/25 re-run, full section loop (compose → strikes → rounds → report → forge), strict-mode 409/200, forge-on-demand, trends, tank gauges, dossier "Why this rank". **One real defect found + fixed** (`1e4d840`): second-look returns were filtered out of the swipe buffer (`decision===null`), leaving round-2 dead in the UI — fixed and verified end to end (SECOND LOOK band, restrike writes round 2). **Live integration 2026-11-11:** backup at `~/dev/bike-tinder-p3/db-backup-pre-p3-20261111.sqlite3`; service restarted; P3 tables + 66 seeded events; section 1 composed; live calibration pass run (`deepseek/deepseek-v4.1-flash`, $0.0042; `agent_runs` #1) → taste profile v1 (`source=agent`).
|
||||||
- Trends panel ("you lean: …"); "because you liked X" chips
|
|
||||||
- Deck ordering by learned taste with cold-start guardrails
|
|
||||||
- **Exit criteria:** after ~30 swipes, deck order visibly reflects taste; explainability shown in UI.
|
|
||||||
|
|
||||||
## Phase 4 — Deploy + automate
|
Glenn's approved design (2026-11-10):
|
||||||
|
|
||||||
- Podman compose stack at `/var/mnt/DATA/podman-stacks/bike-tinder/` + systemd unit (`Delegate=yes`) + in-stack Tailscale sidecar serving `https://biketinder.tailf35a34.ts.net`; PWA install on Glenn's phone
|
- **Sections of 25:** the deck is a finite batch; the agent always runs between sections (learning segmentation + anti-doom-scroll). Modes: forge-on-demand (default) / strict (nightly read only). Fallback to the last profile if the agent fails.
|
||||||
- Enable the new `bike-tinder-import` Hermes cron (08:30 daily, post-hunt; script-only, zero LLM; parked until deploy); DB backup routine
|
- **Section lead:** on open/refresh the best suggestions lead; the 5-card live stack is unchanged.
|
||||||
- Runbook in DESIGN.md; announce + handover note
|
- **Wildcards (~1-in-10):** labeled "maybe you'll like this one instead"; plausible stretches; never hard-noped families; their answers teach the model.
|
||||||
- **Exit criteria:** survives a reboot; refreshes overnight unattended; installable and used from the phone.
|
- **Second looks:** likes + maybes + nopes return (chosen; ≤5/section, ≤2 nopes); Round 2 weighs ×2; flips are the loudest signal; a re-nope rests the item; rounds cap at 2. Returning maybes can bring their variants.
|
||||||
|
- **Match points:** taste + agent adjustments; drive ordering, the section recipe (family caps, price mix), and returns. Explainable in the dossier; tank gauge (non-numeric) in the List.
|
||||||
|
- **Hits objective:** order = expected-like; Trends reports the hit rate (likes; likes+maybes).
|
||||||
|
- **Agent pass:** nightly/on-demand read (mock + live + fallback); **no LLM in the swipe path — ever.**
|
||||||
|
- **Exit criteria:** after ≥30 swipes the deck order visibly reflects taste; explainability visible (dossier breakdown + Trends); the section loop works end-to-end; the agent kill-switch test passes (app fully functional on the last profile with the pass disabled).
|
||||||
|
|
||||||
|
**Carried forward:** service-side OpenRouter key wiring at P4 — until then each forge's agent pass runs in mock mode and supersedes the live profile · wildcard pool hygiene (rare non-bike listings can surface) · early-ramp texture: dense nopes punish broad cells (year 2020+, 12k+, nb) — the Scouts read mid until the agent carves family-level exceptions · minor reload edge: an already-restruck second look can re-cycle on mid-section reload.
|
||||||
|
|
||||||
|
**Glenn's call (2026-11-10, late): B1 "Photo experience v2" pulled forward** — built next, *before* Phase 4 ("get that feature baked in before we proceed to phase 4"). Spec frozen in `UI-SPEC.md` §11 (galleries · tap-side photo flip · center-tap fullscreen viewer · fresh lead photo on repeats) + `DATA-MODEL.md` B1 additions.
|
||||||
|
|
||||||
|
**Status: BUILT → VERIFIED → LIVE (2026-11-11).** Rail: hunt gallery capture + full-catalog backfill — 639/640 state items carry `imgs[]` (612 with 2+ photos); the 1 remainder is a photo-less ad at source ("no photo found", retried and confirmed) → app shows the placeholder, by design. App: Torres on a fresh clone (`~/dev/bike-tinder-b1-build`, commits `3b64635..036a511`, report `~/dev/bike-tinder-b1/TORRES-B1-REPORT.md`); live tree + live DB untouched during the build. Verification: selfcheck 31/31 both sides; `fe_walk_b1` headless §11 walk 390/390 (Alex re-ran green with a fresh setup). **Live integration (2026-11-11, ~00:40 AST):** pre-B1 backups `~/dev/bike-tinder-b1/preb1-live-*.sqlite3` + `preb1-files/`; 11 files deployed; service restarted; `POST /api/import` idempotent (`new:0`); live DB now 639/640 with galleries, 612 multi-photo (sample id 430 Indian Sport Scout n=8; `/img/430?i=1` → 302). Live gesture proof: side-tap flip `?i=0 ↔ ?i=1`, center-tap viewer with segments, screenshot `~/dev/bike-tinder-b1/b1-live-9453.png`. Glenn's ledger intact (248 swipes / 279 events carried). `dev` + `main` merged (`49466fb`); live tree commit `3a399e1`.
|
||||||
|
|
||||||
|
## Phase 4 — Deploy + automate (stack deployed 2026-11-12; phone check pending)
|
||||||
|
|
||||||
|
- ✅ Podman compose stack at `/var/mnt/DATA/podman-stacks/bike-tinder/` (release checkout) + systemd unit `podman-stack-bike-tinder.service` (`Delegate=yes`; verified to survive gateway/desktop restarts and re-raise after `systemctl --user restart`) + in-stack Tailscale sidecar (userspace mode) serving `https://biketinder.tailf35a34.ts.net` — **AUTHORIZED + serving 2026-11-12** (node `biketinder`, `100.114.195.22`; first phone swipes logged through the sidecar)
|
||||||
|
- ✅ `bike-tinder-import` Hermes cron live (08:30 daily, post-hunt): import → live agent read → sqlite backup (14-deep); silent on success (three clean live runs on deploy day)
|
||||||
|
- ✅ DB + image mirror migrated intact into the stack volume; backup drill produced its first files
|
||||||
|
- ✅ Deploy fix (found on first in-container agent call): `requests` was missing from requirements — the host venv had it incidentally, the slim image did not
|
||||||
|
- ✅ Runbook in DESIGN.md (deploy/rollback/logs/import gates/backup-restore/phone checklist)
|
||||||
|
- ✅ **Release v1.0.0 published 2026-11-12** — Gitea release, tag pinned to `main` head; notes summarize the working loop; source zip/tar.gz auto-generated by Gitea (nothing attached)
|
||||||
|
- ✅ In use from Glenn's phone (2026-11-12 — first photo flips + swipes logged through the sidecar); PWA "Add to Home Screen" available; full reboot test optional/pending
|
||||||
|
- **Exit criteria:** survives a reboot (service-level verified; power-level test optional); refreshes overnight unattended (armed, first automatic run 08:30 next day); installable and used from the phone — **achieved 2026-11-12.**
|
||||||
|
|
||||||
## Non-goals (v1)
|
## Non-goals (v1)
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
The deck is ordered by predicted taste. Three rungs, in order: the hunt's existing rubric (v1), a cheap explainable boost from swipe clusters (v1.x), then a small learned model (Phase 3). Nothing ever becomes a black box.
|
The deck is ordered by predicted taste. Three rungs, in order: the hunt's existing rubric (v1), a cheap explainable boost from swipe clusters (v1.x), then a small learned model (Phase 3). Nothing ever becomes a black box.
|
||||||
|
|
||||||
## Signals
|
## Signals
|
||||||
- Like = strong positive (+1). Maybe = weak positive (+0.25, kept in its own bucket). Nope = negative (−1). List-view edits count identically to in-deck swipes; the latest call wins.
|
- Like = strong positive (+1). Maybe = weak positive (+0.4) on the *direction* — "right lane, wrong trim" (Glenn's read): the family/silhouette reads positive, the specific build is uncertain; maybes replenish via second looks + variant hunts. Nope = negative (−1). List-view edits count identically; the latest call wins (full history lives in `swipe_events`).
|
||||||
- Planned v2 signals: dwell time on card, "open listing" click-outs.
|
- Planned v2 signals: dwell time on card, "open listing" click-outs.
|
||||||
|
|
||||||
## Features (v1)
|
## Features (v1)
|
||||||
@@ -16,22 +16,32 @@ Order by the hunt's existing rubric score (family tiers + MVI / whitewalls / win
|
|||||||
## Stage 1.5 — Cheap swipe boost (v1.x, post-launch)
|
## Stage 1.5 — Cheap swipe boost (v1.x, post-launch)
|
||||||
Before any model exists, the importer can nudge `score` with plain swarm signals — e.g. +N when recent likes cluster in a family, small time-decay otherwise — recomputed each import, zero ML, fully explainable. Ships as v1.x if Glenn wants it; v1 itself stays pure rubric.
|
Before any model exists, the importer can nudge `score` with plain swarm signals — e.g. +N when recent likes cluster in a family, small time-decay otherwise — recomputed each import, zero ML, fully explainable. Ships as v1.x if Glenn wants it; v1 itself stays pure rubric.
|
||||||
|
|
||||||
## Stage 2 — Learned order (Phase 3 roadmap)
|
## Stage 2 — P3 taste engine (frozen 2026-11-10)
|
||||||
- Model: logistic regression on the features above; target = like vs nope (maybes contribute softly positive). Trained on all swipe history including edits.
|
|
||||||
- Pure-Python or numpy implementation (tiny data — no heavy ML dependency). Params + training metadata live in the DB; versioned.
|
Deterministic, explainable, pure-Python. Learning reads `swipe_events` only.
|
||||||
- Retrain nightly right after import; on-demand retrain available from the app.
|
|
||||||
- Guardrails:
|
**Event weights.** action: like +1.0 · maybe +0.4 · nope −1.0; round multiplier: round 1 ×1, round ≥2 **×2** (second strikes hit twice as hard — reaffirmations and flips both); recency: ×0.5^(age_days / 21). Event weight = product.
|
||||||
- Blend: until 100 swipes, deck order = 50% rubric rank + 50% model rank; after, model order with an exploration slice (roughly every 8th card = top-rubric card not yet shown).
|
|
||||||
- Never hide: everything stays in List; ranking only reorders the deck.
|
**Feature weights.** Feature set per listing: family · price band (`<3k, 3–5k, 5–8k, 8–12k, 12k+`) · displacement band (from title/desc) · year band · km band (when present) · each extras flag · seller type · silhouette keywords (bobber/custom/classic/cruiser) · color words · region. For each (feature, value): `raw = Σ event weights`; `shrunk = raw × n/(n+3)` — no single strike taboos a family.
|
||||||
- Sanity: if model like-rate underperforms the rubric baseline (tracked weekly), it reverts to rubric order silently.
|
|
||||||
|
**Score.** `taste_raw` = Σ shrunk weights over the listing's values; `taste_norm` = percentile rank among active listings (0–100). Blend: `w = min(0.85, N/60)` where N = total events; `points = clip( round( (1−w)·rubric + w·taste_norm + agent_adj ), 0, 100 )`. `agent_adj` = Σ profile adjustments (clamped ±10 per listing). Breakdown for the dossier: rubric share · top 3 ± taste features · agent lines — all source-labeled.
|
||||||
|
|
||||||
|
**Section recipe.** Every batch targets 25 cards = fresh first (up to 23 slots) + ≥2 second looks whenever eligible stock exists + the 2 wildcard slots (wildcards draw from the fresh pool). When fresh < the remainder, second looks fill up to 25 (family cap relaxes to 8 for fill returns only); leftover fresh rolls to the next batch; fresh/return shortfalls are never padded — a drained pool yields a short (or empty) section. Constraints: ≤4 per family per section for fresh + reserved returns; ≥3 price bands when the pool allows.
|
||||||
|
|
||||||
|
**Wildcards.** Candidates: active, unseen, not in the last two sections. Exclusions: families with ≥3 cumulative nopes and net-negative weight. Prefer: different family AND different price band than the section's top new items. Score = `0.5·points + 0.5·novelty` (novelty = distance from the liked-profile centroid). Picks: 2 per section.
|
||||||
|
|
||||||
|
**Returns ("second looks").** Pool + caps per DATA-MODEL. Order: maybe > like > surprising nope (points ≥55 or net-positive family); rank within role by points DESC. A re-nope (round≥2 nope) rests the item permanently in v1.
|
||||||
|
|
||||||
|
**Guardrails.** Never hide (List untouched; ranking only reorders). The ramp keeps cold start on rubric; the engine must run with zero profile (w=0). Agent offline → compose on the last profile (source-labeled). **No LLM inside any request path or computed score** — the pass only writes the profile.
|
||||||
|
|
||||||
## Explainability
|
## Explainability
|
||||||
Each card can show up to 2 chips: top positive contributions for that bike ("low km for the money", "Vulcan family", "red", "MVI"). Chips derive from the live feature contributions; during cold start they come from the rubric's existing reasons.
|
Each card can show up to 2 chips: top positive contributions for that bike ("low km for the money", "Vulcan family", "red", "MVI"). Chips derive from the live feature contributions; during cold start they come from the rubric's existing reasons.
|
||||||
|
|
||||||
## Evaluation
|
## Evaluation
|
||||||
- Track: swipes/session, like-rate trend, maybe-rate, undo rate, and "liked-item rank" (how deep liked items sat — should trend shallow).
|
- Track: swipes/session, like-rate trend, maybe-rate, undo rate, and "liked-item rank" (how deep liked items sat — should trend shallow).
|
||||||
|
- P3 adds: hit rate (likes/decided · likes+maybes), returns re-strike rate, wildcard hit rate, flip count — surfaced in the Trends tab.
|
||||||
- Notifications (decided 2026-11-10): in-app "new since last visit" badge only — no Conduit pushes. A weekly digest remains possible later if asked for.
|
- Notifications (decided 2026-11-10): in-app "new since last visit" badge only — no Conduit pushes. A weekly digest remains possible later if asked for.
|
||||||
|
|
||||||
## Open decisions
|
## Open decisions
|
||||||
- Maybe-handling: weak positive + separate bucket (proposed) vs pure third bucket — confirm with Glenn at Phase 2.
|
- Maybe-handling — **resolved 2026-11-10 (Glenn):** weak positive on the direction ("right lane, wrong trim"); maybes replenish via second looks + variant hunts.
|
||||||
- Distance feature (map-based proximity) — likely v2.
|
- Distance feature (map-based proximity) — likely v2.
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ Glenn's words were "the easiest to scrape." Literally easiest = AutoTrader (stru
|
|||||||
|
|
||||||
## Evidence highlights (2026-11-10)
|
## Evidence highlights (2026-11-10)
|
||||||
|
|
||||||
- Snapshot: `kijiji-ns` 320 items, `kijiji-nb` 320 items; **74 cross-province twin groups (normalized title + price) → 80 dup rows under the v1 collapse-to-one rule** (verified against the live import 2026-11-10; the earlier "≈58" was a rough estimate). Two multi-unit dealer groups (BMW G 310 R) over-merge under that rule — pairing refinement tracked in `PLAN.md` (Phase 2 backlog).
|
- Snapshot: `kijiji-ns` 320 items, `kijiji-nb` 320 items; **74 cross-province twin groups (normalized title + price) → 80 dup rows under the v1 collapse-to-one rule** (verified against the live import 2026-11-10; the earlier "≈58" was a rough estimate). Two multi-unit dealer groups (BMW G 310 R) over-merged under that rule; **P2 fixed it (2026-11-10)** — `min(ns,nb)` completeness pairing: 640 rows → **563 active + 77 dup**, surplus siblings stay active.
|
||||||
- Live fetch: page 1 fetched twice → byte-identical 40 cards (no flake that day); **40/40 cards carry image URLs**; pages 9 and 15 still full (depth available beyond the current window).
|
- Live fetch: page 1 fetched twice → byte-identical 40 cards (no flake that day); **40/40 cards carry image URLs**; pages 9 and 15 still full (depth available beyond the current window).
|
||||||
- Images: `media.kijiji.ca` URLs are **token-free and size-rewritable** (`?rule=kijijica-640-webp` → 200; `960-webp` rewrite → 200; URL without rule → 404 — the rule is the size selector, not a token).
|
- Images: `media.kijiji.ca` URLs are **token-free and size-rewritable** (`?rule=kijijica-640-webp` → 200; `960-webp` rewrite → 200; URL without rule → 404 — the rule is the size selector, not a token).
|
||||||
- Anti-bot: none observed across 15 pages of probes + production traffic.
|
- Anti-bot: none observed across 15 pages of probes + production traffic.
|
||||||
|
|||||||
123
docs/UI-SPEC.md
123
docs/UI-SPEC.md
@@ -20,6 +20,7 @@
|
|||||||
### Card geometry
|
### Card geometry
|
||||||
- `.card{position:absolute;left:14px;right:14px;top:6px;bottom:34px;border-radius:18px;overflow:hidden;background:var(--card);box-shadow:0 0 0 1px rgba(255,255,255,.07),0 18px 40px rgba(0,0,0,.5);will-change:transform;touch-action:none}`
|
- `.card{position:absolute;left:14px;right:14px;top:6px;bottom:34px;border-radius:18px;overflow:hidden;background:var(--card);box-shadow:0 0 0 1px rgba(255,255,255,.07),0 18px 40px rgba(0,0,0,.5);will-change:transform;touch-action:none}`
|
||||||
- Photo full-bleed (`object-fit:cover`); bottom gradient 52% tall (transparent → rgba(0,0,0,.55) @55% → .88). Info block at bottom:14px: chips row → title (23px/800, clamp 2 lines) → price (20px/600) → sub (13px, var(--mut)).
|
- Photo full-bleed (`object-fit:cover`); bottom gradient 52% tall (transparent → rgba(0,0,0,.55) @55% → .88). Info block at bottom:14px: chips row → title (23px/800, clamp 2 lines) → price (20px/600) → sub (13px, var(--mut)).
|
||||||
|
- Photo fit is **contain** (v1.1): the whole photo is shown uncropped over a soft blurred backdrop of the same image (option C). The backdrop is decorative (blurred, dimmed, overflow-clipped); the sharp photo sits on top with a gentle shadow. The fullscreen viewer already fits; list thumbs stay `cover` (56px).
|
||||||
- No photo → placeholder panel: 45° repeating stripes (#141419/#18181f), centered copy: "Photo pending — thumbnail backfill queued".
|
- No photo → placeholder panel: 45° repeating stripes (#141419/#18181f), centered copy: "Photo pending — thumbnail backfill queued".
|
||||||
- ⓘ button: 32px circle, top-right of card (rgba(0,0,0,.45), 1px rgba(255,255,255,.25) border) → opens the dossier for that card.
|
- ⓘ button: 32px circle, top-right of card (rgba(0,0,0,.45), 1px rgba(255,255,255,.25) border) → opens the dossier for that card.
|
||||||
- Chips: 11px/600, rgba(255,255,255,.14) bg, radius 999px. Production content: max 3 = seller chip ("Dealer" only when seller_type=dealer) + first 2 extras.
|
- Chips: 11px/600, rgba(255,255,255,.14) bg, radius 999px. Production content: max 3 = seller chip ("Dealer" only when seller_type=dealer) + first 2 extras.
|
||||||
@@ -51,6 +52,8 @@
|
|||||||
### Empty state
|
### Empty state
|
||||||
Dashed panel, copy exactly: **"You're all caught up."** / "New bikes arrive nightly — next import 08:30." / "↺ brings the last card back." Undo restores the last card.
|
Dashed panel, copy exactly: **"You're all caught up."** / "New bikes arrive nightly — next import 08:30." / "↺ brings the last card back." Undo restores the last card.
|
||||||
|
|
||||||
|
§2's quiet "You're all caught up." remains only for legacy (pre-section) mode. In section mode, when the deck can serve nothing at all — no fresh listings, no eligible second looks, no wildcards — the deck shows a prominent **"No more new bikes!"** panel with "New bikes arrive nightly — next import 08:30." and, when true, "↺ brings the last card back." This panel supersedes the report card in that state only; the report card remains the end-of-section screen whenever more cards can still be served (v1.1).
|
||||||
|
|
||||||
## 3. Dossier (fullscreen sheet) — one sheet, both doors
|
## 3. Dossier (fullscreen sheet) — one sheet, both doors
|
||||||
|
|
||||||
Opened from deck ⓘ (or tap) AND list row tap. iOS-sheet geometry: top gap 26px; `border-radius:18px 18px 0 0`; bg #0f0f13; slide-in `.3s cubic-bezier(.2,.8,.3,1)` from `translateY(103%)`. Backdrop: rgba(0,0,0,.38), z 69/70.
|
Opened from deck ⓘ (or tap) AND list row tap. iOS-sheet geometry: top gap 26px; `border-radius:18px 18px 0 0`; bg #0f0f13; slide-in `.3s cubic-bezier(.2,.8,.3,1)` from `translateY(103%)`. Backdrop: rgba(0,0,0,.38), z 69/70.
|
||||||
@@ -81,13 +84,13 @@ Opened from deck ⓘ (or tap) AND list row tap. iOS-sheet geometry: top gap 26px
|
|||||||
|
|
||||||
## 6. Trends
|
## 6. Trends
|
||||||
|
|
||||||
- Stub (Phase 3): "Trends — Phase 3" / "The deck learns your taste as you swipe."
|
- **Real (P3):** see §10.6.
|
||||||
|
|
||||||
## 7. Production deltas (the ONLY changes from the mockup)
|
## 7. Production deltas (the ONLY changes from the mockup)
|
||||||
|
|
||||||
1. **Data wiring.** Deck buffer from `GET /api/deck` (limit 40, opaque cursor); refill when buffer <5 and a cursor exists. List from `GET /api/list`. Dossier from the row/card payload. Header from `GET /api/stats`.
|
1. **Data wiring.** Deck buffer from `GET /api/deck` (limit 40, opaque cursor); refill when buffer <5 and a cursor exists. List from `GET /api/list`. Dossier from the row/card payload. Header from `GET /api/stats`.
|
||||||
2. **Swipe persistence.** Commits fire `POST /api/swipe` optimistically — the UI never waits. Failures go to a retry queue (attempts at ~2s/5s/10s, then every 15s while the tab lives); toast "Couldn't save — retrying" on first failure. Undo fires `DELETE /api/swipe/{id}` through the same queue.
|
2. **Swipe persistence.** Commits fire `POST /api/swipe` optimistically — the UI never waits. Failures go to a retry queue (attempts at ~2s/5s/10s, then every 15s while the tab lives); toast "Couldn't save — retrying" on first failure. Undo fires `DELETE /api/swipe/{id}` through the same queue.
|
||||||
3. **Badge.** `POST /api/badge/seen` once per session when the deck first renders. Header shows "{live} live" always; "· {new} new" only when new>0.
|
3. **Badge.** `POST /api/badge/seen` once per session when the deck first renders. Header pill shows `{live} live · {fresh} fresh` always, and `· {new} new` only when new > 0. `fresh` = active listings with no swipe (the deck's new-pool predicate) — v1.1. The pill and the list sub-line refresh themselves shortly after any decision change (swipes, undo, dossier/list/matches edits) and whenever a new batch lands — no reload needed (v1.1.1, issue #2).
|
||||||
4. **List filters + search** per §4 (mockup had placeholder chips only).
|
4. **List filters + search** per §4 (mockup had placeholder chips only).
|
||||||
5. **Matches real (§5); Trends stays stub.**
|
5. **Matches real (§5); Trends stays stub.**
|
||||||
6. **Images.** Every card/thumb references `/img/{id}` (server: cached file → 302 CDN → 404 → placeholder). The client never hotlinks directly.
|
6. **Images.** Every card/thumb references `/img/{id}` (server: cached file → 302 CDN → 404 → placeholder). The client never hotlinks directly.
|
||||||
@@ -110,3 +113,119 @@ Machine-checkable:
|
|||||||
- Selfcheck: all P1 checks green + new API checks green.
|
- Selfcheck: all P1 checks green + new API checks green.
|
||||||
|
|
||||||
Human (parent-run after build): Glenn swipes a real deck from his phone and edits results in the list — the Phase 2 exit criterion.
|
Human (parent-run after build): Glenn swipes a real deck from his phone and edits results in the list — the Phase 2 exit criterion.
|
||||||
|
|
||||||
|
## 10. P3 additions (v3) — Personalization UI
|
||||||
|
|
||||||
|
**Frozen 2026-11-10 (Glenn approved).** Everything in §1–§7 remains in force; all of this is additive. The card face gains only the two system bands (§10.3); everything else lives in the list, the report screen, the dossier, and Trends.
|
||||||
|
|
||||||
|
### 10.1 Sections — the deck becomes a finite batch
|
||||||
|
|
||||||
|
- The deck serves ONE section at a time: 25 cards (default). A section is the unit of a swipe burst; when it is exhausted, the next one is forged (with an agent pass in between — §10.2).
|
||||||
|
- Header right side, next to the stats: section progress "7/25" (12px/650, muted; turns `--accent1` at 25/25; fits within the existing header, no layout shift).
|
||||||
|
- Section composition (engine side — RANKING.md): every batch targets 25 cards = fresh first (up to 23 slots) + ≥2 SECOND LOOK cards whenever eligible stock exists + 2 WILDCARD cards; when fresh < 23, second looks fill the remainder up to 25. Re-nope returns stay ≤2 per batch; fresh shortfalls are never padded (v1.1).
|
||||||
|
- ALL §2 mechanics apply unchanged inside a section: 5-deep stack, `.topcard` invariant, no-lock commit, stamps, undo; the buffer refills from the pinned section order.
|
||||||
|
|
||||||
|
### 10.2 Section report + forge
|
||||||
|
|
||||||
|
- When no undecided item remains, the deck shows the report card (replaces the §2 empty state when a section exists; same visual language):
|
||||||
|
- Title: **"Section complete."**
|
||||||
|
- Counts row: "♥ {likes} · ★ {maybes} · ✕ {nopes}"
|
||||||
|
- Sub: "The file is digesting your strikes."
|
||||||
|
- Line 2 (only when >0): "Second looks re-struck: {n} · Wildcards that hit: {n}"
|
||||||
|
- Actions:
|
||||||
|
- **Forge mode `ondemand` (default):** primary button **"Forge next 25"**. Tap → inline status "The agent is reading your strikes…" → new section hydrates. If the pass fails: "Agent offline — forging on the last read." and the forge still lands (fallback profile).
|
||||||
|
- **Forge mode `strict`:** button disabled; caption "Waiting for the nightly read — next section after the agent runs."
|
||||||
|
- Both modes: small toggle row under the actions — "Forge: on demand ⇄ strict" (persists `prefs['forge_mode']`).
|
||||||
|
- When a forged batch reports `new: 0` the report shows: "Catalog fully reviewed — fresh listings arrive with the next 08:30 import; showing second looks." and the forge reads the same note in its status line (v1.1).
|
||||||
|
- Undo from the report screen re-opens the section with the last card (§2 undo semantics).
|
||||||
|
|
||||||
|
### 10.3 Card bands (the ONLY card-face additions)
|
||||||
|
|
||||||
|
- Position: top-left inset (12px); `pointer-events:none`; no layout shift; no effect on stack metrics or drag. 11px/650, letter-spacing .4px, radius 999px, padding 4px 9px, max-width 78%; may wrap to 2 lines.
|
||||||
|
- **WILDCARD — maybe you'll like this one instead** — color `#ffb43a`; bg `rgba(255,180,58,.14)`; border `1px solid rgba(255,180,58,.35)`.
|
||||||
|
- **SECOND LOOK — you {liked|maybed|noped} this** — color `#c4b5fd`; bg `rgba(124,58,237,.22)`; border `1px solid rgba(167,139,250,.65)`; font-weight 800. Wildcard stays amber (`#ffb43a` family values unchanged) for contrast. The same band renders in the dossier ("info view") when the listing's slot is `return` or `wildcard` (v1.1).
|
||||||
|
- No other text may sit on the card face in v3.
|
||||||
|
|
||||||
|
### 10.4 Tank gauge (match points, non-numeric)
|
||||||
|
|
||||||
|
- Small fuel-tank glyph; fill = match points (0–100). List rows: 18×22px in the trailing cluster, immediately left of the price, vertically centered. Dossier: 28×34px.
|
||||||
|
- Fill band colors: `<30` → `--nope`; `30–70` → `--maybe`; `>70` → `--like`. Track: `rgba(255,255,255,.12)`, radius 4px. Fill animates `.25s ease` on update.
|
||||||
|
- The List view NEVER shows raw numbers.
|
||||||
|
|
||||||
|
### 10.5 Dossier — "Why this rank"
|
||||||
|
|
||||||
|
- New block after chips, before "The machine":
|
||||||
|
- Gauge + "{points}%" (17px/700).
|
||||||
|
- Up to 4 breakdown lines, strongest first, muted: "· {feature} {±n}"; agent contributions render as "· agent: {why} {±n}".
|
||||||
|
- Footer: "from {N} strikes · read {relative}" (N = events count; time = profile version time).
|
||||||
|
- Hidden-by-default detail: the breakdown lines and the "from N strikes" footer render only when `prefs['rubric_debug'] === 'on'` (default `'off'`). With the pref off the block shows **the tank gauge + "{points}%"** only — the minimal rubric display. The toggle lives in the Trends "Actions" area (why-breakdown on ⇄ off, same control style as the forge-mode toggle) — v1.1.
|
||||||
|
- This is the only numeric display in the app (accepted — it exists for audit).
|
||||||
|
|
||||||
|
### 10.6 Trends tab (real)
|
||||||
|
|
||||||
|
Top→bottom:
|
||||||
|
|
||||||
|
1. **Your read** — lead line (18px/700) + "updated {rel}" muted; cold start copy: "Swipe ~10 more and the read sharpens."
|
||||||
|
2. **Leanings** — up to 8 bullets (14px).
|
||||||
|
3. **Stats** — three mini blocks: "Hit rate" ({likes}/{decided}) · "Returns re-struck" ({n}) · "Wildcards hit" ({n}).
|
||||||
|
4. **Since last read** — one muted line (hidden when null).
|
||||||
|
5. **Actions** — "Run agent now" (POST /api/agent/run; inline status) · forge-mode toggle · muted "Last read {time} · {model}".
|
||||||
|
|
||||||
|
- No LLM call at view time; the tab renders the stored profile only.
|
||||||
|
|
||||||
|
### 10.7 Wiring deltas
|
||||||
|
|
||||||
|
- Deck buffer source becomes `GET /api/section` (pinned order). All P2 wiring (optimistic swipe + retry queue, badge, SW) unchanged; bump the `sw.js` cache version.
|
||||||
|
- Card payload additions the FE reads: `slot` ('new'|'return'|'wildcard'), `points`, `tank` {value, band}, `round_next` (1|2, for the SECOND LOOK band copy when needed).
|
||||||
|
|
||||||
|
## 11. Photo experience v2 (B1) — galleries, photo flip, fullscreen viewer
|
||||||
|
|
||||||
|
**Frozen 2026-11-10 (Glenn approved: gesture map + repeat rotation picked by him in chat).** Additive; §1–§10 remain in force except where explicitly superseded here (§2's "Tap … → dossier" and §3's "(or tap)" are retired — §11.4).
|
||||||
|
|
||||||
|
### 11.1 Galleries
|
||||||
|
|
||||||
|
- Every listing carries its full photo gallery: `imgs[]` (page order, deduped by image id; entry 0 = the established primary = today's `img`/`img_url`).
|
||||||
|
- Every gallery image is mirrored locally (`data/img/{id}_{n}.webp`) and served by the app (`GET /img/{id}?i={n}`, `i` default 0; falls back into the §7.6 chain). The client never hotlinks directly.
|
||||||
|
- Zero photos → §2 placeholder; one photo → single photo, no segments, no flip.
|
||||||
|
|
||||||
|
### 11.2 Photo flip (deck card — top card only)
|
||||||
|
|
||||||
|
- Tap LEFT third → previous photo; tap RIGHT third → next photo. **Clamp at the ends** (no wrap). Tap test = §2's (≤6px movement on pointerup); a tap inside the ⓘ button hits ⓘ, not the zones.
|
||||||
|
- Segment indicator bars: one segment per photo along the top edge of the photo, centered, ~3px tall; current = solid white, others `rgba(255,255,255,.35)`; Tinder-style. (§10.3 amendment: when segments are present, band chips render at `top:22px`; no other change to §10.3.)
|
||||||
|
- Flip is instant, commits nothing, locks nothing (§2 THE law). A drag that starts anywhere still drags for LIKE/NOPE/MAYBE — the zones apply to taps only.
|
||||||
|
- Photo position is per-card-session state; a card re-shown fresh returns to §11.5's lead photo.
|
||||||
|
|
||||||
|
### 11.3 Fullscreen viewer
|
||||||
|
|
||||||
|
- Tap the photo in the dossier hero → fullscreen viewer opens (starts at photo 0). Direct card→viewer is retired (see §11.4); the viewer is reached from the dossier (v1.1).
|
||||||
|
- Viewer: horizontal swipe = prev/next (clamp); pinch-zoom + double-tap = zoom (1×–~4×); segment bars stay visible; close via ✕ (36px, top-left) or pull-down (grabber or photo, `dy > 110` — the §3 idiom). Closing returns to the same card, same photo position; deck state untouched.
|
||||||
|
- Also opens from the dossier hero (starts at photo 0). Same sheet geometry as the dossier (z-band 69/70), photo-first: no body, no actions row.
|
||||||
|
- Viewer close (✕, pull-down, or Esc) returns to the dossier; the next Esc returns to the deck (v1.1).
|
||||||
|
|
||||||
|
### 11.4 Doors (supersedes the old tap→dossier)
|
||||||
|
|
||||||
|
- ⓘ (top-right of card) → dossier (unchanged). The card face now owns: **sides = flip, center = dossier (bike data first)**; the "tap anywhere → dossier" gesture is RETIRED and the B1 center-tap→fullscreen gesture is RETIRED in favour of it (v1.1).
|
||||||
|
- Tapping the photo in the dossier hero opens the fullscreen image viewer (§11.3). Viewer close (✕, pull-down, or Esc) returns to the dossier; the next Esc returns to the deck. The dossier stays reachable from ⓘ and from list rows (§3/§4 unchanged).
|
||||||
|
|
||||||
|
### 11.5 Repeat rotation
|
||||||
|
|
||||||
|
- A listing presented again (second look / re-show) opens on a different photo than the last one viewed: lead = `(last_photo_idx + 1) % n`; no stored index → photo 0.
|
||||||
|
- The index is persisted server-side at swipe-commit time (survives sessions and devices; works with §7.2's optimistic retry queue).
|
||||||
|
- List thumbnails and the dossier hero keep photo 0 (the canonical shot) — rotation is deck-only.
|
||||||
|
|
||||||
|
### 11.6 Performance & invariants
|
||||||
|
|
||||||
|
- Preload the top card's next photo and the next card's lead photo; decode off the critical path; **no network on the swipe path** (all mirrored locally).
|
||||||
|
- Stack metrics, `.topcard`, z-order law, stamps, undo, no-lock — all §2 laws untouched. Bands and segments coexist per §11.2.
|
||||||
|
|
||||||
|
### 11.7 Acceptance (B1)
|
||||||
|
|
||||||
|
Machine-checkable:
|
||||||
|
- Gallery capture ≥1 photo for every listing with a page image; multi-photo listings show N segments = N photos; the mirror holds all gallery files; `/img/{id}?i=n` answers for every n.
|
||||||
|
- Flip: left/right taps advance and clamp; a tap never commits (no stamp flash, no flight); drag-commit regression passes (fast-chain, no-lock, one `.topcard`).
|
||||||
|
- Viewer: opens from center tap and dossier hero; swipe/zoom/close work; close restores the card and photo position.
|
||||||
|
- Rotation: a re-shown card leads at `last+1`; persisted across page reload (server-side).
|
||||||
|
- Fallbacks: 0-photo placeholder, 1-photo (no segments), broken file → §7.6 chain.
|
||||||
|
- All §9/§10 acceptance re-run green.
|
||||||
|
|
||||||
|
Human: Glenn flips photos on his phone, opens fullscreen, and sees a fresh photo on a repeat — the B1 exit criterion.
|
||||||
|
|||||||
624
docs/V1.1-BUILD.md
Normal file
624
docs/V1.1-BUILD.md
Normal file
@@ -0,0 +1,624 @@
|
|||||||
|
# V1.1-BUILD — v1.1 implementation specification
|
||||||
|
|
||||||
|
Build spec for the v1.1 QOL batch. Requirements: `docs/BACKLOG.md` "v1.1 — QOL wishlist" (both subsections). Frozen UI contract this amends: `docs/UI-SPEC.md` (v2 + P3 §10 + B1 §11). Version map: `docs/PLAN.md`.
|
||||||
|
|
||||||
|
**Status:** APPROVED 2026-11-12 (Glenn): ≥2-second-looks default confirmed (rich day = 21 fresh + 2 second looks + 2 wildcards); violet band; Backspace kept as undo alias. Build authorized — implementation in progress on `dev`. Every item below states goal, exact change plan (file/function/line anchors verified against the code at `381b6c8`), pseudocode where the logic is heavy, edge cases, acceptance criteria, and the verification steps the parent will run. Nothing is built yet.
|
||||||
|
|
||||||
|
**No data reset. No schema change. v1.1 is frontend + read-path/query only** (see "Data safety"). The only logic rewrite is the compose allocation itself (`compose_section`), which still writes *only* what it writes today (a new `sections`/`section_items` row set, exactly as a forge already does).
|
||||||
|
|
||||||
|
## Code facts everyone should start from (verified 2026-11-12, commit `381b6c8`)
|
||||||
|
|
||||||
|
Backend:
|
||||||
|
|
||||||
|
- `app/sections.py` (488 lines): `NEW_POOL_SQL` (40–44), `RETURNS_SQL` (46–55), `WILDCARD_SQL` (57–61), constants `FAMILY_CAP=4 MAX_RETURNS=5 MAX_RETURN_NOPES=2 WILDCARDS=2 DEFAULT_SIZE=25 MIN_BANDS=3` (28–33), `pick_new` (160–178), `repair_bands` (181–256), `compose_section` (259–404), `section_items` (417–425), `section_counts` (428–454), `maybe_exhaust` (457–466), `read_section` (469–488).
|
||||||
|
- `app/api.py` (738 lines): `/api/stats` (234–264), `swipe` POST (299–357; `section_id` optional at 307–309, `append_event` call at 345–346), `section_state` (446–471), `get_section` (473–488; cold compose hardcodes `size=25` at 482–485), `forge` (490–527), `/api/prefs` (652–666; `allowed = {'forge_mode': ('ondemand','strict')}` at 656), `img` with `object-fit:contain` viewer served at 670–708.
|
||||||
|
- `app/db.py`: `swipe_events` schema with `section_id` (18–26), `append_event` (120–143) — already stamps `section_id` when the client sends it; P3 seeding copies events with `section_id=NULL` (111–117).
|
||||||
|
|
||||||
|
Frontend: `app/static/index.html` (1282 lines, single-file app):
|
||||||
|
|
||||||
|
- Keyboard handler ALREADY EXISTS at 1263–1270 (see item 1 — this must be reconciliated with the BACKLOG key map).
|
||||||
|
- Tap zones: `tapZone` (756–763) — sides flip, center opens viewer. Built faithful to B1 §11.4. v1.1 changes the center-target only.
|
||||||
|
- Deck→dossier door: `.ibtn` click (578–580); `openDetail` (768–796); dossier hero with `dphoto` (245) and hero-tap→viewer in `dEndH` (832–837).
|
||||||
|
- Viewer: `openViewer` (870–877), `closeViewer` (878–883), `vCtx={item,idx,back}` (853, 873); `.vimg` (197) already `object-fit:contain`.
|
||||||
|
- Card DOM: `phwrapHTML` (512–513) puts `<img class="photo">` inside the `.phwrap`; `.card .photo` is `object-fit:cover` (CSS 36).
|
||||||
|
- Header: `#pill` (206, filled by `loadStats` 1127–1136 — it renders `live · new` from `/api/stats`), `#sect` (207) is the section progress pill, `.sect` CSS 137–138, `.pill` 29–30.
|
||||||
|
- Empty state: `#empty` (212) with copy "You're all caught up."; CSS 95–97; shown only by `maybeRefill` in **legacy mode** (652) — unreachable in section mode, where the report card (`#report`, 213–224, shown at 660–662) owns exhaustion.
|
||||||
|
- Section counter: client state `sectDecided/sectTotal` (1139); `paintSect` (1140–1145); increments in `commit` (684) and `undo` (706); server truth re-derived on every `updateSectionUI` (1157–1173) from `body.items[].decision` — the bug: `decision` is the listing's *lifetime* verdict, not a this-section decision.
|
||||||
|
- `bandHTML` (482–489); `.band` (139), `.band.wild` (140), `.band.sl` (141); `hasSegs` band offset (188); `updateSectionUI` (1157); `forge` button flow (1180–1206); prefs pattern `setMode` (1174–1179); `loadStats` (1127).
|
||||||
|
- Info-page hero: `.dphoto` (70) is `object-fit:cover`; list thumbs `.thumb` (110) are `cover` and stay that way.
|
||||||
|
- Rubric display: dossier "Why this rank" block `#drank` (250–257) filled by `fillRank` (798–821) — gauge + `{points}/100` (805) + up to 4 breakdown lines (806–812) + footer (813–814); list rows show tank only (985).
|
||||||
|
|
||||||
|
Discrepancies found between docs and code (code wins; both recorded):
|
||||||
|
|
||||||
|
- B1 §11.4 says center-tap = fullscreen viewer. Code matches. v1.1 re-maps center-tap = dossier (BACKLOG UI change 1) — the UI-SPEC amendment in this doc supersedes §11.3/§11.4.
|
||||||
|
- `sections.py` module docstring (2–17) and `docs/RANKING.md:29` still say "≤5 returns"; DATA-MODEL line 165 says "Max 5 returns per section". All replaced by item 9's formula (docs to update: RANKING.md:29, DATA-MODEL.md:165, the module docstring — the spec's one doc-touch, see item 9).
|
||||||
|
- The 2026-11-12 empty-pool symptom ("forges returned 2-card sections") means the eligible-return pool is *near-empty today*; details in item 9.
|
||||||
|
- Current keyboard handler maps **Backspace**, not ArrowDown, to undo, and does not prevent arrow page scroll — item 1 fixes this.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 1 — Keyboard shortcuts
|
||||||
|
|
||||||
|
**Goal (BACKLOG "Shortcuts"):** ← nope, → like, ↑ maybe, ↓ undo (back one), Esc exits fullscreen views. Desktop/Bluetooth-keyboard benefit; no touch changes.
|
||||||
|
|
||||||
|
Notes before the plan:
|
||||||
|
|
||||||
|
- The expected row of actions shares handlers: commits go through `commit()` (`index.html` 677–689; button bindings at 721–723), undo through `undo()` (702–720; binding 724). The keys must call these exact functions so all P3 invariants (optimistic persist through `setDecision`, photo_idx, no-lock flight) hold — never bespoke handlers.
|
||||||
|
- A keyboard handler already exists (1263–1270): ← / → / ↑ do the right things, but undo is **Backspace**, ↓ is unmapped, Esc is unmapped, there is no scroll guard, and the text-input guard covers only `INPUT`.
|
||||||
|
|
||||||
|
**Plan (all in `app/static/index.html`):**
|
||||||
|
|
||||||
|
1. Replace the handler (1264–1270) with the v1.1 key map:
|
||||||
|
```
|
||||||
|
ArrowLeft -> nope (commit(pile[0].el,'nope'))
|
||||||
|
ArrowRight -> like (commit(pile[0].el,'like'))
|
||||||
|
ArrowUp -> maybe (commit(pile[0].el,'maybe'))
|
||||||
|
ArrowDown -> undo (undo())
|
||||||
|
Backspace -> undo (undo()) // kept: existing users' muscle memory — note in Open Questions
|
||||||
|
Escape -> exit fullscreen views (below)
|
||||||
|
```
|
||||||
|
2. Guard before acting: **ignore all keys while a text input has focus.** Extend the existing `e.target.tagName==='INPUT'` check to also skip `TEXTAREA`, `SELECT`, or any element with `isContentEditable` (the list screen has a live search input; searching with arrows must filter, not swipe):
|
||||||
|
```
|
||||||
|
const t=e.target;
|
||||||
|
if(t && (t.tagName==='INPUT'||t.tagName==='TEXTAREA'||t.tagName==='SELECT'||t.isContentEditable)) return;
|
||||||
|
```
|
||||||
|
3. **Prevent arrow-key page scroll** (the app body is `overflow:hidden`, but focused buttons can still scroll; be safe): for the four arrow keys, `e.preventDefault()` immediately after the guard passes — i.e. before letting the browser's default horizontal/vertical scroll run. Backspace default is harmless and untouched.
|
||||||
|
4. **Esc, one key, stack order** (viewer `#viewer` z-72 > dossier `#detail` z-70 > deck):
|
||||||
|
- if `#viewer` has class `on` -> `closeViewer()` (878)
|
||||||
|
- else if `#detail` has class `on` -> `closeDetail()` (822; returns to deck or, from a list row, to the list screen that stays open underneath — same as today's pull-down close)
|
||||||
|
- else if `#report` has class `on` -> do nothing (Esc never forges or navigates tabs)
|
||||||
|
- else -> ignore.
|
||||||
|
Note: the currently-open sheet also swallows keys before Esc gets them, because the existing early-return for open `#detail`/`#viewer` is removed in favour of the Esc-first ordering above. Esc only closes; arrows only work on the deck.
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- Arrow keys must do nothing when no pile: `commit` and `undo` already no-op on empty `pile[0]`/`hist` (677–678, 703).
|
||||||
|
- Key repeat (holding →): `commit` drains the pile as fast as repeat fires — same behaviour as mashing the like button, acceptable, no extra lock needed (the no-lock law already tolerates fast commits).
|
||||||
|
- The report card is up but the section is closed: arrows no-op (pile empty); Esc no-ops; the Forge button remains mouse/tap only.
|
||||||
|
|
||||||
|
**Acceptance criteria (objective):**
|
||||||
|
|
||||||
|
1. New chromium/headless page, deck served: dispatch `ArrowLeft/Right/Up` -> top card flies nope/like/maybe; the very same network payloads fire as button taps (verify no second code path: instrument or assert `sectDecided`/paintSect side effects identical).
|
||||||
|
2. Dispatch `ArrowDown` -> the swiped card returns to the top of the pile (undo flight + `DELETE /api/swipe/{id}` queued).
|
||||||
|
3. Dispatch `Backspace` -> same undo.
|
||||||
|
4. Focus the list search input, type arrows -> input cursor moves / text unchanged, zero swipe commits (assert pile length unchanged).
|
||||||
|
5. While the dossier is open, all arrow keys are inert; Esc closes it. While the viewer is open, Esc closes only the viewer (dossier, if underneath, stays open; a second Esc closes it).
|
||||||
|
6. Call `window.scrollTo(0,100)`-style check: with `document.body.scrollTop` manipulable, arrow dispatch does not change scroll (assert via `e.defaultPrevented` spy on `keydown`).
|
||||||
|
|
||||||
|
**Parent verification:**
|
||||||
|
|
||||||
|
1. `fe_walk_b1`-style headless walk: boot app, run acceptance 1–6 in order, assert green.
|
||||||
|
2. Optional human spot-check on MacBook over the tailnet.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 2 — Info-first tap flow
|
||||||
|
|
||||||
|
**Goal (BACKLOG UI changes 1):** center tap → bike data dossier first; tapping the photo there → fullscreen image viewer; side taps still flip photos; Esc walks back out (viewer → data → deck). Resolves "can't get info unless I hit the ⓘ button". Esc routing is item 1's stack; this item changes the tap targets + the viewer's back-chain.
|
||||||
|
|
||||||
|
**Current state:** `tapZone` (756–763) sends the center third to `openViewer(entry.d, entry.cur, {type:'card',entry})`; side thirds flip (`flipCard`, 525–536). `openDetail` is only reachable via ⓘ (578–580) / list rows (1036) / matches rows (1113). Dossier hero tap opens the viewer (832–837). Viewer close returns to whatever was under it (878–883).
|
||||||
|
|
||||||
|
**Plan (all `app/static/index.html`):**
|
||||||
|
|
||||||
|
1. `tapZone` (761): change the center branch to `openDetail(entry.d, {type:'deck'})`. Sides unchanged. Do not clear the card's photo position — the dossier is a sheet over the deck.
|
||||||
|
2. **Esc walk-back chain (new):** `openDetail` (768) must record *how it was opened* so a viewer reached from it can walk back. Add one module-level variable, e.g. `let dEntry=null;` — set in `tapZone`'s new branch to `{...dCtx, entry}` (the pile entry with its live `cur` photo index), set to `null` for row/matches opens (1036, 1113) and for ⓘ (580). `closeDetail` (822) clears it.
|
||||||
|
3. Viewer back-chain: when `tapZone`'s center branch opens the dossier, the dossier hero keeps its existing tap→`openViewer(dData,0,{type:'dossier'})` (835). Extend the viewer context so it can walk back to the dossier-on-deck:
|
||||||
|
- in `openViewer` (870–877): pass anextra field on `back`, e.g. `{type:'dossier', walkBack:dEntry}` or simpler: make the dossier-hero call (835) pass `{type:'dossier', backToDetail:dEntry}`;
|
||||||
|
- item 1's Esc handler calls, before plain `closeViewer()`, a new tiny `vWalkBack()`: if `vCtx.back.backToDetail` exists, capture the item, call `closeViewer()`, then `openDetail(item, backToDetail)` — image returns to the deck dossier (photo 0, per §3's hero contract), where the *next* Esc closes to the deck. If no `backToDetail`, plain `closeViewer()`.
|
||||||
|
4. The dossier closes to exactly the deck it came from: when opened from a deck card, the ⓘ/pull-down close semantics (822) already leave the pile untouched; no change. From a list row, Esc closes to the list (already true).
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- One-photo / no-photo cards: center tap still opens the dossier (data-first is the point); the hero tap no-ops when `dData.img` is null (835 already guards).
|
||||||
|
- The dossier's own action buttons (838–848) still commit the current top card — unchanged; the flow is open data, read it, swipe from inside it. That is desired: dossier actions exist today.
|
||||||
|
- Viewer opened the old way from a deck card — gone: after item 2 there is no direct card→viewer path; every deck-carded viewer is reached via dossier. `{type:'card',entry}` back shape (873) becomes unused from the deck; leave the plumbing, back-chain field only added for the dossier path.
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Center tap (≤6px movement) on the top card opens `#detail` (`.on`), populated with that card's payload; **no** viewer opens; no commit fires.
|
||||||
|
2. Side taps still flip photos both directions, clamped; no dossier opens (existing B1 §11.2 behaviour re-verified).
|
||||||
|
3. Tap the dossier hero photo -> `#viewer` opens at photo 0. Esc -> viewer closes, dossier still open. Esc -> dossier closes, same deck/pile state and same top card (photo index intact).
|
||||||
|
4. ⓘ tap still opens the dossier (regression).
|
||||||
|
5. From the list: row tap -> dossier -> hero tap -> viewer -> Esc -> dossier -> Esc -> list (regression with the new Esc routing).
|
||||||
|
|
||||||
|
**Parent verification:**
|
||||||
|
|
||||||
|
1. Headless walk of acceptance 1–5 plus the B1 §11.7 suite re-run green (flip clamps, viewer swarm, no-lock, one `.topcard`).
|
||||||
|
2. Human: phone pass — Glenn taps the middle of a card, sees data, taps the photo, sees fullscreen, Esc/Esc (desktop) or ✕/pull-close (touch) walks back out.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 3 — Photos = option C (fit + blurred backdrop)
|
||||||
|
|
||||||
|
**Goal (BACKLOG UI changes 2, decided 2026-11-12: "OH C! I love C!"):** the whole photo is shown uncropped (fit) over a soft blurred backdrop of itself, on the deck card AND the info-page hero. Fullscreen viewer unchanged (already fits). List thumbs stay `cover` (56px, irrelevant). Demo reference panel C: `~/dev/bike-tinder-demos/fit-vs-fill.html`.
|
||||||
|
|
||||||
|
**Verified current CSS (index.html):**
|
||||||
|
|
||||||
|
- Deck card photo: `.card .photo{position:absolute;inset:0;width:100%;height:100%;object-fit:cover}` (36) — the element is inside `.phwrap` (513).
|
||||||
|
- Info hero: `.dphoto{width:100%;height:100%;object-fit:cover;display:block}` (70) inside `.dhero` (69; bg `#17171c`).
|
||||||
|
- Viewer: `.vimg{...object-fit:contain...}` (197) — already fits; **verify only, change nothing.**
|
||||||
|
- List thumbs: `.thumb{...object-fit:cover...}` (110) — **unchanged.**
|
||||||
|
|
||||||
|
**Plan (CSS-only, `app/static/index.html` <style>):**
|
||||||
|
|
||||||
|
1. Deck card:
|
||||||
|
- change line 36 to `object-fit:contain` (keep `position:absolute;inset:0;width:100%;height:100%`).
|
||||||
|
- add inside `.phwrap` (512–513) a backdrop layer behind the sharp photo. HTML: when a `src` exists render `<div class="phblur"></div><img class="photo" ...>` (use an inline `style="background-image:url('...')"` from the same `photoSrc` value; escape the URL-attrs as the file already does elsewhere — the src is app-server-generated, but quote-escape it to be safe). CSS:
|
||||||
|
```
|
||||||
|
.phblur{position:absolute;inset:-18px;background-size:cover;background-position:center;
|
||||||
|
background-repeat:no-repeat;filter:blur(22px) brightness(.55) saturate(1.1);transform:scale(1.08)}
|
||||||
|
```
|
||||||
|
The photo gets a touch of shadow so the edge reads on top of the blur: `filter:drop-shadow(0 6px 24px rgba(0,0,0,.45))` on `.card .photo`. The `nophoto` fallback path (513) renders no `.phblur`, naturally.
|
||||||
|
- `container` presence check: `.card .grad` (37) sits over the fit area — fine (it is a scrim, UI-SPEC §2).
|
||||||
|
2. Info-page hero:
|
||||||
|
- change line 70 to `object-fit:contain`.
|
||||||
|
- give `#dphoto` the same twin: add a `.dblur` div inside `.dhero` (245) *behind* the image, styled identically to `.phblur` (`.dhero` has no `overflow:hidden`; add it). Populate on `openDetail` (772 area): same `data.img` URL, emptied when no photo / error fallback (774). Simplest robust approach: reuse a tiny helper `setBackdrop(shellEl, url)` called from `openDetail` and `phwrapHTML`'s error/success paths.
|
||||||
|
3. No-reflow guarantees: both fits put letterbox bars around the photo; because the photo is `position:absolute` and the blur is a sibling underneath, nothing shifts. Card title/text keep their spot (info block is below the fold of a 4:3-ish area; on unusually tall images the photo shrinks towards the center and the bottom gradient may clip more of it than today — accepted, that is the point of "uncropped").
|
||||||
|
4. Cost note: `filter:blur(22px)` per card × up to 5 cards. GPU-composited in Chrome/Safari; on the older iPhone, if paint cost shows, drop the blur radius to 16px or disable the backdrop layer when `prefers-reduced-motion` — implementer's knob, not a spec change.
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- 0-photo listing: `.nophoto` panel unchanged, no backdrop, dossier hero dimmed as today.
|
||||||
|
- Aspect extremes (portrait screenshots inside 16:9 crops): letterbox grows as the photo gets wider/taller; clamp with a `max-height:100%` implicit via fit (contain already does), nothing else needed.
|
||||||
|
- B1 preloader (538–551) fetches `photoSrc` — unaffected.
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Computed style on a rendered deck card photo == `object-fit:contain`; a backdrop node exists per card and its background-image equals the photo's resolved URL; paddings/sizes of the info block are unchanged vs pre-change (measure before/after in the headless walk).
|
||||||
|
2. Dossier hero photo == `contain`, backdrop present, spec table layout unmoved.
|
||||||
|
3. Viewer `.vimg` computed style == `contain` (no diff), list `.thumb` == `cover` (no diff).
|
||||||
|
4. 0-photo card and 1-photo card still render their fallbacks (B1 §11.7 fallback checks re-run).
|
||||||
|
|
||||||
|
**Parent verification:** headless computed-style assertions 1–4 + half a dozen screenshots (portrait + landscape cards, dossier, viewer) eyeballed against the demo's panel C.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 4 — Minimal rubric + debug "why" toggle
|
||||||
|
|
||||||
|
**Goal (BACKLOG UI changes 3):** bike views show ONLY the gas-tank gauge + percent full (e.g. "76%"); a setting re-enables the full "why" breakdown for debugging. Wired via the existing `prefs` key/value API (same pattern as `forge_mode`).
|
||||||
|
|
||||||
|
**Where rubric displays today (index.html):**
|
||||||
|
|
||||||
|
- Dossier "Why this rank" block `#drank` (250–257), filled by `fillRank` (798–821): gauge (`drankTank`), `{points}/100` (805), breakdown lines (806–812), footer (813–814).
|
||||||
|
- List rows: tank gauge only, inside `rowHTML` (985) — already minimal; **untouched**.
|
||||||
|
- Deck card face: no rubric display — nothing to do.
|
||||||
|
|
||||||
|
**Plan:**
|
||||||
|
|
||||||
|
Backend (`app/api.py`):
|
||||||
|
|
||||||
|
1. Extend the allowed prefs set (656): `allowed = {'forge_mode': ('ondemand','strict'), 'rubric_debug': ('off','on')}` — the key/value API then accepts `{key:'rubric_debug', value:'off'|'on'}` with zero new plumbing (the endpoint at 652–666 is generic).
|
||||||
|
2. Expose the value so the FE can paint: add to `section_state` (446–471) one field, e.g. `data['prefs'] = {'rubric_debug': get_pref(conn, 'rubric_debug', 'off')}` (same read shape as `data['mode']` at 453). `/api/section` and the forge response already go through `section_state`, so every section load carries it. No `/api/stats` change needed.
|
||||||
|
3. Nothing else backend-side: the breakdown payload (`points_breakdown`) is already in section items (`include_breakdown=True`, 454–456) — cheap to keep sending.
|
||||||
|
|
||||||
|
Frontend (index.html):
|
||||||
|
|
||||||
|
4. `fillRank` (798–821) renders by default: the gauge (existing 804) + compact "76%" label. Change 805 from `data.tank.value+'/100'` to `data.tank.value+'%'` and move the label into the existing left gauge column or directly beside it (e.g. wrap gauge + `%` in the `.tankwrap`).
|
||||||
|
5. Guard the detailed parts behind the pref: build the `dlines` HTML (806–812) and the `dfoot` line (813–814) ONLY when the pref is `'on'`; otherwise empty both and keep `#drank` visible with just gauge + `%` (the block itself persists so the minimal state is still "gauge + %", matching the BACKLOG ask).
|
||||||
|
6. Toggle UI (findable by Glenn, harmless otherwise): add to the Trends "Actions" area (317–320) a second `.trow` exactly in the `setMode` style (1174–1179 + 1248–1249): "Why breakdown: on ⇄ off", both spans wired like `tOnd`/`tStr` to a new `setRubricDebug(m)` that POSTs `/api/prefs` and updates the sections-wide `section.prefs.rubric_debug` for the next `fillRank`. Persist per session: `updateSectionUI` (1157–1173) also stores `section.prefs` (already via body) so a re-loaded deck repaints correctly.
|
||||||
|
7. `fillRank` reads `((section&§ion.prefs&§ion.prefs.rubric_debug)==='on')`; also keep the existing on-the-quiet `/api/listing/` enrichment for list rows (816–819) guarded the same way — when off, don't fetch (saves a request; nothing is displayed).
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- Legacy (no `/api/section`) mode: `section` is null → pref off → minimal display; the API call for the toggle then target isn't the issue (legacy servers return 404 on `/api/prefs` POST; FE silently ignores failure, `setMode` precedent 1175–1176).
|
||||||
|
- `tank.value` null (unscored/deep rows): `fillRank` already hides the whole block (799–802) — unchanged.
|
||||||
|
- The percent must be the rounded tank value (0–100 clamp is already applied in `tankSVG` 455).
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Dossier for a scored listing shows gauge + "NN%" and NO breakdown lines, NO footer line, on a fresh load (pref unset).
|
||||||
|
2. `POST /api/prefs {rubric_debug:'on'}` → 200 `{key,value}`; reload → same dossier shows breakdown lines + footer. `'off'` → they disappear. Invalid value → 400.
|
||||||
|
3. List rows and card face carry zero rubric text in both states (regression).
|
||||||
|
4. Pref survives server restart (SQLite `prefs`) and page reload; forge response carries it.
|
||||||
|
|
||||||
|
**Parent verification:**
|
||||||
|
|
||||||
|
1. TestClient: prefs happy path / 400 path; fresh-DB `/api/section` body includes `prefs.rubric_debug`.
|
||||||
|
2. Headless: dossier DOM assertions with the pref off and on (steps A2).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 5 — Section counter fix (the 2/2 → 3/2 bug)
|
||||||
|
|
||||||
|
**Goal (BACKLOG "Counter bug"):** count a card as decided only if decided DURING this section (server-side per-item flag, from events stamped with this section_id — the source `section_counts` uses); clamp to total; no premature "hot"/complete state.
|
||||||
|
|
||||||
|
**Root cause (verified):** `section_items` (sections.py 417–425) LEFT-JOINs `swipes` so items carry `decision` = lifetime verdict. A second-look card enters its section with `decision='like'|'maybe'|'nope'` already set, but it has NOT been decided in this section (no event with `section_id` = this section). `read_section` progress (473) and the FE (`updateSectionUI` 1160–1164) count those as decided → fresh batch paints "2/2" before any touch; the next swipe increments optimistically (`commit` 684) → "3/2". Fix = use only events with `e.section_id = section.id` as the decided set, plus clamping at the FE.
|
||||||
|
|
||||||
|
**Plan:**
|
||||||
|
|
||||||
|
Server (`app/sections.py`):
|
||||||
|
|
||||||
|
1. Add a helper next to `section_counts`:
|
||||||
|
```sql
|
||||||
|
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 = ? -- decisive: an event stamped in THIS section
|
||||||
|
```
|
||||||
|
as `section_decided_ids(conn, section) -> set(listing_id)`. (Filtering to `si.listing_id` membership keeps out-of-section stray events from a foreign section_id honest; the join is the same shape `section_counts` uses at 439–451.)
|
||||||
|
2. Add the per-item flag on the read path: `section_items` cannot see events per row without a second join; cheaper — after building `items` in `read_section` (471–472), set `item['decided_this_section'] = item['id'] in decided_ids`. (`sqlite3.Row` is read-only until cast; either materialize items as dicts there or attach the flag in `api.section_state` (447–457) where items are rebuilt as full payloads anyway — recompute `section_decided_ids` once in `section_state` and add `p['decided_this_section'] = p['id'] in ids` alongside `p['slot']` at 454–456. Prefer this: one read, no Row mutation.)
|
||||||
|
3. `read_section` progress (473): `decided = sum(1 for it in items if it['decided_this_section'])`. `maybe_exhaust` (457–466) **unchanged** — it must wait for real in-section verdicts too, which it now does automatically because `section_items` is ALSO the joined-swipes view it reads... No: `maybe_exhaust` reads `it['decision'] is not None`. Change line 462 to check `section_decided_ids(conn, section)` membership instead: `undecided = [it for it in items if it['id'] not in decided_ids]` and `if not undecided: close_section(...)`. Otherwise a returns-only section reports "complete" before a single strike. This is part of the same fix.
|
||||||
|
4. `section_counts` (428–454) already derives like/maybe/nope from `decision` (432–438) — same lifetime-verdict flaw in *report numbers*. Change its per-item loop to use the same decided-ids set: count an action for this section ONLY when an event with this `section_id` exists and matches (`SELECT e.listing_id, e.action ... WHERE e.section_id=?` joined to `section_items` the same way; the existing `restruck`/`wildcard_hits` queries at 439–451 already do exactly this join — mirror it). Note: this does not change what is *stored*, only what the report shows.
|
||||||
|
5. No DB writes anywhere in this item (flag is computed per read).
|
||||||
|
|
||||||
|
Frontend (`app/static/index.html`):
|
||||||
|
|
||||||
|
6. `updateSectionUI` (1157–1173) counting loop (1160–1163) → `if(local===undefined ? !!it.decided_this_section : local!=null) decided++`. (Local intent still wins while a swipe's POST is in flight — the optimistic window is seconds and cannot resurrect the 2/2 bug because it only ever reflects what the user just did in this session.)
|
||||||
|
7. Clamp everywhere `sectDecided` is incremented, so a stale/foreign path can never paint past the total: `commit` (684) → `sectDecided=Math.min(sectTotal, sectDecided+1)`; `undo` (706) already clamps the floor. `paintSect` (1140–1145) additionally renders `Math.min(sectDecided, sectTotal)`.
|
||||||
|
8. Hot state: `paintSect` toggles `hot` only when `sectDecided>=sectTotal` (1144, unchanged code) — with in-section counting this now fires only when the section truly has a verdict per card; `commit` keeps painting immediately after a full sweep (the server resync in `maybeRefill` 663–666 handles the exhausted flip and a rapid double-swipe resync on deck reload straightens the numbers, same as today).
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- **Undo of a swipe on a second look (return slot) that already has a lifetime verdict:** the DELETE restores the prior `swipes` row and clears no `section_id` event (out of scope — see Data safety), so the server's in-section decided count still contains that card until the section dies. The FE decrements optimistically (706), then a resync re-counts it. Net: `sectDecided` may show one more decided than the user feels, but NEVER past `sectTotal` thanks to the clamp, and never "hot" before the section is substantively done. This same artifact exists today, worse. Flagged in Open Questions (a true fix needs an event-deletion rule — data semantics, v1.2 territory).
|
||||||
|
- List/matches edits during a section: swipes posted WITHOUT `section_id` (setDecision from rows, 844–845) → no event with this section_id → a card in the section whose verdict was flipped from the list is (correctly) not counted as decided-in-section. The deck will simply re-offer it if still in buffer — acceptable and consistent.
|
||||||
|
- **Seeded legacy events have `section_id=NULL`** (db.py 111–117): they never count for any section — exactly the intended behaviour (they predate sections).
|
||||||
|
- `decided_this_section` must be present for ALL items in `section_state` payloads or the FE `local===undefined` branch misfires — set it unconditionally (not only for returns).
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Compose a returns-only section (fresh pool 0 — reproducible by DELETE-ing all fresh rows' absence or staging a DB where every active listing has a swipe): `/api/section` `progress.decided == 0` even though every `item.decision` is non-null; every `item.decided_this_section == false`.
|
||||||
|
2. Post one swipe with `section_id`: progress 1/2; recount matches events; `paintSect` shows 1/2. Post the second: 2/2, `hot` class present, section flips to `report` on next read, report counts reflect the two verdicts (not lifetime).
|
||||||
|
3. With 2 items and a double-swipe (two commits before any resync): painted pill never exceeds `2/2` (clamp assertion).
|
||||||
|
4. A section that mixes new + return: decided counts only in-section events on both slots; a fresh item swiped from the LIST (no section_id) leaves `progress.decided` unchanged.
|
||||||
|
5. Full P3 regression: compose→strike→rounds→report→forge loop green; undo restores the previous pill value minus one.
|
||||||
|
|
||||||
|
**Parent verification:**
|
||||||
|
|
||||||
|
1. Selfcheck additions in `selfcheck.py` (new checks against a temp DB, same style as existing) + the staged-DB walk in acceptance 1–4.
|
||||||
|
2. Live-DB dry run: snapshot `swipe_events`, compute `section_decided_ids` for the current section, assert it matches the user-visible count story. No writes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 6 — Dry-catalog messaging
|
||||||
|
|
||||||
|
**Goal (BACKLOG "Dry-catalog messaging"):** whenever a composed/forged batch reports `new: 0`, surface "Catalog fully reviewed — fresh listings arrive with the next 08:30 import; showing second looks" in the UI and label forge results accordingly, so the deck never silently drips 2 cards.
|
||||||
|
|
||||||
|
**Why it happens:** `NEW_POOL_SQL` (sections.py 40–44) is empty once every active listing has a swipe row; `compose_section` still forges from `RETURNS_SQL` — under v1 formula a few cards (night-of-symptom: 2). By design, but invisible.
|
||||||
|
|
||||||
|
**Plan:**
|
||||||
|
|
||||||
|
Server: the recipe already carries the number. `compose_section` returns `recipe: {'new': ..., 'return': ..., 'wildcard': ...}` (377–386) and `section_state` (446–471) already exposes `recipe` in `read_section` (479–480). **No server change required for the number itself.** Optional hardening: `forge` (490–527) appends to its `note` when the composed recipe has 0 new — e.g. `note += ' · catalog fully reviewed'` — so `section_state(note=...)` returns a labeled state (469–470); the FE below is the primary surface.
|
||||||
|
|
||||||
|
Frontend (index.html):
|
||||||
|
|
||||||
|
1. Shared renderer: a function `dryLabel(body)` → `body.recipe && body.recipe.new===0` (and `body.items.length>0`, i.e. there IS a section being served) → the BACKLOG sentence. Wire it into both hydrations:
|
||||||
|
- `updateSectionUI` (1157): show/hide a new caption node in the report block — reuse `#strictCap` pattern (223): add `<div class="rcap" id="dryCap">Catalog fully reviewed — fresh listings arrive with the next 08:30 import; showing second looks</div>` to `#report` (212–224 area; display:none default), toggled by `dryLabel`. Report is where the deck lands after the batch, so the message persists exactly where Glenn looks.
|
||||||
|
- `btnForge` success path (1188–1198): when the forge response is dry → `$('forgeStatus').textContent = dry text` (instead of blanking at 1189–1190) and `dryCap` stays visible via the `updateSectionUI(body)` call (1197).
|
||||||
|
2. Fresh inventory restored mid-session (import lands): `loadSection` already refetches on reload/resync — `updateSectionUI` recomputes `dryLabel` every time; no timer needed.
|
||||||
|
3. Copy stays exactly the BACKLOG sentence (keep "08:30" hardcoded as today — same assumption the empty state makes).
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- Dry + zero items (nothing left at all): item 8's loud empty owns that state — `dryCap` must NOT show there (guard on `items.length>0`).
|
||||||
|
- `recipe` absent/legacy API: no label; `dryLabel` returns false (body check).
|
||||||
|
- Forge in `strict` mode can't be run anyway; the caption still paints from `GET /api/section` on load.
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Forge with `recipe.new:0` and >0 items → forgeStatus shows the exact sentence; report shows `dryCap`; a subsequent full-import + reload clears it (recipe.new>0).
|
||||||
|
2. Non-dry forge → `dryCap` absent, forgeStatus behaves as before.
|
||||||
|
3. Deck behaviour unchanged otherwise (no new fetch loops; swipes unaffected).
|
||||||
|
|
||||||
|
**Parent verification:** staged-DB forge with an emptied fresh pool; assert the DOM states in A1/A2 + the existing section-loop selfcheck green.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 7 — Fresh counter (header)
|
||||||
|
|
||||||
|
**Goal (BACKLOG "Fresh counter"):** extend `/api/stats` with `fresh` = active listings with no swipe (the `NEW_POOL_SQL` predicate); header shows "N fresh" by the existing live/new pill. Zero reads at a glance.
|
||||||
|
|
||||||
|
**Plan:**
|
||||||
|
|
||||||
|
Backend (`app/api.py`, `stats`, 234–264):
|
||||||
|
|
||||||
|
1. Add one query next to `live` (238–240):
|
||||||
|
```sql
|
||||||
|
SELECT COUNT(*) AS n FROM listings l
|
||||||
|
WHERE l.status='active'
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM swipes s WHERE s.listing_id = l.id)
|
||||||
|
```
|
||||||
|
— verbatim the `NEW_POOL_SQL` predicate (sections.py 40–44). Add `'fresh': fresh` to the return dict (254–262). Note the deliberate semantic: a like/nope/maybe row of ANY age removes freshness (fresh = never touched), which is the same predicate the pool uses. A maybe swiped 8+ days ago that the legacy deck re-admits is still not "fresh" — correct per definition.
|
||||||
|
2. No other backend change: `fresh` can differ from `live - decided` because `swipes` rows are never deleted by undos that keep events, and list edits create rows too — define fresh exactly by the predicate above and do not try to reconcile.
|
||||||
|
|
||||||
|
Frontend (index.html, `loadStats`, 1127–1136):
|
||||||
|
|
||||||
|
3. Change the pill line 1130 to `<b>${res.live}</b> live` + `· <b>${res.fresh}</b> fresh` (always shown; `` fresh `0` when the pool is dry — that's the point) + the existing new-count only when >0. Spacing stays in the one `.pill` (206). Match the UI-SPEC §10.1 fit rule (no layout shift — a pill string is fine).
|
||||||
|
4. Add the same fresh number to the list sub-line? No — keep `lsub` unchanged (1131); the header pill is the requested surface.
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- Fresh goes below 0/negative never (a COUNT).
|
||||||
|
- Badge `new` counts first-seen-since-visit, an orthogonal thing; both can coexist in the pill.
|
||||||
|
- Old API (missing `fresh`): `res.fresh` undefined → guard (`res.fresh??0` or `typeof res.fresh==='number'?res.fresh:0`), fallback renders no fresh segment.
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Fresh staging DB: 5 actives, 3 with a swipes row, 2 not → `/api/stats.fresh == 2`; header pill text contains "2 fresh".
|
||||||
|
2. All-5 swiped → `fresh:0`; pill shows "0 fresh" (not hidden).
|
||||||
|
3. Swiping one card → next `loadStats` shows 4 fresh (server read reflects the upsert; FE doesn't optimistically touch the stats pill — the pill updates on next stats fetch, same as today).
|
||||||
|
|
||||||
|
**Parent verification:** TestClient stats assertions on the staged DB + headless DOM text check after a swipe.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 8 — Loud empty state ("No more new bikes!")
|
||||||
|
|
||||||
|
**Goal (BACKLOG "Empty state, loud"):** when the deck can serve nothing at all — no fresh, no eligible second looks, no wildcards — show a prominent "No more new bikes!" keeping the "next import 08:30" line. Today's `.empty` ("You're all caught up") is unreachable in section mode (only `legacyMode` shows it — `maybeRefill` 649–654) and the report card masks exhaustion.
|
||||||
|
|
||||||
|
**Plan:**
|
||||||
|
|
||||||
|
Server (`app/sections.py` + `app/api.py`):
|
||||||
|
|
||||||
|
1. New pool helper in `sections.py` (mirrors the composer's candidate queries, minus the per-section exclusions — "can the forge serve anything?"):
|
||||||
|
```python
|
||||||
|
def pool_counts(conn):
|
||||||
|
fresh = count(NEW_POOL_SQL)
|
||||||
|
rets = count(RETURNS_SQL)
|
||||||
|
wilds = count(WILDCARD_SQL)
|
||||||
|
```
|
||||||
|
(`RETURNS_SQL` is the composer's own eligibility predicate — resting/one-shot/last-two rules included; the last-two exclusion inside `compose_section` (274–278) is skipped here so the answer is about *stock*, not *this batch*; document that choice.)
|
||||||
|
2. `section_state` (446–471) gains `data['pool'] = secmod.pool_counts(conn)` (fresh/returns/wildcards). Every section GET + forge response carries it. No write.
|
||||||
|
|
||||||
|
Frontend (index.html):
|
||||||
|
|
||||||
|
3. **Loud empty element:** new `#empty2` beside `#report` in `#stage` (after 212), styled prominently (CSS near 95–97): big title 22px/800, larger panel (min-height ~220px), accent border, centered; copy:
|
||||||
|
```
|
||||||
|
<b>No more new bikes!</b>
|
||||||
|
New bikes arrive nightly — next import 08:30.
|
||||||
|
(↺ still brings the last card back — keep the line only if it remains true after item 1.)
|
||||||
|
```
|
||||||
|
4. **Reachability (the fix):**
|
||||||
|
- `maybeRefill` (649–674) section-mode branch: BEFORE the `sectDecided>=sectTotal` report check, if `section.pool` exists and `!buffer.length&&!pile.length` and `section.pool.fresh===0 && section.pool.returns===0 && section.pool.wildcards===0` → show `#empty2`, hide `#report` and `#empty`.
|
||||||
|
- `btnForge` success path (1188–1206): after `updateSectionUI`, run the same check against the new body — if the forge returned zero items and `pool` is all-zero, show `#empty2` and don't leave the report's Forge button inviting a wasted agent run (hide the button or keep row; show empty2 above it).
|
||||||
|
- Report screen stays the state for "section finished, more remains" (pool non-zero) — unchanged.
|
||||||
|
5. Keep `.empty` (legacy mode) as-is; `#empty2` exists only for section mode. Undo (`undo()` 702–720) removes `#empty2`'s `.on` class exactly like it clears `.empty`/`.report` (718–719).
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- The last-two-sections exclusion: a listing only appears in the last two sections' worth of items — pool query ignores that; near-total exhaustion could still leave 1–2 stale-but-served listings? No: `section_items` only blocks `RETURNS_SQL` re-offer, not `NEW`/`WILDCARD`; if pool counts say zero, compose genuinely returns zero cards (same SQLs). The tiny edge where pool>0 but compose emits 0 due to last-two+wanted-order interplay is handled by the `items.length===0` fallback: empty2 shows when a forge returns no cards AND pool is all-zero; if forge returns 0 items with pool>0 (should not happen), show empty2 with the dry line instead and log — implementer's guard, not spec.
|
||||||
|
- Import lands at 08:30 → first visit reloads sections: pool.fresh>0 → empty2 clears automatically on the next `loadSection`. No push, same as all state today.
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Staged DB with zero fresh, zero eligible returns, zero wildcards: `/api/section` body includes `pool` all-zero and `items` empty; FE shows `#empty2.on` with the exact title "No more new bikes!" and the 08:30 line; `#report` NOT shown.
|
||||||
|
2. `pool.returns>0` (dry but alive) → report card shown, `dryCap` (item 6) active, NO empty2.
|
||||||
|
3. Undo restores the last card from empty2 → empty2 hides (718–719 parity).
|
||||||
|
4. Report + forge loops still work when pool is non-zero (regression).
|
||||||
|
|
||||||
|
**Parent verification:** staged-DB headless walk A1–A4; plus the nightly-import path simulated by inserting one fresh listing + reload → normal deck.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 9 — Batch formula v2 (compose rewrite)
|
||||||
|
|
||||||
|
**This item AMENDS the frozen P3 rule "≤5 returns per section"** (`RANKING.md:29`, `DATA-MODEL.md:165`, `sections.py` docstring 2–17 — all say ≤5 / max 5). Say so in the commit notes. New rule, Glenn's words (BACKLOG "Batch formula v2"):
|
||||||
|
|
||||||
|
- Every batch targets **25 cards**.
|
||||||
|
- Fresh first (up to 23 slots); **≥2 second looks per batch whenever eligible stock exists** *(DEFAULT PENDING GLENN'S CONFIRMATION — the alternative, pure-fresh batches on rich days with second looks as fill only, is below)*; when fresh < 23, second looks FILL the remainder up to 25 (his example: 5 fresh + 20 second looks).
|
||||||
|
- Leftover fresh rolls to the next batch.
|
||||||
|
- Limits kept: ≤2 re-noped returns per batch; family cap relaxes for fill-returns when needed (spec below); returns stay one-shot.
|
||||||
|
- Fresh exhausted → second-looks-only batches; returns exhausted → fresh-only; both → item 8's loud empty state (forges may return 0 items — already legal: `compose_section` inserts whatever it picks, 388–401).
|
||||||
|
|
||||||
|
### Allocation (the exact slot math, spelling out the 23/25/2 interplay)
|
||||||
|
|
||||||
|
Wildcards keep their 2 slots (BACKLOG leaves them standing; `WILDCARDS=2` 31). Resolve the equations in this order:
|
||||||
|
|
||||||
|
```
|
||||||
|
want_ret = 2 if len(ret_cands) >= 2 else len(ret_cands) # the >=2 guarantee
|
||||||
|
(0 when ret_cands empty -> pure-fresh batch)
|
||||||
|
want_wild = 2 if usable wildcard candidates exist else fewer
|
||||||
|
fresh_cap = 23
|
||||||
|
want_fresh = size(25) - want_ret - want_wild
|
||||||
|
clamped to min(len(new_cands), fresh_cap)
|
||||||
|
```
|
||||||
|
1. Returns and wildcards take their reserved slots FIRST (returns before wildcards before fresh, so the guarantee can never be crowded out by fresh).
|
||||||
|
2. Fresh fills the remainder: `want_fresh = 25 - want_ret - want_wild` (max 23 when want_ret=2&wild=0? No — min is 21 on a rich day with 2+2 reserved). Then, **fill slack**: if `len(ret_cands)` has more offers and `len(picked) < 25`, add returns in role order (maybe > like > nope, points DESC — existing key 305–310) until 25 or the pool dries.
|
||||||
|
|
||||||
|
The resulting shapes on Glenn's scenarios:
|
||||||
|
|
||||||
|
- **Rich day, returns exist:** 25 cards = up to 23 fresh + ≥2 second looks + 2 wildcards. With 2 wildcards AND the 2-return guarantee, a full deck actually reads 21 fresh / 2 returns / 2 wildcards. (This is the one arithmetic consequence of "≥2 always" + "wildcards keep 2" + "target 25". The veto alternative below restores 23 fresh + ≤2.)
|
||||||
|
- **His dry-day example:** 5 fresh + fill returns up to total 25 → 5 fresh + up to 18 returns + 2 wildcards (or 20 returns + 0 wildcards when the wildcard pool is dry — his literal "5 fresh + 20 second looks"). Fractional pools (e.g. 5 fresh + 12 returns) produce a short section (<25) — correct; nothing is invented to pad.
|
||||||
|
- **Fresh = 0:** returns-only section(s) up to 25 cards each until the ~one-shot pool (≈0–30 remaining tonight per BACKLOG) dries — burns fast; BACKLOG's noted consequence is accepted in v1.1: after that, only nightly imports feed the deck.
|
||||||
|
- **Returns = 0:** fresh 25-card batches again (plus wildcards).
|
||||||
|
- **Both 0:** the loud empty state (item 8). Forge then legitimately returns a zero-item composed row; the FE renders empty2 — the report screen's Forge button should also stop being the only thing on screen (item 8 handles the mask).
|
||||||
|
|
||||||
|
### Re-nope + family-cap rules for fill-returns
|
||||||
|
|
||||||
|
- Keep ≤2 nope-verdict returns per batch (`MAX_RETURN_NOPES` 30; the "surprising" gate at 317–321 stays for *reserved* nope slots only).
|
||||||
|
- **Fill-returns relax the family cap:** when selected as fill and the choice is between skipping the cap and landing a short deck, raise the per-family limit for `return`-slot items from 4 to 8 (fill-returns only; fresh picks and reserved returns keep 4). Deterministic: fill sweeps role order; a candidate that would exceed 8 is skipped; <=4 is always preferred pass 1 (two-pass fill: first under cap 4, then under cap 8).
|
||||||
|
- Returns stay one-shot (`RETURNS_SQL`'s `NOT EXISTS section_items slot='return'` 53–54) and re-noped items rest forever (50–52). **No change to those predicates.**
|
||||||
|
- Prefer not to burn the whole one-shot pool in two batches? The BACKLOG explicitly notes the pool ~30 tonight and accepts the burn; no v1.1 policy change. **If Glenn instead wants a drip feed, that is the veto line in Open Questions.**
|
||||||
|
|
||||||
|
### Compose pseudocode (replaces the flavour of `compose_section` at 259–404; structure kept)
|
||||||
|
|
||||||
|
```
|
||||||
|
compose_section(conn, engine, size=DEFAULT_SIZE, ...):
|
||||||
|
now, rows_by_id, last_two # unchanged (266-271)
|
||||||
|
new_cands = NEW_POOL_SQL - last_two # unchanged (273-274)
|
||||||
|
ret_cands = RETURNS_SQL - last_two # unchanged (275-276)
|
||||||
|
wild_cands = WILDCARD_SQL - last_two - excluded_fams # unchanged (277-289)
|
||||||
|
fam_count, picked_ids # unchanged (291-292)
|
||||||
|
top3 = pick_new(...) ; top_fams/top_bands # unchanged (294-298) — feeds wildcard
|
||||||
|
# preference; pure pick, no commit
|
||||||
|
|
||||||
|
# 1) reserved returns: min(2 + shortfall_unknown, len(ret_cands)), role order
|
||||||
|
reserved_rets = ret_cands sorted by role_order/points; take until want_ret_nopes-rule or 2-max;
|
||||||
|
always count the 2 GUARANTEED slots (skipping only ineligible nopes)
|
||||||
|
# 2) wildcards: n_wilds = 2 (existing novel/preferred pick 328-358, unchanged)
|
||||||
|
# 3) fresh: want_fresh = size - len(rets_reserved) - n_wilds, capped 23; pick_new (360-362)
|
||||||
|
# 4) fill returns: while len(rets)<size-n_wilds-len(picked_new) and ret_cands remain:
|
||||||
|
next in role order; nope cap (<=2 total incl. reserved);
|
||||||
|
family rescue pass at cap 8 for fill only
|
||||||
|
# 5) repair bands on new slots only (unchanged 367-375)
|
||||||
|
# 6) pinned order new -> returns -> wildcards (unchanged 364-365); recipe/JSON/inserts
|
||||||
|
unchanged (377-404) with recipe.new/return/wildcard the real counts
|
||||||
|
```
|
||||||
|
|
||||||
|
Deliberate simplifications vs a full re-derivation:
|
||||||
|
|
||||||
|
- `pick_new`'s family cap 4, band repair, wildcard novelty, excluded families, LAST_TWO exclusion: **all unchanged** (160–178, 181–256, 284–289, 329–358).
|
||||||
|
- The `wanted_rets` line 304 dies; the reserved-returns stage replaces it. Constants at 28–33: `MAX_RETURNS` is deleted/renamed (fill is bounded by size), `MAX_RETURN_NOPES` stays 2, add `FRESH_CAP=23` and `MIN_RETURNS=2` (pending Glenn's confirmation — the DEFAULT in this spec).
|
||||||
|
- `read_section`/`section_state`/FE need **zero changes for the formula itself** — a section of any size ≤25 renders as today (progress pill counts `body.progress.total`).
|
||||||
|
|
||||||
|
Edge cases:
|
||||||
|
|
||||||
|
- Fewer than 2 eligible returns but ≥1: guarantee degrades gracefully (1 return + fill from... nothing else exists, that's the pool) — never fake it.
|
||||||
|
- `size` stays the clamp (`size=25` default; the forge endpoint 492–495 accepts 1..100 — body `size` unchanged in the FE which always sends 25/`{}`).
|
||||||
|
- Batch smaller than 25 mid-drain: the FE, report, and progress all already key off `progress.total`; nothing to cap client-side.
|
||||||
|
|
||||||
|
The veto alternative (record for Glenn):
|
||||||
|
|
||||||
|
- **≥2-always** (this spec's DEFAULT): every batch with any eligible return carries ≥2 second looks, even on a rich fresh day (21/2/2 shape). Keeps Glenn's "min 2" literally true.
|
||||||
|
- **Pure-fresh-on-rich-days:** reserve 2 wildcards + up to 23 fresh; returns used ONLY as fill when fresh < 23 (rich day → 23 fresh + 2 wildcards + 0 returns). This obeys "fresh first, up to 23" literally but drops the "min 2" guarantee on the days he has the most fresh to see. One boolean switch in the allocator (`min_ret = 2 if Glenn confirms else 0`); nothing else moves.
|
||||||
|
|
||||||
|
**Acceptance criteria (staged DBs, deterministic pools):**
|
||||||
|
|
||||||
|
1. Pool 30 fresh / 10 eligible returns / wildcards available → forge composes 25: fresh + wildcard + return counts match the ≥2 + fill math; `recipe` JSON exact; `progress.total == 25`.
|
||||||
|
2. Pool 5 fresh / 20 eligible returns / wildcards available → 25 cards, 5 fresh, returns fill to 20 (with 2 wildcards → 18; without → 20); his example honoured modulo wildcards.
|
||||||
|
3. Pool 23 fresh / 5 returns / wildcards available → fresh 21 + returns 2 + wildcards 2 (the ≥2 formula) — and this exact result is the sentence Glenn must sign off (Open Questions).
|
||||||
|
4. Pool 0 fresh / 12 returns → 12-card returns-only section; ≤2 nopes present; no item repeated from either of the previous two sections (LAST_TWO).
|
||||||
|
5. Pool 40 fresh / 0 returns → 25-card fresh batch, order points DESC, leftover 15 fresh roll and appear in the NEXT forge (compose twice, zero overlap, no re-offer).
|
||||||
|
6. Pool 0/0/0 → composed section with `items=[]`, FE shows item 8's empty2.
|
||||||
|
7. Family cap: a fill-heavy batch with a dominant family raises its return-slot count only to ≤8 and never violates ≤4 for fresh/reserved; band-repair still reaches 3 bands when possible.
|
||||||
|
8. Re-nope gate: exactly ≤2 nope-verdict returns per batch; a rested item (round≥2 nope) never appears.
|
||||||
|
|
||||||
|
**Parent verification:**
|
||||||
|
|
||||||
|
1. `selfcheck.py` additions driving `compose_section` against generated temp DBs for A1–A8 (the module is pure Python + SQLite and already selfcheck-tested this way for P3).
|
||||||
|
2. Full P3 loop regression on a rich and a dry DB: compose → strikes → rounds → report → forge → counts; trends numbers unchanged in shape.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Item 10 — Second Look band pops
|
||||||
|
|
||||||
|
**Goal (BACKLOG "Second Look band pops"):** `.band.sl` is near-invisible today — re-tint to a **violet accent** (bold fill + border), on card face + info view. Wildcard stays amber for contrast. *(DEFAULT violet pending Glenn's nod — one-line re-tint if he prefers another hue.)*
|
||||||
|
|
||||||
|
**Current:** `.band.sl{color:rgba(255,255,255,.85);background:rgba(255,255,255,.10);border:1px solid rgba(255,255,255,.22)}` (141) — white ~10% on dark, matches the P3 §10.3 second-look recipe (142). The band appears where `bandHTML` renders it: card face (560) and — verify — the dossier does NOT currently render a band at all: `openDetail` (768–796) has no band markup. "Info view" for the pill therefore needs a small addition, see 3 below.
|
||||||
|
|
||||||
|
**Plan (CSS + one markup line):**
|
||||||
|
|
||||||
|
1. Replace line 141:
|
||||||
|
```
|
||||||
|
.band.sl{color:#c4b5fd;background:rgba(124,58,237,.22);border:1px solid rgba(167,139,250,.65);font-weight:800}
|
||||||
|
```
|
||||||
|
(violet #7c3aed family; text near-white-violet `#c4b5fd`; bold fill + crisp border). Uppercase "SECOND LOOK" label (486) reads instantly against this.
|
||||||
|
2. `.band.wild` (140) untouched — amber stays the contrast.
|
||||||
|
3. Dossier/info view: add the same band to the dossier body so "info view" carries it. In `openDetail`, after the chips (778), insert the band when `data.slot==='return'|'wildcard'`:
|
||||||
|
- simplest: an anchor div in `#dbody` (246, e.g. `<div class="band" style="position:static;display:inline-block;margin-top:6px" id="dband"></div>` — `display:none` default) filled by `openDetail` with `bandHTML(data)` logic (reuse the string builder 482–488; note `.band` is `position:absolute` by default, so give the dossier variant the static class or a dedicated `.dband` style with the same palette, per UI-SPEC palette text below).
|
||||||
|
- `.card.hasSegs .band{top:22px}` (188) already handles the card-face coexistence with segment bars.
|
||||||
|
4. No other surface: list rows don't carry bands today (not required).
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
|
||||||
|
- Wildcard + segments + violet coexistence on the card: bands at 22px (188), segments at 8px (185) — unchanged geometry.
|
||||||
|
- The band wraps up to 2 lines (139 `max-width:78%`) — violet fill still legible.
|
||||||
|
- Contrast check: `#a78bfa` border on `#0b0b0e`-family backgrounds ≈ 7:1; text `#c4b5fd` on the violet-22% fill ≈ 6.5:1 — passes WCAG AA while staying dark-mode.
|
||||||
|
|
||||||
|
**Acceptance criteria:**
|
||||||
|
|
||||||
|
1. Card with `slot:'return'` renders a violet band; computed styles match the new palette (assert colour/background/border strings); wildcard card still amber.
|
||||||
|
2. Dossier opened on a return card shows the same violet band; on a new-slot card, no band.
|
||||||
|
3. Segments + band layout regression green (B1 walk).
|
||||||
|
|
||||||
|
**Parent verification:** computed-style assertions + two screenshots (card, dossier) over the tailnet or headless.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Data safety
|
||||||
|
|
||||||
|
v1.1 makes **ZERO schema migrations, zero data mutations, no reset.** Every DATA-MODEL table and every existing row survives untouched; Glenn's ledger (swipes/events) is not read-modified in any new way.
|
||||||
|
|
||||||
|
Writes that happen in v1.1 are only the writes that already happen today, unchanged:
|
||||||
|
|
||||||
|
- `POST /api/swipe` (upsert `swipes` + `append_event`), `DELETE /api/swipe`, `POST /api/prefs` (one new key: `rubric_debug` — a new `prefs` row is the ONLY new write in v1.1, and it is a settings row, not data).
|
||||||
|
- Compose/forge writes a new `sections` + `section_items` row set — the normal P3 flow, identical write shape.
|
||||||
|
|
||||||
|
New READS only:
|
||||||
|
|
||||||
|
- `/api/stats` +1 COUNT (fresh).
|
||||||
|
- `GET /api/section` / forge response +2 small derived reads: `decided_this_section` flags (from `swipe_events` WHERE section_id — read-only), `pool` counts (the three existing candidate SQLs, counted).
|
||||||
|
- `/api/section` payload +1 prefs read (`rubric_debug`).
|
||||||
|
|
||||||
|
No new tables, no new columns, no imports, no re-seeding (the `swipe_events` seed in `db.init_db`, db.py 109–117, runs only when empty and is not re-run by anything here, and v1.1 ships no changes to `schema.sql` or `P3_SCHEMA`).
|
||||||
|
|
||||||
|
Rollback: revert the commit; the only residue is the `rubric_debug` pref row, harmless if left.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Open Questions (blocking decisions → Glenn / Alex)
|
||||||
|
|
||||||
|
1. **Item 9 — the "≥2 second looks" guarantee on rich days (confirmed needed to build).** Default specced: every batch carries ≥2 returns whenever eligible stock exists → a full 25-deck on a rich day reads **21 fresh + 2 second looks + 2 wildcards** (fresh capped at 23 = 25-2, and the two wildcard slots come out of fresh). Alternative for Glenn to veto: pure-fresh batches on rich days (23 fresh + 2 wildcards + 0 returns; returns fill only when fresh < 23). One switch in the allocator; everything else identical.
|
||||||
|
2. **Item 10 — violet nod.** Default `#7c3aed`/`#a78bfa`/`#c4b5fd` (see amendment text). Trivial to re-tint if he picks another hue.
|
||||||
|
3. **Item 1 — Backspace vs ArrowDown for undo.** BACKLOG names ↓; the live v1.0 handler already uses Backspace. Spec keeps BOTH (↓ primary per Glenn's words, Backspace retained as the muscle-memory alias). Confirm or drop Backspace.
|
||||||
|
4. **Item 2 — dossier opened from a deck card, swiped via its own buttons:** `openDetail` from the deck currently allows committing the top card from inside the dossier (`dAction` 838–848). Under info-first flow this stays. If Glenn wants the sheet to be read-only from the deck, that's a v1.2 behaviour change — not in this spec.
|
||||||
|
5. **Item 5 — undo-on-a-return residual:** un-deciding a second look that carries a lifetime verdict restores the swipes row but leaves the `section_id`-stamped event; the in-section count re-includes it on resync (clamped so it can never exceed the total, but it can sit AT the total with one undone card). A clean fix needs an event-deletion/„undo" semantics decision (v1.2 territory alongside analytics); accepted as-is for v1.1 and stated explicitly so the parent doesn't chase it as a leak.
|
||||||
|
6. **Item 9 — one-shot return-pool burn:** with fill-to-25 the remaining eligible second looks (~≤30 as of 2026-11-12) drain within a batch or two. Accepted per BACKLOG's noted consequence; the drip-vs-burn preference belongs to Glenn before build if he cares.
|
||||||
|
7. **Item 8 — copy retention:** "↺ brings the last card back" line kept only if still true after item 1's undo mapping (it is — undo exists either way). Any copy tweaks Glenn wants on "No more new bikes!" (exclamation, wording) noted before build.
|
||||||
|
8. **Found while verifying (pre-existing, NOT v1.1 scope — flagged):** `maybe_exhaust` uses lifetime `decision` (sections.py 462) and the FE send of `photo_idx` is conditional on `nPhotos>1` (683) so single-photo cards don't update rotation; both are covered or neutral after item 5, no action. Also verified: the live "keyboard arrows" feature in v1.0 was live-deployed ahead of the BACKLOG as part of the P2 build (DESIGN.md:63, index.html 1263) — v1.1 formalizes and extends it rather than introducing it.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Proposed UI-SPEC amendments (verbatim replacement text — DO NOT apply during the docs phase)
|
||||||
|
|
||||||
|
### A) §11.4 "Doors" — replace the whole subsection's body paragraph
|
||||||
|
|
||||||
|
> ### 11.4 Doors (supersedes the old tap→dossier)
|
||||||
|
>
|
||||||
|
> - ⓘ (top-right of card) → dossier (unchanged). The card face now owns: **sides = flip, center = dossier (bike data first)**; the "tap anywhere → dossier" gesture is RETIRED and the B1 center-tap→fullscreen gesture is RETIRED in favour of it.
|
||||||
|
> - Tapping the photo in the dossier hero opens the fullscreen image viewer (§11.3). **Viewer close (✕, pull-down, or Esc) returns to the dossier; the next Esc returns to the deck.** The dossier stays reachable from ⓘ and from list rows (§3/§4 unchanged).
|
||||||
|
|
||||||
|
### B) §11.3 "Fullscreen viewer" — amend the entry sentence
|
||||||
|
|
||||||
|
> - Tap the CENTER third → fullscreen viewer opens on the photo currently shown.
|
||||||
|
>
|
||||||
|
> becomes:
|
||||||
|
>
|
||||||
|
> - Tap the photo in the dossier hero → fullscreen viewer opens (starts at photo 0). Direct card→viewer is retired (see §11.4); the viewer is reached from the dossier.
|
||||||
|
|
||||||
|
### C) §2 card geometry — photo fit: add a new bullet after "Photo full-bleed"
|
||||||
|
|
||||||
|
> - Photo fit is **contain**: the whole photo is shown uncropped over a soft blurred backdrop of the same image (option C). The backdrop is decorative (blurred, dimmed, overflow-clipped); the sharp photo sits on top with a gentle shadow. The fullscreen viewer already fits; list thumbs stay `cover` (56px).
|
||||||
|
|
||||||
|
### D) §10.3 Card bands — replace the SECOND LOOK palette line
|
||||||
|
|
||||||
|
> - **SECOND LOOK — you {liked|maybed|noped} this** — color `rgba(255,255,255,.85)`; bg `rgba(255,255,255,.10)`; border `1px solid rgba(255,255,255,.22)`.
|
||||||
|
>
|
||||||
|
> becomes:
|
||||||
|
>
|
||||||
|
> - **SECOND LOOK — you {liked|maybed|noped} this** — color `#c4b5fd`; bg `rgba(124,58,237,.22)`; border `1px solid rgba(167,139,250,.65)`; font-weight 800. Wildcard stays amber (`#ffb43a` family values unchanged) for contrast. The same band renders in the dossier ("info view") when the listing's slot is `return` or `wildcard`.
|
||||||
|
|
||||||
|
### E) §10.5 Dossier "Why this rank" — add a debug-visibility paragraph
|
||||||
|
|
||||||
|
> - Hidden-by-default detail: the breakdown lines and the "from N strikes" footer render only when `prefs['rubric_debug'] === 'on'` (default `'off'`). With the pref off the block shows **the tank gauge + "{points}%"** only — the minimal rubric display. The toggle lives in the Trends "Actions" area (why-breakdown on ⇄ off, same control style as the forge-mode toggle).
|
||||||
|
|
||||||
|
### F) §10.1 Sections — replace the composition sentence within the section-progress bullet
|
||||||
|
|
||||||
|
> - Section composition (engine side — RANKING.md): ≤5 SECOND LOOK cards + 2 WILDCARD cards + the remainder new.
|
||||||
|
>
|
||||||
|
> becomes:
|
||||||
|
>
|
||||||
|
> - Section composition (engine side — RANKING.md): every batch targets 25 cards = fresh first (up to 23 slots) + ≥2 SECOND LOOK cards whenever eligible stock exists + 2 WILDCARD cards; when fresh < 23, second looks fill the remainder up to 25. Re-nope returns stay ≤2 per batch; fresh shortfalls are never padded.
|
||||||
|
|
||||||
|
### G) §2 Empty state + §10.2 report — add the loud exhausted state
|
||||||
|
|
||||||
|
> - §2's quiet "You're all caught up." remains only for legacy (pre-section) mode. In section mode, when the deck can serve nothing at all — no fresh listings, no eligible second looks, no wildcards — the deck shows a prominent **"No more new bikes!"** panel with "New bikes arrive nightly — next import 08:30." and, when true, "↺ brings the last card back." This panel supersedes the report card in that state only; the report card remains the end-of-section screen whenever more cards can still be served.
|
||||||
|
|
||||||
|
### H) §1 Globals — header pill
|
||||||
|
|
||||||
|
> - Header pill shows `{live} live · {fresh} fresh` always, and `· {new} new` only when new > 0. `fresh` = active listings with no swipe (the deck's new-pool predicate).
|
||||||
|
|
||||||
|
### I) §10.2 Forge — dry-catalog note
|
||||||
|
|
||||||
|
> - When a forged batch reports `new: 0` the report shows: "Catalog fully reviewed — fresh listings arrive with the next 08:30 import; showing second looks." and the forge reads the same note in its status line.
|
||||||
|
|
||||||
|
(Collecting all in one place per protocol — the file itself is not touched in this phase.)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Verification plan (summary the parent will re-run after the build)
|
||||||
|
|
||||||
|
1. Fresh-clone checkout of the v1.1 build, temp DB copies only (never the live DB): existing selfcheck + new v1.1 checks, all green.
|
||||||
|
2. Per item with the acceptance criteria above: 1 (keyboard: key dispatch, guards, scroll, Esc stack) · 2 (tap-flow walk + B1 §11.7 suite) · 3 (computed styles + screenshots vs demo panel C) · 4 (prefs API 200/400, dossier DOM with pref off/on) · 5 (staged returns-only section: 0/2 → 1/2 → 2/2 → report, clamp, list-edit neutrality) · 6 (dry forge → caption + status line) · 7 (stats fresh count + pill text) · 8 (0/0/0 pool → loud empty2, report mask gone) · 9 (A1–A8 compose matrix on generated pools) · 10 (violet band computed styles, wildcard unchanged, segments coexistence).
|
||||||
|
3. Full section loop regression: compose → strikes → rounds → report → forge on a rich DB and a dry DB; trends, deck feed, undos unchanged in shape.
|
||||||
|
4. Zero-mutation audit: diff against `381b6c8` shows no `schema.sql`/`P3_SCHEMA`/migration writes; `rubric_debug` pref row is the only new write.
|
||||||
|
5. Glenn phone pass (human): keyboard on MacBook; center-tap data-first on phone; violet band visible; fresh counter reads; empty2 appears when shown a dry copy (staged), loud and clear.
|
||||||
BIN
docs/images/bike-details.png
Normal file
BIN
docs/images/bike-details.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
BIN
docs/images/swipe-deck.png
Normal file
BIN
docs/images/swipe-deck.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 101 KiB |
@@ -2,3 +2,5 @@
|
|||||||
fastapi
|
fastapi
|
||||||
uvicorn
|
uvicorn
|
||||||
httpx
|
httpx
|
||||||
|
requests # agent-pass live call (OpenRouter) — imported by app/agentpass.py
|
||||||
|
playwright==1.54.0 # B1 dev/test only: headless FE walk (app/fe_walk_b1.py)
|
||||||
|
|||||||
Reference in New Issue
Block a user