# S01: Fix Key Moment Search Links

**Goal:** Key moment search results link to the correct parent technique page and scroll to the specific moment, instead of 404ing.
**Demo:** After this: Search 'compression', click any key moment result → lands on parent technique page with key moment visible

## Tasks
- [x] **T01: Added technique_page_slug to search results across Qdrant payloads, semantic enrichment, keyword search, and the response schema** — Fix the backend so key moment search results include the parent technique page slug. Three areas need changes:

1. **Qdrant payload enrichment** (`backend/pipeline/qdrant_client.py` + `backend/pipeline/stages.py`): Add `slug` to technique page payloads and `technique_page_slug`/`technique_page_id` to key moment payloads. The stage 6 code in `stages.py` (~line 960) builds `page_dicts` and `moment_dicts` — add the missing fields there. For moments, build a `page_id_to_slug` mapping from the `pages` list, then look up each moment's `technique_page_id` to get the slug.

2. **Search service** (`backend/search_service.py`):
   - `_enrich_results()` (~line 310): Currently falls back to `title.lower().replace(' ', '-')` for slug. Add `technique_page_slug` to the enriched dict, reading from `payload.get('technique_page_slug', '')`. For technique pages, use `payload.get('slug', '')` directly (no longer need the naive fallback once re-indexed, but keep it as fallback for old data).
   - `keyword_search()` (~line 137): Key moment results currently set `slug: ''`. Add an outerjoin to `TechniquePage` via `KeyMoment.technique_page_id == TechniquePage.id` to get the parent slug. Add `technique_page_slug` to the result dict.
   - Also add `technique_page_slug` to technique_page keyword results (set to same as `slug` — a technique page IS its own parent).

3. **Schema** (`backend/schemas.py`): Add `technique_page_slug: str = ''` to `SearchResultItem`.

4. **Tests** (`backend/tests/test_search.py`): Update the keyword fallback test to verify `technique_page_slug` is present in results. Add a test for key moment keyword search returning the correct parent technique page slug.
  - Estimate: 45m
  - Files: backend/pipeline/stages.py, backend/pipeline/qdrant_client.py, backend/search_service.py, backend/schemas.py, backend/tests/test_search.py
  - Verify: cd /home/aux/projects/content-to-kb-automator && python -m pytest backend/tests/test_search.py -v 2>&1 | tail -30
- [x] **T02: Key moment search results now link to parent technique page with hash-scroll to the specific moment** — Fix the frontend so clicking a key moment search result navigates to the parent technique page and scrolls to the specific moment.

1. **TypeScript interface** (`frontend/src/api/public-client.ts`): Add `technique_page_slug?: string` to the `SearchResultItem` interface (line ~10).

2. **Search result card** (`frontend/src/pages/SearchResults.tsx`): In `SearchResultCard` (~line 145), the `<Link>` currently always goes to `/techniques/${item.slug}`. Change to:
   - If `item.type === 'key_moment'` and `item.technique_page_slug`: link to `/techniques/${item.technique_page_slug}#km-${item.slug || item.title}`
   - If `item.type === 'key_moment'` but no `technique_page_slug`: link to `/search?q=${encodeURIComponent(item.title)}` (graceful fallback — don't 404)
   - Otherwise (technique_page): keep `/techniques/${item.slug}`

3. **Key moment anchors** (`frontend/src/pages/TechniquePage.tsx`): The `<li key={km.id}>` at ~line 421 has no `id` attribute. Add `id={`km-${km.id}`}` to the `<li>` element so hash fragments can target specific moments.

4. **Scroll-to-hash** (`frontend/src/pages/TechniquePage.tsx`): Add a `useEffect` that runs after technique data loads. If `window.location.hash` starts with `#km-`, find the element by ID and call `scrollIntoView({ behavior: 'smooth', block: 'start' })`. The effect should depend on `technique` (the loaded data) so it fires after render.

5. **Build verification**: Run `npm run build` in the frontend to ensure no TypeScript errors.
  - Estimate: 30m
  - Files: frontend/src/api/public-client.ts, frontend/src/pages/SearchResults.tsx, frontend/src/pages/TechniquePage.tsx
  - Verify: cd /home/aux/projects/content-to-kb-automator/frontend && npm run build 2>&1 | tail -20
