{"type":"thinking_level_change","id":"ff7b98d1","parentId":null,"timestamp":"2026-04-03T01:47:51.084Z","thinkingLevel":"medium"}
{"type":"model_change","id":"798bf286","parentId":"ff7b98d1","timestamp":"2026-04-03T01:47:51.085Z","provider":"anthropic","modelId":"claude-opus-4-6"}
{"type":"custom_message","customType":"gsd-auto","content":"You are executing GSD auto-mode.\n\n## Working Directory\n\nYour working directory is `/home/aux/projects/content-to-kb-automator`. All file reads, writes, and shell commands MUST operate relative to this directory. Do NOT `cd` to any other directory.\n\nAll relevant context has been preloaded below — start working immediately without re-reading these files.\n\n## Requirements Advanced\n\nNone.\n\n## Requirements Validated\n\nNone.\n\n## Requirements Invalidated or Re-scoped\n\nNone.\n\n## Requirements Advanced\n\n- R012 — Stage 5 now composes new video content into existing technique pages instead of overwriting, fulfilling the incremental update aspect of R012\n\n## Requirements Validated\n\nNone.\n\n## Requirements Invalidated or Re-scoped\n\nNone.\n\n## UNIT: Research Slice S06 (\"Admin UI — Multi-Source Pipeline Management\") — Milestone M014\n\n## Inlined Context (preloaded — do not re-read these files)\n\n### Milestone Roadmap\nSource: `.gsd/milestones/M014/M014-ROADMAP.md`\n\n# M014: \n\n## Vision\nRestructure technique pages to be broader (per-creator+category across videos), deeper (H2/H3 nesting with TOC), multi-source (compose new video content into existing pages), with inline citations linking prose claims to source moments, admin multi-source management, and section-level search with deep linking.\n\n## Slice Overview\n| ID | Slice | Risk | Depends | Done | After this |\n|----|-------|------|---------|------|------------|\n| S01 | Synthesis Prompt v5 — Nested Sections + Citations | high | — | ✅ | Run test harness with new prompt → output has list-of-objects body_sections with H2/H3 nesting, citation markers on key claims, broader page scope. |\n| S02 | Composition Prompt + Test Harness Compose Mode | high | S01 | ✅ | Run test harness --compose mode with existing page + new moments → merged output with deduplication, new sections, updated citations. |\n| S03 | Data Model + Migration | low | — | ✅ | Alembic migration runs clean. API response includes body_sections_format and source_videos fields. |\n| S04 | Pipeline Compose-or-Create Logic | high | S01, S02, S03 | ✅ | Process two COPYCATT videos. Second video's moments composed into existing page. technique_page_videos has both video IDs. |\n| S05 | Frontend — Nested Rendering, TOC, Citations | medium | S03 | ✅ | Format-2 page renders with TOC, nested sections, clickable citations. Format-1 pages unchanged. |\n| S06 | Admin UI — Multi-Source Pipeline Management | medium | S03, S04 | ⬜ | Admin view for multi-source page shows source dropdown, composition history, per-video chunking inspection. |\n| S07 | Search — Per-Section Embeddings + Deep Linking | medium | S04, S05 | ⬜ | Search 'LFO grain position' → section-level result → click → navigates to page#section and scrolls. |\n\n---\n\n### Decisions\nSource: `.gsd/DECISIONS.md`\n\n# Decisions (compact)\nFields: id | when | scope | decision | choice | rationale | revisable | made_by\n\nD024 | M014/S01 | architecture | Content model for sections with subsections | Sections with subsections use empty-string content field; substance lives in subsections | Avoids duplication between section-level content and subsection content. The section heading serves as H2 container; all prose lives in subsection content fields. Sections without subsections use the section content field directly. | Yes | agent\n\n---\n\n### Requirements\nSource: `.gsd/REQUIREMENTS.md`\n\n# Requirements\n\n## R001 — Whisper Transcription Pipeline\n**Status:** validated\n**Description:** Desktop Python script that accepts video files (MP4/MKV), extracts audio via ffmpeg, runs Whisper large-v3 on RTX 4090, and outputs timestamped transcript JSON with segment-level timestamps and word-level timing. Must be resumable.\n**Validation:** Script processes a sample video and produces valid JSON with timestamped segments.\n**Primary Owner:** M001/S01\n\n## R002 — Transcript Ingestion API\n**Status:** validated\n**Description:** FastAPI endpoint that accepts transcript JSON uploads, creates/updates Creator and Source Video records, and stores transcript data in PostgreSQL. Handles new creator detection from folder names.\n**Validation:** POST transcript JSON → 200 OK, records created in DB, file stored on filesystem.\n**Primary Owner:** M001/S02\n\n## R003 — LLM-Powered Extraction Pipeline (Stages 2-5)\n**Status:** validated\n**Description:** Background worker pipeline: transcript segmentation → key moment extraction → classification/tagging → technique page synthesis. Uses OpenAI-compatible API with primary (DGX Sparks Qwen) and fallback (local Ollama) endpoints. Pipeline must be resumable per-video per-stage.\n**Validation:** End-to-end: transcript JSON in → technique pages with key moments, tags, and cross-references out.\n**Primary Owner:** M001/S03\n\n## R004 — Review Queue UI\n**Status:** validated\n**Description:** Admin interface for reviewing extracted key moments: approve, edit+approve, split, merge, reject. Organized by source video for contextual review. Includes mode toggle (review vs auto-publish).\n**Validation:** Admin can review, edit, and approve/reject moments; mode toggle controls whether new moments require review.\n**Primary Owner:** M001/S04\n\n## R005 — Search-First Web UI\n**Status:** validated\n**Description:** Landing page with prominent search bar, live typeahead (results after 2-3 chars), scope toggle (All/Topics/Creators), and two navigation cards (Topics, Creators). Recently added section. Search powered by Qdrant semantic search with keyword fallback.\n**Validation:** User types query → results appear within 500ms, grouped by type, with clickable navigation.\n**Primary Owner:** M001/S05\n\n## R006 — Technique Page Display\n**Status:** validated\n**Description:** Core content unit: header (tags, title, creator, meta), study guide prose (organized by sub-aspects with signal chain blocks and quotes), key moments index (timestamped list), related techniques, plugins referenced. Amber banner for livestream-sourced content.\n**Validation:** Technique page renders with all sections populated from synthesized data.\n**Primary Owner:** M001/S05\n\n## R007 — Creators Browse Page\n**Status:** validated\n**Description:** Filterable creator list with genre filter pills, type-to-narrow, sort options (randomized default, alphabetical, view count). Each row: name, genre tags, technique count, video count, view count. Links to creator detail page.\n**Validation:** Page loads with randomized order, genre filtering works, clicking row navigates to creator detail.\n**Primary Owner:** M001/S05\n\n## R008 — Topics Browse Page\n**Status:** validated\n**Description:** Two-level topic hierarchy (7 top-level categories → sub-topics). Filter input, genre filter pills. Each sub-topic shows technique count and creator count. Clicking sub-topic loads a dedicated page with creator-grouped techniques and breadcrumbs.\n**Validation:** Hierarchy renders, filtering works, sub-topics have dedicated pages at /topics/:category/:subtopic with backend filtering and creator grouping. Verified: M010/S01.\n**Primary Owner:** M001/S05\n\n## R009 — Qdrant Vector Search Integration\n**Status:** validated\n**Description:** Embed key moment summaries, technique page content, and transcript segments in Qdrant using configurable embedding model (nomic-embed-text default). Power semantic search with metadata filtering.\n**Validation:** Semantic search returns relevant results for natural language queries; embeddings update when content changes.\n**Primary Owner:** M001/S03\n\n## R010 — Docker Compose Deployment\n**Status:** validated\n**Description:** Single docker-compose.yml packaging API, web UI, PostgreSQL, and worker services. Follows XPLTD conventions: bind mounts at /vmPool/r/services/, compose at /vmPool/r/compose/chrysopedia/, xpltd_chrysopedia project name, dedicated Docker network.\n**Validation:** `docker compose up -d` brings up all services; data persists across restarts.\n**Primary Owner:** M001/S01\n\n## R011 — Canonical Tag System\n**Status:** validated\n**Description:** Editable canonical tag list (config file) with aliases. Pipeline references tags during classification. New tags can be proposed by LLM and queued for admin approval or auto-added within existing categories.\n**Validation:** Tag list is editable; pipeline uses canonical tags consistently; alias normalization works.\n**Primary Owner:** M001/S03\n\n## R012 — Incremental Content Addition\n**Status:** validated\n**Description:** System handles ongoing content: new videos processed through pipeline, new creators auto-detected, existing technique pages updated when new moments are added for same creator+topic.\n**Validation:** Adding a new video for an existing creator updates their technique pages; new creator folder creates new Creator record.\n**Primary Owner:** M001/S03\n\n## R013 — Prompt Template System\n**Status:** validated\n**Description:** Extraction prompts (stages 2-5) stored as editable configuration files, not hardcoded. Admin can edit prompts and re-run extraction on specific or all videos for calibration.\n**Validation:** Prompt files are editable; re-processing a video with updated prompts produces different output.\n**Primary Owner:** M001/S03\n\n## R014 — Creator Equity\n**Status:** validated\n**Description:** No creator is privileged in the UI. Default sort on Creators page is randomized on every page load. All creators get equal visual weight.\n**Validation:** Refreshing Creators page shows different order each time; no creator gets larger/bolder display.\n**Primary Owner:** M001/S05\n\n## R015 — 30-Second Retrieval Target\n**Status:** active\n**Description:** A producer mid-session can find a specific technique in under 30 seconds from Alt+Tab to reading the key insight.\n**Validation:** Timed test: Alt+Tab → search → read technique → under 30 seconds.\n**Primary Owner:** M001/S05\n\n## R016 — Card Hover Animations & Staggered Entrance\n**Status:** validated\n**Description:** All technique/content cards animate on hover (scale + shadow transition). Card grids use staggered fade-in-up entrance animations on page load.\n**Validation:** Cards visually respond to hover with smooth 200ms transition. Grid cards appear sequentially on page load.\n**Primary Owner:** M011/S01\n\n## R017 — Featured Technique Visual Redesign\n**Status:** validated\n**Description:** Featured Technique section on homepage has a visually distinct treatment (gradient border, glow, or full-bleed) that differentiates it from ordinary cards.\n**Validation:** Featured technique is visually prominent and clearly distinct from regular technique cards.\n**Primary Owner:** M011/S01\n\n## R018 — Random Technique Discovery\n**Status:** validated\n**Description:** A \"Random Technique\" button exists that navigates to a randomly selected technique page.\n**Validation:** Clicking the button loads a different technique each time.\n**Primary Owner:** M011/S01\n\n## R019 — Topics Page Default-Collapsed\n**Status:** validated\n**Description:** Topics page loads with all categories collapsed, showing only category cards. Clicking a category expands its sub-topics with a smooth slide animation.\n**Validation:** Page loads collapsed. Click expands with animation. Click again collapses.\n**Primary Owner:** M011/S02\n\n## R020 — Global Search in Navigation\n**Status:** validated\n**Description:** A compact search input in the navigation bar is available on all pages except the homepage. Cmd+K (or /) keyboard shortcut focuses it from any page.\n**Validation:** Search bar visible in nav on Topics, Creators, Technique, About pages. Cmd+K focuses it.\n**Primary Owner:** M011/S03\n\n## R021 — Mobile Hamburger Menu\n**Status:** validated\n**Description:** Navigation uses a hamburger menu on viewports below 768px with generous touch targets (44×44px minimum).\n**Validation:** Mobile viewport shows hamburger icon. Tapping reveals nav items. Touch targets meet minimum size.\n**Primary Owner:** M011/S03\n\n## R022 — Heading Hierarchy Fix\n**Status:** validated\n**Description:** Every page has exactly one H1 element. Heading levels are sequential (no skips from H1 to H3).\n**Validation:** DOM inspection confirms single H1 per page and sequential heading levels.\n**Primary Owner:** M011/S04\n\n## R023 — Skip-to-Content Link\n**Status:** validated\n**Description:** A visually hidden skip link is the first focusable element on every page, visible on keyboard focus.\n**Validation:** Tab from page load shows \"Skip to content\" link. Clicking it jumps to main content area.\n**Primary Owner:** M011/S04\n\n## R024 — Text Contrast AA Compliance\n**Status:** validated\n**Description:** All text meets WCAG AA contrast ratios (4.5:1 for normal text, 3:1 for large text) against the dark background.\n**Validation:** Secondary text color passes 4.5:1 contrast check against page background.\n**Primary Owner:** M011/S04\n\n## R025 — Page-Specific Document Titles\n**Status:** validated\n**Description:** Each route sets a descriptive document title (e.g., \"Bass — Sound Design — Chrysopedia\", \"COPYCATT — Chrysopedia\").\n**Validation:** Browser tab shows distinct title per page. Navigating between pages updates the title.\n**Primary Owner:** M011/S04\n\n## R026 — Creator Stats Topic-Colored Pills\n**Status:** validated\n**Description:** Creator detail page stats line rendered as topic-colored pill badges instead of run-on text.\n**Validation:** Stats show as visually distinct colored pills grouped by topic category.\n**Primary Owner:** M011/S02\n\n## R027 — Tag Overflow Limit on Cards\n**Status:** validated\n**Description:** Technique cards show a maximum of 4 tags, with a \"+N more\" indicator for additional tags.\n**Validation:** Cards with >4 tags show exactly 4 plus a \"+N more\" badge.\n**Primary Owner:** M011/S02\n\n## R028 — Empty Subtopic Handling\n**Status:** validated\n**Description:** Sub-topics with 0 techniques either hidden by default or display a \"Coming soon\" badge.\n**Validation:** Empty subtopics are visually distinguished or hidden. No dead-end clicks.\n**Primary Owner:** M011/S02\n\n## Denied / Out of Scope\n\n## R029 — Beginner Learning Paths\n**Status:** out-of-scope\n**Description:** Curated beginner learning paths with skill-level tags and \"New to Production?\" section.\n**Validation:** n/a\n**Notes:** Denied — audience knows what they're looking for. Assessment F01.\n\n## R030 — YouTube Deep Links on Key Moments\n**Status:** out-of-scope\n**Description:** Key moment timestamps link to source YouTube videos at the correct time.\n**Validation:** n/a\n**Notes:** Denied — no video URLs in data model / don't want to link out. Assessment F02.\n\n## R031 — Hide Admin Dropdown\n**Status:** out-of-scope\n**Description:** Admin dropdown hidden from public visitors behind auth or flag.\n**Validation:** n/a\n**Notes:** Denied — fine as-is. Assessment F03.\n\n## R032 — CTA Label Changes\n**Status:** out-of-scope\n**Description:** Change \"Start Exploring\" to more descriptive CTA text.\n**Validation:** n/a\n**Notes:** Denied — skipped. Assessment F15.\n\n---\n\n[...truncated 6 sections]\n\n### Dependency Slice Summaries\n\nPay particular attention to **Forward Intelligence** sections — they contain hard-won knowledge about what's fragile, what assumptions changed, and what to watch out for.\n\n#### S03 Summary\nSource: `.gsd/milestones/M014/slices/S03/S03-SUMMARY.md`\n\n---\nid: S03\nparent: M014\nmilestone: M014\nprovides:\n  - body_sections_format column on technique_pages (default 'v1')\n  - technique_page_videos association table\n  - SourceVideoSummary schema\n  - source_videos field on TechniquePageDetail API response\n  - body_sections accepts list | dict | None\nrequires:\n  []\naffects:\n  - S04\n  - S05\n  - S06\nkey_files:\n  - alembic/versions/012_multi_source_format.py\n  - backend/models.py\n  - backend/schemas.py\n  - backend/routers/techniques.py\nkey_decisions:\n  - Used TIMESTAMP (not WITH TIME ZONE) for added_at to stay consistent with existing schema convention\npatterns_established:\n  - Association table pattern with dual CASCADE FKs and unique constraint for many-to-many with metadata (added_at)\n  - body_sections_format discriminator column for handling multiple content formats in the same table\nobservability_surfaces:\n  - none\ndrill_down_paths:\n  - .gsd/milestones/M014/slices/S03/tasks/T01-SUMMARY.md\n  - .gsd/milestones/M014/slices/S03/tasks/T02-SUMMARY.md\nduration: \"\"\nverification_result: passed\ncompleted_at: 2026-04-03T01:20:27.897Z\nblocker_discovered: false\n---\n\n# S03: Data Model + Migration\n\n**Added body_sections_format column, technique_page_videos association table, and wired both into the API response for multi-source technique pages.**\n\n## What Happened\n\nThis slice laid the data foundation for M014's multi-source, nested-section technique pages. Two tasks delivered three changes:\n\n**T01 — Schema + Migration:** Created Alembic migration 012 adding `body_sections_format` (VARCHAR(20), NOT NULL, default 'v1') to technique_pages and a new `technique_page_videos` association table with dual CASCADE foreign keys and a unique constraint on (technique_page_id, source_video_id). Updated SQLAlchemy models with the new `TechniquePageVideo` class and `body_sections_format` column on `TechniquePage`. Widened the Pydantic `body_sections` type from `dict | None` to `list | dict | None` to support both v1 (dict) and v2 (list-of-objects) formats. Added `SourceVideoSummary` schema and `source_videos` field to `TechniquePageDetail`.\n\n**T02 — API Wiring:** Updated `get_technique()` to eagerly load `source_video_links` → `source_video` via chained `selectinload`. Builds the `source_videos` list from association table rows. Ran migration on ub01 and verified the API returns both new fields with correct defaults (`body_sections_format: \"v1\"`, `source_videos: []`).\n\nAll existing technique pages continue to work unchanged — the v1 default ensures backward compatibility.\n\n## Verification\n\nAll slice-level checks passed:\n1. `from models import TechniquePageVideo, TechniquePage; assert hasattr(TechniquePage, 'body_sections_format')` → OK\n2. `from schemas import SourceVideoSummary, TechniquePageDetail` → OK\n3. `alembic upgrade head` on ub01 Docker → clean (migration 012 applied)\n4. `curl` to live API for existing technique → `body_sections_format: \"v1\"` and `source_videos: []` present in response\n\n## New Requirements Surfaced\n\nNone.\n\n## Deviations\n\nNone.\n\n## Known Limitations\n\nNone.\n\n## Follow-ups\n\nNone.\n\n## Files Created/Modified\n\n- `alembic/versions/012_multi_source_format.py` — New migration: body_sections_format column + technique_page_videos table\n- `backend/models.py` — Added TechniquePageVideo model, body_sections_format column, source_video_links relationship\n- `backend/schemas.py` — Widened body_sections type, added SourceVideoSummary, added source_videos to TechniquePageDetail\n- `backend/routers/techniques.py` — Eager-load source_video_links, build source_videos list in technique detail response\n\n#### S04 Summary\nSource: `.gsd/milestones/M014/slices/S04/S04-SUMMARY.md`\n\n---\nid: S04\nparent: M014\nmilestone: M014\nprovides:\n  - Compose-or-create branching in stage5_synthesis\n  - body_sections_format='v2' on all technique pages\n  - TechniquePageVideo join table populated for every page+video combination\nrequires:\n  - slice: S01\n    provides: Synthesis prompt v5 with nested sections + citations format\n  - slice: S02\n    provides: Compose prompt + test harness validation\n  - slice: S03\n    provides: Data model with body_sections_format column and TechniquePageVideo table\naffects:\n  - S06\n  - S07\nkey_files:\n  - backend/pipeline/stages.py\n  - backend/pipeline/test_compose_pipeline.py\nkey_decisions:\n  - Compose detection queries all matching pages and warns on multiple matches, uses first\n  - pg_insert with on_conflict_do_nothing for idempotent TechniquePageVideo inserts\n  - Source-code assertions for branching logic tests instead of fragile full-session mocks\npatterns_established:\n  - Compose-or-create branching: query existing pages by creator_id + LOWER(category), compose into first match, fall through to standard synthesis otherwise\n  - XML-tagged compose prompt with offset-indexed moments: existing [0]-[N-1], new [N]-[N+M-1]\nobservability_surfaces:\n  - INFO log when compose path triggered: 'Stage 5: Composing into existing page ...'\n  - WARNING log when multiple pages match creator+category\ndrill_down_paths:\n  - .gsd/milestones/M014/slices/S04/tasks/T01-SUMMARY.md\n  - .gsd/milestones/M014/slices/S04/tasks/T02-SUMMARY.md\nduration: \"\"\nverification_result: passed\ncompleted_at: 2026-04-03T01:34:24.402Z\nblocker_discovered: false\n---\n\n# S04: Pipeline Compose-or-Create Logic\n\n**Stage 5 now detects existing technique pages by creator+category and branches to a compose path that merges new video content into them, with body_sections_format='v2' and TechniquePageVideo tracking on all pages.**\n\n## What Happened\n\nTwo tasks delivered the compose-or-create pipeline logic and its test suite.\n\nT01 added `_build_compose_user_prompt()` and `_compose_into_existing()` helper functions to stages.py, then wired compose detection into the stage5_synthesis per-category loop. The detection query uses `creator_id + LOWER(topic_category)` for case-insensitive matching. When an existing page matches, the compose path loads its linked moments, builds an XML-tagged prompt with offset-indexed moment references, and calls the LLM with the stage5_compose.txt system prompt. If no match, the existing chunked synthesis path runs unchanged. All pages now get `body_sections_format='v2'` set unconditionally, and a `TechniquePageVideo` row is inserted via `pg_insert` with `on_conflict_do_nothing` for idempotency.\n\nT02 created 12 unit tests across 4 test classes: prompt construction (5 tests for XML structure, offset indices, empty existing moments, page JSON serialization, moment content), branching logic (3 tests using source-code assertions + focused mocks), format/tracking (3 tests for v2 flag, pg_insert usage, INSERT values), and case sensitivity (1 test verifying func.lower on both sides of the category comparison). All 12 pass.\n\n## Verification\n\nAll slice-level verification checks pass:\n1. `PYTHONPATH=backend python -c \"from pipeline.stages import _build_compose_user_prompt, _compose_into_existing; print('imports OK')\"` → exit 0\n2. `grep -q 'body_sections_format' backend/pipeline/stages.py` → exit 0\n3. `grep -q 'TechniquePageVideo' backend/pipeline/stages.py` → exit 0\n4. `grep -q 'stage5_compose' backend/pipeline/stages.py` → exit 0\n5. `PYTHONPATH=backend python -m pytest backend/pipeline/test_compose_pipeline.py -v` → 12 passed in 1.50s\n\n## New Requirements Surfaced\n\nNone.\n\n## Deviations\n\nUsed `default=str` in `json.dumps()` for page serialization in `_build_compose_user_prompt()` to handle UUID/datetime fields — not in plan but necessary for robustness. T02 replaced integration-level branching tests with source-code structure assertions + focused unit tests due to session mock fragility.\n\n## Known Limitations\n\nNone.\n\n## Follow-ups\n\nNone.\n\n## Files Created/Modified\n\n- `backend/pipeline/stages.py` — Added _build_compose_user_prompt(), _compose_into_existing(), compose-or-create branching in stage5_synthesis, body_sections_format='v2' setting, TechniquePageVideo insertion\n- `backend/pipeline/test_compose_pipeline.py` — New file: 12 unit tests covering compose prompt construction, branching logic, format tracking, and case-insensitive matching\n\n## Your Role in the Pipeline\n\nYou are the scout. After you finish, a **planner agent** reads your output in a fresh context with no memory of your exploration. It uses your findings to decompose this slice into executable tasks — deciding what files change, what order to build things, how to verify the work. Then **executor agents** build each task in isolated context windows.\n\nWrite for the planner, not for a human. The planner needs:\n- **What files exist and what they do** — so it can scope tasks to specific files\n- **Where the natural seams are** — where work divides into independent units\n- **What to build or prove first** — what's riskiest, what unblocks everything else\n- **How to verify the result** — what commands, tests, or checks confirm the slice works\n\nIf the research doc is vague, the planner will waste its context re-exploring code you already read. If it's precise, the planner can decompose immediately.\n\n## Calibrate Depth\n\nRead the slice title, the roadmap excerpt, and any milestone research above. Ask: does this slice involve unfamiliar technology, risky integration, novel architecture, or ambiguous requirements? Or is it straightforward application of known patterns to known code?\n\n- **Deep research** — new technology, unfamiliar APIs, risky integration, multiple viable approaches, or ambiguous scope. Explore broadly, look up docs, check libraries, investigate constraints. Write all template sections. This is the default when you're genuinely uncertain.\n- **Targeted research** — known technology but new to this codebase, or moderately complex integration. Explore the relevant code, check one or two libraries, identify constraints. Omit Don't Hand-Roll and Sources if nothing applies.\n- **Light research** — well-understood work using established patterns already in the codebase (wiring up existing APIs, adding standard UI components, CRUD operations, configuration changes). Read the relevant files to confirm the pattern, note any constraints, write Summary + Recommendation + Implementation Landscape. Skip the rest. A light research doc can be 15-20 lines. Don't manufacture pitfalls or risks for work that doesn't have them.\n\nAn honest \"this is straightforward, here's the pattern to follow\" is more valuable than invented complexity.\n\n## Steps\n\nResearch what this slice needs. Narrate key findings and surprises as you go — what exists, what's missing, what constrains the approach.\n0. If `REQUIREMENTS.md` was preloaded above, identify which Active requirements this slice owns or supports. Research should target these requirements — surfacing risks, unknowns, and implementation constraints that could affect whether the slice actually delivers them.\n1.  Reference specific rules from loaded skills in your findings where they inform the implementation approach.\n2. **Skill Discovery (suggest):**\n   Identify the key technologies, frameworks, and services this work depends on (e.g. Stripe, Clerk, Supabase, JUCE, SwiftUI).\n   For each, check if a professional agent skill already exists:\n   - First check `<available_skills>` in your system prompt — a skill may already be installed.\n   - For technologies without an installed skill, run: `npx skills find \"<technology>\"`\n   - Only consider skills that are **directly relevant** to core technologies — not tangentially related.\n   - Evaluate results by install count and relevance to the actual work.\n   - Note promising skills in your research output with their install commands, but do NOT install them.\n   - The user will decide which to install.\n3. Explore relevant code for this slice's scope. For targeted exploration, use `rg`, `find`, and reads. For broad or unfamiliar subsystems, use `scout` to map the relevant area first.\n4. Use `resolve_library` / `get_library_docs` for unfamiliar libraries — skip this for libraries already used in the codebase\n5. **Web search budget:** You have a limited budget of web searches (max ~15 per session). Use them strategically — prefer `resolve_library` / `get_library_docs` for library documentation. Do NOT repeat the same or similar queries. If a search didn't find what you need, rephrase once or move on. Target 3-5 total web searches for a typical research unit.\n6. Use the **Research** output template from the inlined context above — include only sections that have real content. The template is already inlined above; do NOT attempt to read any template file from disk (there is no `templates/SLICE-RESEARCH.md` — the correct template is already present in this prompt).\n7. Call `gsd_summary_save` with `milestone_id: M014`, `slice_id: S06`, `artifact_type: \"RESEARCH\"`, and the full research markdown as `content` — the tool computes the file path and persists to both DB and disk.\n\nThe slice directory already exists at `.gsd/milestones/M014/slices/S06/`. Do NOT mkdir.\n\n**You MUST call `gsd_summary_save` with the research content before finishing.**\n\nWhen done, say: \"Slice S06 researched.\"","display":false,"id":"d72b9134","parentId":"798bf286","timestamp":"2026-04-03T01:47:51.088Z"}
{"type":"message","id":"2bc2b7ea","parentId":"d72b9134","timestamp":"2026-04-03T01:48:00.159Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"Let me research slice S06: \"Admin UI — Multi-Source Pipeline Management\". The roadmap says:\n\n> Admin view for multi-source page shows source dropdown, composition history, per-video chunking inspection.\n\nThis depends on S03 (data model) and S04 (pipeline compose logic). Let me understand what exists and what needs to be built.\n\nThis is a frontend admin UI slice. Let me check the existing admin UI patterns, the data model from S03/S04, and what API endpoints exist or need to be created.","thinkingSignature":"EqgFClkIDBgCKkCATUuHvRo4oJb9gQUCgMVpdDO/Y8bZIzfQuJc5xh/BoAbbB4HwvYppsZnqxvCzhKpo/N4f6A1pEuEjSdJWCVd/Mg9jbGF1ZGUtb3B1cy00LTY4ABIMvIik7skMtiGjnFOYGgwJcZ4oUKNQlPrhkLYiMMPK5NaJjtU3Z78EKzUloptJp0lPODZf/6Wai5/2mhssEip2z8s4qLznvKwx6lj1gCr8A1LCfn8924VvawSGxuGIiDDCvuB0vO7LRU7yB9AAIUmAD2dlw1FY7eQkiZLqqKEetj5buhZhEeMISvmV9aXCNvmsjP6/TukQJs7lKByU5EIzoYynRWF0rLNv0p1dRTYmae0+DskoYpCbXmy2D9NHgKqk8xOtnIy8aSfVnk3WCjGQLtcH481YAvJ6ofDrmRs45WthNhwHUGCXFSdloIACH2OY7v93flXohH2pRfjpi0N2QX0ktwE6ZsumzwxC04RTElZHf+Zj3Mqj4wXjMnXGAYZMPa4aN0Zo46cq0qOQPNH0YwimIo3z/pvNq6g1cJt6bq1eFPx+gnbSr928ypynHbjeOK1t77d85UAN/KJxZKT0uQEEeyrnmT0Xt5V+AVkyzhDXo5gal9NYxHXGhJngiuFkKXAhiZcIGBbhtdqxb7tEZjd9FLLPejlwMRg3L4pO3uSUKsO21rDFHYSTdkpVw0HLK4Eq+lSg4FlLsttNb05/I5Pa/ZOTukBrZWRQwK7eVD+eKer46NCzPGoY1+QK0gnaARpFiCCPPmRwpEshAXNc6ABffSdCVeJmN7rTlQXCzK0umo7OMtgCHu5TcQZRgA3v5VL6XJbKHY0Kzb0ev3ZJyQBRpOgejiAs78bYCABJhu9KxYCI36FpwENOJJQV57xNrCYhktCR2RlKbXYYAQ=="},{"type":"text","text":"Looking at S06: Admin UI for multi-source pipeline management. Depends on S03 (data model) and S04 (compose logic). Let me check what admin UI patterns exist and what API surface is available."},{"type":"toolCall","id":"toolu_016U6XTipY59k76FXxcUweeV","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && find frontend/src -path '*admin*' -o -path '*Admin*' -o -path '*review*' -o -path '*Review*' | head -30"}},{"type":"toolCall","id":"toolu_0194FfkT4PqvWqrzDbJh8UD8","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && find backend/routers -name '*.py' | xargs head -5"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":3,"output":354,"cacheRead":59865,"cacheWrite":7690,"totalTokens":67912,"cost":{"input":0.000015000000000000002,"output":0.00885,"cacheRead":0.029932499999999997,"cacheWrite":0.0480625,"total":0.08685999999999999}},"stopReason":"toolUse","timestamp":1775180871086}}
{"type":"message","id":"e8ff6f8e","parentId":"2bc2b7ea","timestamp":"2026-04-03T01:48:00.172Z","message":{"role":"toolResult","toolCallId":"toolu_016U6XTipY59k76FXxcUweeV","toolName":"bash","content":[{"type":"text","text":"frontend/src/pages/AdminPipeline.tsx\nfrontend/src/pages/AdminReports.tsx\nfrontend/src/components/AdminDropdown.tsx\n"}],"isError":false,"timestamp":1775180880172}}
{"type":"message","id":"7f1ae5d4","parentId":"e8ff6f8e","timestamp":"2026-04-03T01:48:00.175Z","message":{"role":"toolResult","toolCallId":"toolu_0194FfkT4PqvWqrzDbJh8UD8","toolName":"bash","content":[{"type":"text","text":"==> backend/routers/techniques.py <==\n\"\"\"Technique page endpoints — list and detail with eager-loaded relations.\"\"\"\n\nfrom __future__ import annotations\n\nimport logging\n\n==> backend/routers/topics.py <==\n\"\"\"Topics endpoint — two-level category hierarchy with aggregated counts.\"\"\"\n\nfrom __future__ import annotations\n\nimport logging\n\n==> backend/routers/reports.py <==\n\"\"\"Content reports router — public submission + admin management.\n\nPublic:\n  POST /reports              Submit a content issue report\n\n\n==> backend/routers/health.py <==\n\"\"\"Health check endpoints for Chrysopedia API.\"\"\"\n\nimport logging\n\nfrom fastapi import APIRouter, Depends\n\n==> backend/routers/videos.py <==\n\"\"\"Source video endpoints for Chrysopedia API.\"\"\"\n\nimport logging\nfrom typing import Annotated\n\n\n==> backend/routers/pipeline.py <==\n\"\"\"Pipeline management endpoints — public trigger + admin dashboard.\n\nPublic:\n  POST /pipeline/trigger/{video_id}    Trigger pipeline for a video\n\n\n==> backend/routers/__init__.py <==\n\"\"\"Chrysopedia API routers package.\"\"\"\n\n==> backend/routers/creators.py <==\n\"\"\"Creator endpoints for Chrysopedia API.\n\nEnhanced with sort (random default per R014), genre filter, and\ntechnique/video counts for browse pages.\n\"\"\"\n\n==> backend/routers/ingest.py <==\n\"\"\"Transcript ingestion endpoint for the Chrysopedia API.\n\nAccepts a Whisper-format transcript JSON via multipart file upload, finds or\ncreates a Creator, upserts a SourceVideo, bulk-inserts TranscriptSegments,\npersists the raw JSON to disk, and returns a structured response.\n\n==> backend/routers/search.py <==\n\"\"\"Search endpoint for semantic + keyword search with graceful fallback.\"\"\"\n\nfrom __future__ import annotations\n\nimport logging\n"}],"isError":false,"timestamp":1775180880173}}
{"type":"message","id":"e67bd01f","parentId":"7f1ae5d4","timestamp":"2026-04-03T01:48:03.577Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_01HfYsPd3ugpVNX3joUrgAQa","name":"read","arguments":{"path":"backend/routers/pipeline.py"}},{"type":"toolCall","id":"toolu_013n4VAyKF4KWhYoAN8tW7oa","name":"read","arguments":{"path":"frontend/src/pages/AdminPipeline.tsx"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":103,"cacheRead":67555,"cacheWrite":755,"totalTokens":68414,"cost":{"input":0.000005,"output":0.002575,"cacheRead":0.033777499999999995,"cacheWrite":0.00471875,"total":0.041076249999999995}},"stopReason":"toolUse","timestamp":1775180880174}}
{"type":"message","id":"dbfbb8f8","parentId":"e67bd01f","timestamp":"2026-04-03T01:48:03.584Z","message":{"role":"toolResult","toolCallId":"toolu_01HfYsPd3ugpVNX3joUrgAQa","toolName":"read","content":[{"type":"text","text":"\"\"\"Pipeline management endpoints — public trigger + admin dashboard.\n\nPublic:\n  POST /pipeline/trigger/{video_id}    Trigger pipeline for a video\n\nAdmin:\n  GET  /admin/pipeline/videos          Video list with status + event counts\n  POST /admin/pipeline/trigger/{video_id}  Retrigger (same as public but under admin prefix)\n  POST /admin/pipeline/revoke/{video_id}   Revoke/cancel active tasks for a video\n  GET  /admin/pipeline/events/{video_id}   Event log for a video (paginated)\n  GET  /admin/pipeline/worker-status       Active/reserved tasks from Celery inspect\n\"\"\"\n\nimport logging\nimport uuid\nfrom datetime import datetime, timezone\nfrom typing import Annotated\n\nfrom fastapi import APIRouter, Depends, HTTPException, Query\nfrom sqlalchemy import func, select, case\nfrom sqlalchemy.ext.asyncio import AsyncSession\n\nfrom config import get_settings\nfrom database import get_session\nfrom models import PipelineEvent, PipelineRun, PipelineRunStatus, SourceVideo, Creator, KeyMoment, TranscriptSegment, ProcessingStatus\nfrom redis_client import get_redis\nfrom schemas import DebugModeResponse, DebugModeUpdate, TokenStageSummary, TokenSummaryResponse\n\nlogger = logging.getLogger(\"chrysopedia.pipeline\")\n\nrouter = APIRouter(tags=[\"pipeline\"])\n\nREDIS_DEBUG_MODE_KEY = \"chrysopedia:debug_mode\"\n\n\n# ── Public trigger ───────────────────────────────────────────────────────────\n\n@router.post(\"/pipeline/trigger/{video_id}\")\nasync def trigger_pipeline(\n    video_id: str,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Manually trigger (or re-trigger) the LLM extraction pipeline for a video.\"\"\"\n    stmt = select(SourceVideo).where(SourceVideo.id == video_id)\n    result = await db.execute(stmt)\n    video = result.scalar_one_or_none()\n\n    if video is None:\n        raise HTTPException(status_code=404, detail=f\"Video not found: {video_id}\")\n\n    from pipeline.stages import run_pipeline\n\n    try:\n        run_pipeline.delay(str(video.id), trigger=\"manual\")\n        logger.info(\"Pipeline manually triggered for video_id=%s\", video_id)\n    except Exception as exc:\n        logger.warning(\"Failed to dispatch pipeline for video_id=%s: %s\", video_id, exc)\n        raise HTTPException(\n            status_code=503,\n            detail=\"Pipeline dispatch failed — Celery/Redis may be unavailable\",\n        ) from exc\n\n    return {\n        \"status\": \"triggered\",\n        \"video_id\": str(video.id),\n        \"current_processing_status\": video.processing_status.value,\n    }\n\n\n# ── Admin: Video list ────────────────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/videos\")\nasync def list_pipeline_videos(\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"List all videos with processing status and pipeline event counts.\"\"\"\n    # Subquery for event counts per video\n    event_counts = (\n        select(\n            PipelineEvent.video_id,\n            func.count().label(\"event_count\"),\n            func.sum(case(\n                (PipelineEvent.event_type == \"llm_call\", PipelineEvent.total_tokens),\n                else_=0\n            )).label(\"total_tokens_used\"),\n            func.max(PipelineEvent.created_at).label(\"last_event_at\"),\n        )\n        .group_by(PipelineEvent.video_id)\n        .subquery()\n    )\n\n    # Subquery for the most recent pipeline run per video\n    latest_run = (\n        select(\n            PipelineRun.video_id,\n            PipelineRun.id.label(\"run_id\"),\n            PipelineRun.run_number,\n            PipelineRun.trigger.label(\"run_trigger\"),\n            PipelineRun.status.label(\"run_status\"),\n            PipelineRun.started_at.label(\"run_started_at\"),\n            PipelineRun.finished_at.label(\"run_finished_at\"),\n            PipelineRun.error_stage.label(\"run_error_stage\"),\n            PipelineRun.total_tokens.label(\"run_total_tokens\"),\n        )\n        .order_by(PipelineRun.video_id, PipelineRun.started_at.desc())\n        .distinct(PipelineRun.video_id)\n        .subquery()\n    )\n\n    # Subquery for the most recent stage start event per video (active stage indicator)\n    latest_stage = (\n        select(\n            PipelineEvent.video_id,\n            PipelineEvent.stage.label(\"active_stage\"),\n            PipelineEvent.event_type.label(\"active_stage_status\"),\n            PipelineEvent.created_at.label(\"stage_started_at\"),\n        )\n        .where(PipelineEvent.event_type.in_([\"start\", \"complete\", \"error\"]))\n        .order_by(PipelineEvent.video_id, PipelineEvent.created_at.desc())\n        .distinct(PipelineEvent.video_id)\n        .subquery()\n    )\n\n    stmt = (\n        select(\n            SourceVideo.id,\n            SourceVideo.filename,\n            SourceVideo.processing_status,\n            SourceVideo.content_hash,\n            SourceVideo.created_at,\n            SourceVideo.updated_at,\n            Creator.name.label(\"creator_name\"),\n            event_counts.c.event_count,\n            event_counts.c.total_tokens_used,\n            event_counts.c.last_event_at,\n            latest_stage.c.active_stage,\n            latest_stage.c.active_stage_status,\n            latest_stage.c.stage_started_at,\n            latest_run.c.run_id,\n            latest_run.c.run_number,\n            latest_run.c.run_trigger,\n            latest_run.c.run_status,\n            latest_run.c.run_started_at,\n            latest_run.c.run_finished_at,\n            latest_run.c.run_error_stage,\n            latest_run.c.run_total_tokens,\n        )\n        .join(Creator, SourceVideo.creator_id == Creator.id)\n        .outerjoin(event_counts, SourceVideo.id == event_counts.c.video_id)\n        .outerjoin(latest_stage, SourceVideo.id == latest_stage.c.video_id)\n        .outerjoin(latest_run, SourceVideo.id == latest_run.c.video_id)\n        .order_by(SourceVideo.updated_at.desc())\n    )\n\n    result = await db.execute(stmt)\n    rows = result.all()\n\n    return {\n        \"items\": [\n            {\n                \"id\": str(r.id),\n                \"filename\": r.filename,\n                \"processing_status\": r.processing_status.value if hasattr(r.processing_status, 'value') else str(r.processing_status),\n                \"content_hash\": r.content_hash,\n                \"creator_name\": r.creator_name,\n                \"created_at\": r.created_at.isoformat() if r.created_at else None,\n                \"updated_at\": r.updated_at.isoformat() if r.updated_at else None,\n                \"event_count\": r.event_count or 0,\n                \"total_tokens_used\": r.total_tokens_used or 0,\n                \"last_event_at\": r.last_event_at.isoformat() if r.last_event_at else None,\n                \"active_stage\": r.active_stage,\n                \"active_stage_status\": r.active_stage_status,\n                \"stage_started_at\": r.stage_started_at.isoformat() if r.stage_started_at else None,\n                \"latest_run\": {\n                    \"id\": str(r.run_id),\n                    \"run_number\": r.run_number,\n                    \"trigger\": r.run_trigger.value if hasattr(r.run_trigger, 'value') else r.run_trigger,\n                    \"status\": r.run_status.value if hasattr(r.run_status, 'value') else r.run_status,\n                    \"started_at\": r.run_started_at.isoformat() if r.run_started_at else None,\n                    \"finished_at\": r.run_finished_at.isoformat() if r.run_finished_at else None,\n                    \"error_stage\": r.run_error_stage,\n                    \"total_tokens\": r.run_total_tokens or 0,\n                } if r.run_id else None,\n            }\n            for r in rows\n        ],\n        \"total\": len(rows),\n    }\n\n\n# ── Admin: Retrigger ─────────────────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/trigger/{video_id}\")\nasync def admin_trigger_pipeline(\n    video_id: str,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Admin retrigger — same as public trigger.\"\"\"\n    return await trigger_pipeline(video_id, db)\n\n\n# ── Admin: Clean Retrigger ───────────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/clean-retrigger/{video_id}\")\nasync def clean_retrigger_pipeline(\n    video_id: str,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Wipe prior pipeline output for a video, then retrigger.\n\n    Deletes: pipeline_events, key_moments, transcript_segments,\n    and associated Qdrant vectors. Resets processing_status to 'not_started'.\n    Does NOT delete technique_pages — the pipeline re-synthesizes via upsert.\n    \"\"\"\n    stmt = select(SourceVideo).where(SourceVideo.id == video_id)\n    result = await db.execute(stmt)\n    video = result.scalar_one_or_none()\n\n    if video is None:\n        raise HTTPException(status_code=404, detail=f\"Video not found: {video_id}\")\n\n    # Delete pipeline events\n    await db.execute(\n        PipelineEvent.__table__.delete().where(PipelineEvent.video_id == video_id)\n    )\n    # Delete key moments\n    await db.execute(\n        KeyMoment.__table__.delete().where(KeyMoment.source_video_id == video_id)\n    )\n    # Note: transcript_segments are NOT deleted — they are the pipeline's input\n    # data created during ingest, not pipeline output. Deleting them would leave\n    # the pipeline with nothing to process.\n    \n    # Reset status\n    video.processing_status = ProcessingStatus.not_started\n    await db.commit()\n\n    deleted_counts = {\n        \"pipeline_events\": \"cleared\",\n        \"key_moments\": \"cleared\",\n    }\n\n    # Best-effort Qdrant cleanup (non-blocking)\n    try:\n        settings = get_settings()\n        from pipeline.qdrant_client import QdrantManager\n        qdrant = QdrantManager(settings)\n        qdrant.delete_by_video_id(str(video_id))\n        deleted_counts[\"qdrant_vectors\"] = \"cleared\"\n    except Exception as exc:\n        logger.warning(\"Qdrant cleanup failed for video_id=%s: %s\", video_id, exc)\n        deleted_counts[\"qdrant_vectors\"] = f\"skipped: {exc}\"\n\n    # Clear Redis classification/prior-pages cache to prevent stale data\n    try:\n        import redis as redis_lib\n        settings = get_settings()\n        r = redis_lib.Redis.from_url(settings.redis_url)\n        r.delete(f\"chrysopedia:classification:{video_id}\")\n        r.delete(f\"chrysopedia:prior_pages:{video_id}\")\n        deleted_counts[\"redis_cache\"] = \"cleared\"\n        logger.info(\"Redis cache cleared for video_id=%s\", video_id)\n    except Exception as exc:\n        logger.warning(\"Redis cache cleanup failed for video_id=%s: %s\", video_id, exc)\n        deleted_counts[\"redis_cache\"] = f\"skipped: {exc}\"\n\n    # Clear durable classification_data column too\n    video.classification_data = None\n    await db.commit()\n\n    # Now trigger the pipeline\n    from pipeline.stages import run_pipeline\n    try:\n        run_pipeline.delay(str(video.id), trigger=\"clean_reprocess\")\n        logger.info(\"Clean retrigger dispatched for video_id=%s\", video_id)\n    except Exception as exc:\n        logger.warning(\"Failed to dispatch pipeline after cleanup for video_id=%s: %s\", video_id, exc)\n        raise HTTPException(\n            status_code=503,\n            detail=\"Cleanup succeeded but pipeline dispatch failed — Celery/Redis may be unavailable\",\n        ) from exc\n\n    return {\n        \"status\": \"clean_retriggered\",\n        \"video_id\": str(video.id),\n        \"cleaned\": deleted_counts,\n    }\n\n\n# ── Admin: Re-run Single Stage ──────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/rerun-stage/{video_id}/{stage_name}\")\nasync def rerun_stage(\n    video_id: str,\n    stage_name: str,\n    prompt_override: str | None = None,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Re-run a single pipeline stage without running predecessors.\n\n    Designed for fast prompt iteration — especially stage 5 synthesis.\n    Optionally accepts a prompt_override string that temporarily replaces\n    the on-disk prompt template for this run only.\n\n    Valid stage names: stage2_segmentation, stage3_extraction,\n    stage4_classification, stage5_synthesis, stage6_embed_and_index.\n    \"\"\"\n    from pipeline.stages import _PIPELINE_STAGES, run_single_stage\n\n    # Validate stage name\n    if stage_name not in _PIPELINE_STAGES:\n        raise HTTPException(\n            status_code=400,\n            detail=f\"Invalid stage '{stage_name}'. Valid: {_PIPELINE_STAGES}\",\n        )\n\n    # Validate video exists\n    stmt = select(SourceVideo).where(SourceVideo.id == video_id)\n    result = await db.execute(stmt)\n    video = result.scalar_one_or_none()\n    if video is None:\n        raise HTTPException(status_code=404, detail=f\"Video not found: {video_id}\")\n\n    # Dispatch single-stage re-run\n    try:\n        run_single_stage.delay(\n            str(video.id),\n            stage_name,\n            trigger=\"stage_rerun\",\n            prompt_override=prompt_override,\n        )\n        logger.info(\n            \"Single-stage re-run dispatched: video_id=%s, stage=%s, prompt_override=%s\",\n            video_id, stage_name, \"yes\" if prompt_override else \"no\",\n        )\n    except Exception as exc:\n        logger.warning(\n            \"Failed to dispatch single-stage re-run for video_id=%s: %s\", video_id, exc,\n        )\n        raise HTTPException(\n            status_code=503,\n            detail=\"Stage re-run dispatch failed — Celery/Redis may be unavailable\",\n        ) from exc\n\n    return {\n        \"status\": \"stage_rerun_dispatched\",\n        \"video_id\": str(video.id),\n        \"stage\": stage_name,\n        \"prompt_override\": bool(prompt_override),\n    }\n\n\n# ── Admin: Chunking Inspector ───────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/chunking/{video_id}\")\nasync def get_chunking_data(\n    video_id: str,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Return chunking/grouping data for a video — topic boundaries, classifications,\n    and synthesis group breakdowns.\n\n    Helps diagnose whether bad synthesis output is a prompt problem or a data shape problem.\n    \"\"\"\n    from config import get_settings\n\n    stmt = select(SourceVideo).where(SourceVideo.id == video_id)\n    result = await db.execute(stmt)\n    video = result.scalar_one_or_none()\n    if video is None:\n        raise HTTPException(status_code=404, detail=f\"Video not found: {video_id}\")\n\n    settings = get_settings()\n\n    # 1. Topic boundaries (stage 2 output): segments grouped by topic_label\n    segments = (\n        await db.execute(\n            select(TranscriptSegment)\n            .where(TranscriptSegment.source_video_id == video_id)\n            .order_by(TranscriptSegment.segment_index)\n        )\n    ).scalars().all()\n\n    topic_boundaries: list[dict] = []\n    current_label = None\n    current_group: list[dict] = []\n\n    for seg in segments:\n        label = seg.topic_label or \"unlabeled\"\n        if label != current_label:\n            if current_group:\n                topic_boundaries.append({\n                    \"topic_label\": current_label,\n                    \"segment_count\": len(current_group),\n                    \"start_time\": current_group[0][\"start_time\"],\n                    \"end_time\": current_group[-1][\"end_time\"],\n                    \"start_index\": current_group[0][\"segment_index\"],\n                    \"end_index\": current_group[-1][\"segment_index\"],\n                })\n            current_label = label\n            current_group = []\n\n        current_group.append({\n            \"start_time\": seg.start_time,\n            \"end_time\": seg.end_time,\n            \"segment_index\": seg.segment_index,\n        })\n\n    if current_group:\n        topic_boundaries.append({\n            \"topic_label\": current_label,\n            \"segment_count\": len(current_group),\n            \"start_time\": current_group[0][\"start_time\"],\n            \"end_time\": current_group[-1][\"end_time\"],\n            \"start_index\": current_group[0][\"segment_index\"],\n            \"end_index\": current_group[-1][\"segment_index\"],\n        })\n\n    # 2. Key moments (stage 3 output)\n    moments = (\n        await db.execute(\n            select(KeyMoment)\n            .where(KeyMoment.source_video_id == video_id)\n            .order_by(KeyMoment.start_time)\n        )\n    ).scalars().all()\n\n    key_moments = [\n        {\n            \"id\": str(m.id),\n            \"title\": m.title,\n            \"content_type\": m.content_type.value,\n            \"start_time\": m.start_time,\n            \"end_time\": m.end_time,\n            \"plugins\": m.plugins or [],\n            \"technique_page_id\": str(m.technique_page_id) if m.technique_page_id else None,\n        }\n        for m in moments\n    ]\n\n    # 3. Classification data (stage 4 output)\n    classification: list[dict] = []\n    cls_source = \"missing\"\n\n    if video.classification_data:\n        classification = video.classification_data\n        cls_source = \"postgresql\"\n    else:\n        try:\n            import redis as redis_lib\n            r = redis_lib.Redis.from_url(settings.redis_url)\n            raw = r.get(f\"chrysopedia:classification:{video_id}\")\n            if raw:\n                classification = __import__(\"json\").loads(raw)\n                cls_source = \"redis\"\n        except Exception:\n            pass\n\n    # 4. Synthesis groups (how stage 5 would group moments)\n    cls_by_moment_id = {c[\"moment_id\"]: c for c in classification}\n    synthesis_groups: dict[str, dict] = {}\n\n    for m in moments:\n        cls_info = cls_by_moment_id.get(str(m.id), {})\n        category = cls_info.get(\"topic_category\", \"Uncategorized\").strip().title()\n        if category not in synthesis_groups:\n            synthesis_groups[category] = {\n                \"category\": category,\n                \"moment_count\": 0,\n                \"moment_ids\": [],\n                \"exceeds_chunk_threshold\": False,\n            }\n        synthesis_groups[category][\"moment_count\"] += 1\n        synthesis_groups[category][\"moment_ids\"].append(str(m.id))\n\n    chunk_size = settings.synthesis_chunk_size\n    for group in synthesis_groups.values():\n        group[\"exceeds_chunk_threshold\"] = group[\"moment_count\"] > chunk_size\n        group[\"chunks_needed\"] = max(1, -(-group[\"moment_count\"] // chunk_size))  # ceil division\n\n    return {\n        \"video_id\": str(video.id),\n        \"total_segments\": len(segments),\n        \"total_moments\": len(moments),\n        \"classification_source\": cls_source,\n        \"synthesis_chunk_size\": chunk_size,\n        \"topic_boundaries\": topic_boundaries,\n        \"key_moments\": key_moments,\n        \"classification\": classification,\n        \"synthesis_groups\": list(synthesis_groups.values()),\n    }\n\n\n# ── Admin: Revoke ────────────────────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/revoke/{video_id}\")\nasync def revoke_pipeline(\n    video_id: str,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Revoke/cancel active Celery tasks for a video.\n    \n    Uses Celery's revoke with terminate=True to kill running tasks.\n    Also marks the latest running pipeline_run as cancelled.\n    This is best-effort — the task may have already completed.\n    \"\"\"\n    from worker import celery_app\n    \n    try:\n        # Get active tasks and revoke any matching this video_id\n        inspector = celery_app.control.inspect()\n        active = inspector.active() or {}\n        revoked_count = 0\n        \n        for _worker, tasks in active.items():\n            for task in tasks:\n                task_args = task.get(\"args\", [])\n                if task_args and str(task_args[0]) == video_id:\n                    celery_app.control.revoke(task[\"id\"], terminate=True)\n                    revoked_count += 1\n                    logger.info(\"Revoked task %s for video_id=%s\", task[\"id\"], video_id)\n\n        # Mark any running pipeline_runs as cancelled\n        running_runs = await db.execute(\n            select(PipelineRun).where(\n                PipelineRun.video_id == video_id,\n                PipelineRun.status == PipelineRunStatus.running,\n            )\n        )\n        for run in running_runs.scalars().all():\n            run.status = PipelineRunStatus.cancelled\n            run.finished_at = datetime.now(timezone.utc).replace(tzinfo=None)\n        await db.commit()\n        \n        return {\n            \"status\": \"revoked\" if revoked_count > 0 else \"no_active_tasks\",\n            \"video_id\": video_id,\n            \"tasks_revoked\": revoked_count,\n        }\n    except Exception as exc:\n        logger.warning(\"Failed to revoke tasks for video_id=%s: %s\", video_id, exc)\n        raise HTTPException(\n            status_code=503,\n            detail=\"Failed to communicate with Celery worker\",\n        ) from exc\n\n\n# ── Admin: Recent activity feed ──────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/recent-activity\")\nasync def recent_pipeline_activity(\n    limit: Annotated[int, Query(ge=1, le=20)] = 10,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Get the most recent pipeline stage completions and errors with video context.\"\"\"\n    stmt = (\n        select(\n            PipelineEvent.id,\n            PipelineEvent.video_id,\n            PipelineEvent.stage,\n            PipelineEvent.event_type,\n            PipelineEvent.total_tokens,\n            PipelineEvent.duration_ms,\n            PipelineEvent.created_at,\n            SourceVideo.filename,\n            Creator.name.label(\"creator_name\"),\n        )\n        .join(SourceVideo, PipelineEvent.video_id == SourceVideo.id)\n        .join(Creator, SourceVideo.creator_id == Creator.id)\n        .where(PipelineEvent.event_type.in_([\"complete\", \"error\"]))\n        .order_by(PipelineEvent.created_at.desc())\n        .limit(limit)\n    )\n    result = await db.execute(stmt)\n    rows = result.all()\n\n    return {\n        \"items\": [\n            {\n                \"id\": str(r.id),\n                \"video_id\": str(r.video_id),\n                \"filename\": r.filename,\n                \"creator_name\": r.creator_name,\n                \"stage\": r.stage,\n                \"event_type\": r.event_type,\n                \"total_tokens\": r.total_tokens,\n                \"duration_ms\": r.duration_ms,\n                \"created_at\": r.created_at.isoformat() if r.created_at else None,\n            }\n            for r in rows\n        ],\n    }\n\n\n# ── Admin: Pipeline runs ─────────────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/runs/{video_id}\")\nasync def list_pipeline_runs(\n    video_id: str,\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"List all pipeline runs for a video, newest first.\"\"\"\n    # Count events per run\n    event_counts = (\n        select(\n            PipelineEvent.run_id,\n            func.count().label(\"event_count\"),\n        )\n        .where(PipelineEvent.run_id.isnot(None))\n        .group_by(PipelineEvent.run_id)\n        .subquery()\n    )\n\n    stmt = (\n        select(\n            PipelineRun,\n            event_counts.c.event_count,\n        )\n        .outerjoin(event_counts, PipelineRun.id == event_counts.c.run_id)\n        .where(PipelineRun.video_id == video_id)\n        .order_by(PipelineRun.started_at.desc())\n    )\n    result = await db.execute(stmt)\n    rows = result.all()\n\n    # Also count legacy events (run_id IS NULL) for this video\n    legacy_count_result = await db.execute(\n        select(func.count())\n        .select_from(PipelineEvent)\n        .where(PipelineEvent.video_id == video_id, PipelineEvent.run_id.is_(None))\n    )\n    legacy_count = legacy_count_result.scalar() or 0\n\n    items = []\n    for run, evt_count in rows:\n        items.append({\n            \"id\": str(run.id),\n            \"run_number\": run.run_number,\n            \"trigger\": run.trigger.value if hasattr(run.trigger, 'value') else str(run.trigger),\n            \"status\": run.status.value if hasattr(run.status, 'value') else str(run.status),\n            \"started_at\": run.started_at.isoformat() if run.started_at else None,\n            \"finished_at\": run.finished_at.isoformat() if run.finished_at else None,\n            \"error_stage\": run.error_stage,\n            \"total_tokens\": run.total_tokens or 0,\n            \"event_count\": evt_count or 0,\n        })\n\n    return {\n        \"items\": items,\n        \"legacy_event_count\": legacy_count,\n    }\n\n\n# ── Admin: Event log ─────────────────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/events/{video_id}\")\nasync def list_pipeline_events(\n    video_id: str,\n    offset: Annotated[int, Query(ge=0)] = 0,\n    limit: Annotated[int, Query(ge=1, le=200)] = 100,\n    stage: Annotated[str | None, Query(description=\"Filter by stage name\")] = None,\n    event_type: Annotated[str | None, Query(description=\"Filter by event type\")] = None,\n    run_id: Annotated[str | None, Query(description=\"Filter by pipeline run ID\")] = None,\n    order: Annotated[str, Query(description=\"Sort order: asc or desc\")] = \"desc\",\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Get pipeline events for a video. Default: newest first (desc).\"\"\"\n    stmt = select(PipelineEvent).where(PipelineEvent.video_id == video_id)\n    \n    if run_id:\n        stmt = stmt.where(PipelineEvent.run_id == run_id)\n    if stage:\n        stmt = stmt.where(PipelineEvent.stage == stage)\n    if event_type:\n        stmt = stmt.where(PipelineEvent.event_type == event_type)\n    \n    # Validate order param\n    if order not in (\"asc\", \"desc\"):\n        raise HTTPException(status_code=400, detail=\"order must be 'asc' or 'desc'\")\n\n    # Count\n    count_stmt = select(func.count()).select_from(stmt.subquery())\n    total = (await db.execute(count_stmt)).scalar() or 0\n    \n    # Fetch\n    order_clause = PipelineEvent.created_at.asc() if order == \"asc\" else PipelineEvent.created_at.desc()\n    stmt = stmt.order_by(order_clause)\n    stmt = stmt.offset(offset).limit(limit)\n    result = await db.execute(stmt)\n    events = result.scalars().all()\n    \n    return {\n        \"items\": [\n            {\n                \"id\": str(e.id),\n                \"video_id\": str(e.video_id),\n                \"stage\": e.stage,\n                \"event_type\": e.event_type,\n                \"prompt_tokens\": e.prompt_tokens,\n                \"completion_tokens\": e.completion_tokens,\n                \"total_tokens\": e.total_tokens,\n                \"model\": e.model,\n                \"duration_ms\": e.duration_ms,\n                \"payload\": e.payload,\n                \"created_at\": e.created_at.isoformat() if e.created_at else None,\n                \"system_prompt_text\": e.system_prompt_text,\n                \"user_prompt_text\": e.user_prompt_text,\n                \"response_text\": e.response_text,\n            }\n            for e in events\n        ],\n        \"total\": total,\n        \"offset\": offset,\n        \"limit\": limit,\n    }\n\n\n# ── Admin: Debug mode ─────────────────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/debug-mode\", response_model=DebugModeResponse)\nasync def get_debug_mode() -> DebugModeResponse:\n    \"\"\"Get the current pipeline debug mode (on/off).\"\"\"\n    settings = get_settings()\n    try:\n        redis = await get_redis()\n        try:\n            value = await redis.get(REDIS_DEBUG_MODE_KEY)\n            if value is not None:\n                return DebugModeResponse(debug_mode=value.lower() == \"true\")\n        finally:\n            await redis.aclose()\n    except Exception as exc:\n        logger.warning(\"Redis unavailable for debug mode read, using config default: %s\", exc)\n\n    return DebugModeResponse(debug_mode=settings.debug_mode)\n\n\n@router.put(\"/admin/pipeline/debug-mode\", response_model=DebugModeResponse)\nasync def set_debug_mode(body: DebugModeUpdate) -> DebugModeResponse:\n    \"\"\"Set the pipeline debug mode (on/off).\"\"\"\n    try:\n        redis = await get_redis()\n        try:\n            await redis.set(REDIS_DEBUG_MODE_KEY, str(body.debug_mode))\n        finally:\n            await redis.aclose()\n    except Exception as exc:\n        logger.error(\"Failed to set debug mode in Redis: %s\", exc)\n        raise HTTPException(\n            status_code=503,\n            detail=f\"Redis unavailable: {exc}\",\n        )\n\n    logger.info(\"Pipeline debug mode set to %s\", body.debug_mode)\n    return DebugModeResponse(debug_mode=body.debug_mode)\n\n\n# ── Admin: Token summary ─────────────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/token-summary/{video_id}\", response_model=TokenSummaryResponse)\nasync def get_token_summary(\n    video_id: str,\n    db: AsyncSession = Depends(get_session),\n) -> TokenSummaryResponse:\n    \"\"\"Get per-stage token usage summary for a video.\"\"\"\n    stmt = (\n        select(\n            PipelineEvent.stage,\n            func.count().label(\"call_count\"),\n            func.coalesce(func.sum(PipelineEvent.prompt_tokens), 0).label(\"total_prompt_tokens\"),\n            func.coalesce(func.sum(PipelineEvent.completion_tokens), 0).label(\"total_completion_tokens\"),\n            func.coalesce(func.sum(PipelineEvent.total_tokens), 0).label(\"total_tokens\"),\n        )\n        .where(PipelineEvent.video_id == video_id)\n        .where(PipelineEvent.event_type == \"llm_call\")\n        .group_by(PipelineEvent.stage)\n        .order_by(PipelineEvent.stage)\n    )\n\n    result = await db.execute(stmt)\n    rows = result.all()\n\n    stages = [\n        TokenStageSummary(\n            stage=r.stage,\n            call_count=r.call_count,\n            total_prompt_tokens=r.total_prompt_tokens,\n            total_completion_tokens=r.total_completion_tokens,\n            total_tokens=r.total_tokens,\n        )\n        for r in rows\n    ]\n    grand_total = sum(s.total_tokens for s in stages)\n\n    return TokenSummaryResponse(\n        video_id=video_id,\n        stages=stages,\n        grand_total_tokens=grand_total,\n    )\n\n\n# ── Admin: Stale Pages ──────────────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/stale-pages\")\nasync def get_stale_pages(\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Detect technique pages synthesized with an older prompt than the current one.\n\n    Compares the SHA-256 hash of the current stage5_synthesis.txt against the\n    prompt hashes stored in TechniquePageVersion.pipeline_metadata.\n    \"\"\"\n    import hashlib\n    from pathlib import Path as _Path\n    from models import TechniquePage, TechniquePageVersion\n\n    settings = get_settings()\n    prompt_path = _Path(settings.prompts_path) / \"stage5_synthesis.txt\"\n\n    if not prompt_path.exists():\n        raise HTTPException(status_code=500, detail=\"stage5_synthesis.txt not found\")\n\n    current_hash = hashlib.sha256(\n        prompt_path.read_text(encoding=\"utf-8\").encode()\n    ).hexdigest()[:12]\n\n    # Get all technique pages\n    pages = (await db.execute(select(TechniquePage))).scalars().all()\n    total = len(pages)\n\n    stale_count = 0\n    fresh_count = 0\n    stale_by_creator: dict[str, dict] = {}\n\n    for page in pages:\n        # Get latest version to check prompt hash\n        latest_version = (\n            await db.execute(\n                select(TechniquePageVersion)\n                .where(TechniquePageVersion.technique_page_id == page.id)\n                .order_by(TechniquePageVersion.version_number.desc())\n                .limit(1)\n            )\n        ).scalar_one_or_none()\n\n        page_hash = None\n        if latest_version and latest_version.pipeline_metadata:\n            meta = latest_version.pipeline_metadata\n            page_hash = meta.get(\"prompt_hash\", meta.get(\"stage5_prompt_hash\"))\n\n        if page_hash == current_hash:\n            fresh_count += 1\n        else:\n            stale_count += 1\n            # Look up creator name\n            creator = (await db.execute(\n                select(Creator.name).where(Creator.id == page.creator_id)\n            )).scalar_one_or_none() or \"Unknown\"\n\n            if creator not in stale_by_creator:\n                stale_by_creator[creator] = {\"creator\": creator, \"stale_count\": 0, \"page_slugs\": []}\n            stale_by_creator[creator][\"stale_count\"] += 1\n            stale_by_creator[creator][\"page_slugs\"].append(page.slug)\n\n    return {\n        \"current_prompt_hash\": current_hash,\n        \"total_pages\": total,\n        \"stale_pages\": stale_count,\n        \"fresh_pages\": fresh_count,\n        \"stale_by_creator\": list(stale_by_creator.values()),\n    }\n\n\n# ── Admin: Bulk Re-Synthesize ───────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/bulk-resynthesize\")\nasync def bulk_resynthesize(\n    video_ids: list[str] | None = None,\n    stage: str = \"stage5_synthesis\",\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Re-run a single stage on multiple videos without full pipeline reset.\n\n    If video_ids is None, targets all videos with processing_status='complete'.\n    Rate-limited to avoid overwhelming the worker queue.\n    \"\"\"\n    from pipeline.stages import _PIPELINE_STAGES, run_single_stage\n\n    if stage not in _PIPELINE_STAGES:\n        raise HTTPException(status_code=400, detail=f\"Invalid stage: {stage}\")\n\n    if video_ids is None:\n        result = await db.execute(\n            select(SourceVideo.id).where(\n                SourceVideo.processing_status == ProcessingStatus.complete\n            )\n        )\n        video_ids = [str(row[0]) for row in result.all()]\n\n    if not video_ids:\n        return {\"status\": \"no_videos\", \"dispatched\": 0, \"skipped\": 0, \"total\": 0}\n\n    dispatched = 0\n    skipped = []\n    for vid in video_ids:\n        try:\n            run_single_stage.delay(vid, stage, trigger=\"stage_rerun\")\n            dispatched += 1\n        except Exception as exc:\n            logger.warning(\"Bulk re-synth dispatch failed for video_id=%s: %s\", vid, exc)\n            skipped.append({\"video_id\": vid, \"reason\": str(exc)})\n\n    logger.info(\n        \"[BULK-RESYNTH] Dispatched %d/%d %s re-runs\",\n        dispatched, len(video_ids), stage,\n    )\n\n    return {\n        \"status\": \"dispatched\",\n        \"stage\": stage,\n        \"total\": len(video_ids),\n        \"dispatched\": dispatched,\n        \"skipped\": skipped if skipped else None,\n    }\n\n\n# ── Admin: Wipe All Output ──────────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/wipe-all-output\")\nasync def wipe_all_output(\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Wipe ALL pipeline output while preserving raw input data.\n\n    Deletes: technique_page_versions, related_technique_links, technique_pages,\n    key_moments, pipeline_events, pipeline_runs, Qdrant vectors, Redis cache.\n    Resets: all video processing_status to 'not_started', classification_data to null.\n    Preserves: creators, source_videos (metadata), transcript_segments.\n    \"\"\"\n    from models import TechniquePageVersion, TechniquePage, RelatedTechniqueLink, PipelineRun\n\n    counts = {}\n\n    # Order matters due to FK constraints: versions → links → pages → moments → events → runs\n\n    # 1. Technique page versions\n    result = await db.execute(TechniquePageVersion.__table__.delete())\n    counts[\"technique_page_versions\"] = result.rowcount\n\n    # 2. Related technique links\n    result = await db.execute(RelatedTechniqueLink.__table__.delete())\n    counts[\"related_technique_links\"] = result.rowcount\n\n    # 3. Key moments (FK to technique_pages, must clear before pages)\n    result = await db.execute(KeyMoment.__table__.delete())\n    counts[\"key_moments\"] = result.rowcount\n\n    # 4. Technique pages\n    result = await db.execute(TechniquePage.__table__.delete())\n    counts[\"technique_pages\"] = result.rowcount\n\n    # 5. Pipeline events\n    result = await db.execute(PipelineEvent.__table__.delete())\n    counts[\"pipeline_events\"] = result.rowcount\n\n    # 6. Pipeline runs\n    result = await db.execute(PipelineRun.__table__.delete())\n    counts[\"pipeline_runs\"] = result.rowcount\n\n    # 7. Reset all video statuses and classification data\n    from sqlalchemy import update\n    result = await db.execute(\n        update(SourceVideo).values(\n            processing_status=ProcessingStatus.not_started,\n            classification_data=None,\n        )\n    )\n    counts[\"videos_reset\"] = result.rowcount\n\n    await db.commit()\n\n    # 8. Clear Qdrant vectors (best-effort)\n    try:\n        settings = get_settings()\n        from pipeline.qdrant_client import QdrantManager\n        qdrant = QdrantManager(settings)\n        qdrant.ensure_collection()\n        # Delete entire collection and recreate empty\n        from qdrant_client.models import Distance, VectorParams\n        qdrant.client.delete_collection(settings.qdrant_collection)\n        qdrant.client.create_collection(\n            collection_name=settings.qdrant_collection,\n            vectors_config=VectorParams(\n                size=settings.embedding_dimensions,\n                distance=Distance.COSINE,\n            ),\n        )\n        counts[\"qdrant\"] = \"collection_recreated\"\n    except Exception as exc:\n        logger.warning(\"Qdrant wipe failed: %s\", exc)\n        counts[\"qdrant\"] = f\"skipped: {exc}\"\n\n    # 9. Clear Redis pipeline cache keys (best-effort)\n    try:\n        import redis as redis_lib\n        settings = get_settings()\n        r = redis_lib.Redis.from_url(settings.redis_url)\n        cursor = 0\n        deleted_keys = 0\n        while True:\n            cursor, keys = r.scan(cursor, match=\"chrysopedia:*\", count=100)\n            if keys:\n                r.delete(*keys)\n                deleted_keys += len(keys)\n            if cursor == 0:\n                break\n        counts[\"redis_keys_deleted\"] = deleted_keys\n    except Exception as exc:\n        logger.warning(\"Redis wipe failed: %s\", exc)\n        counts[\"redis\"] = f\"skipped: {exc}\"\n\n    logger.info(\"[WIPE] All pipeline output wiped: %s\", counts)\n\n    return {\n        \"status\": \"wiped\",\n        \"deleted\": counts,\n    }\n\n\n# ── Admin: Prompt Optimization ──────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/optimize-prompt/{stage}\")\nasync def trigger_optimization(\n    stage: int,\n    video_id: str = Query(..., description=\"Video UUID to use as fixture source\"),\n    iterations: int = Query(5, description=\"Number of optimization iterations\"),\n    variants_per_iter: int = Query(2, description=\"Variants per iteration\"),\n):\n    \"\"\"Trigger an automated prompt optimization run as a background Celery task.\n\n    Exports a fixture from the specified video, then runs the optimization loop\n    with generate-score-select cycles. Progress is tracked in\n    quality/results/progress_stage{N}.json.\n    \"\"\"\n    from pipeline.quality.scorer import STAGE_CONFIGS\n\n    if stage not in STAGE_CONFIGS:\n        raise HTTPException(status_code=400, detail=f\"Unsupported stage {stage}. Valid: {sorted(STAGE_CONFIGS)}\")\n\n    # Dispatch as a Celery task\n    from worker import celery_app\n\n    @celery_app.task\n    def _run_optimization(video_id: str, stage: int, iterations: int, variants_per_iter: int) -> dict:\n        \"\"\"Background optimization task.\"\"\"\n        import tempfile\n        from pipeline.export_fixture import export_fixture\n        from pipeline.quality.optimizer import OptimizationLoop\n        from pipeline.quality.__main__ import write_results_json\n        from config import get_settings as _get_settings\n        from pipeline.llm_client import LLMClient as _LLMClient\n\n        settings = _get_settings()\n\n        # Export fixture\n        tmp = tempfile.NamedTemporaryFile(suffix=\".json\", prefix=\"opt_fixture_\", delete=False)\n        tmp.close()\n        exit_code = export_fixture(settings.database_url, settings.redis_url, video_id, tmp.name)\n        if exit_code != 0:\n            return {\"error\": f\"Fixture export failed (exit code {exit_code})\"}\n\n        client = _LLMClient(settings)\n        loop = OptimizationLoop(\n            client=client,\n            stage=stage,\n            fixture_path=tmp.name,\n            iterations=iterations,\n            variants_per_iter=variants_per_iter,\n            output_dir=\"backend/pipeline/quality/results/\",\n        )\n        result = loop.run()\n\n        json_path = write_results_json(\n            result=result, output_dir=\"backend/pipeline/quality/results/\",\n            stage=stage, iterations=iterations, variants_per_iter=variants_per_iter,\n            fixture_path=tmp.name,\n        )\n        return {\n            \"best_score\": result.best_score.composite,\n            \"results_path\": json_path,\n            \"iterations\": iterations,\n        }\n\n    try:\n        task = _run_optimization.delay(video_id, stage, iterations, variants_per_iter)\n        logger.info(\n            \"[OPTIMIZE] Dispatched optimization: stage=%d, video_id=%s, iterations=%d, task_id=%s\",\n            stage, video_id, iterations, task.id,\n        )\n    except Exception as exc:\n        raise HTTPException(status_code=503, detail=f\"Failed to dispatch optimization: {exc}\") from exc\n\n    return {\n        \"status\": \"dispatched\",\n        \"task_id\": task.id,\n        \"stage\": stage,\n        \"video_id\": video_id,\n        \"iterations\": iterations,\n        \"variants_per_iter\": variants_per_iter,\n    }\n\n\n# ── Admin: Reindex All ─────────────────────────────────────────────────────\n\n@router.post(\"/admin/pipeline/reindex-all\")\nasync def reindex_all(\n    db: AsyncSession = Depends(get_session),\n):\n    \"\"\"Re-run stage 6 (embed & index) for all videos with processing_status='complete'.\n\n    Use after changing embedding text composition or Qdrant payload fields\n    to regenerate all vectors and payloads without re-running the full pipeline.\n    \"\"\"\n    stmt = select(SourceVideo.id).where(\n        SourceVideo.processing_status == ProcessingStatus.complete\n    )\n    result = await db.execute(stmt)\n    video_ids = [str(row[0]) for row in result.all()]\n\n    if not video_ids:\n        return {\n            \"status\": \"no_videos\",\n            \"message\": \"No videos with processing_status='complete' found.\",\n            \"dispatched\": 0,\n        }\n\n    from pipeline.stages import stage6_embed_and_index\n\n    dispatched = 0\n    errors = []\n    for vid in video_ids:\n        try:\n            stage6_embed_and_index.delay(vid)\n            dispatched += 1\n        except Exception as exc:\n            logger.warning(\"Failed to dispatch reindex for video_id=%s: %s\", vid, exc)\n            errors.append({\"video_id\": vid, \"error\": str(exc)})\n\n    logger.info(\n        \"Reindex-all dispatched %d/%d stage6 tasks.\",\n        dispatched, len(video_ids),\n    )\n\n    return {\n        \"status\": \"dispatched\",\n        \"dispatched\": dispatched,\n        \"total_complete_videos\": len(video_ids),\n        \"errors\": errors if errors else None,\n    }\n\n\n# ── Admin: Worker status ─────────────────────────────────────────────────────\n\n@router.get(\"/admin/pipeline/worker-status\")\nasync def worker_status():\n    \"\"\"Get current Celery worker status — active, reserved, and stats.\"\"\"\n    from worker import celery_app\n    \n    try:\n        inspector = celery_app.control.inspect()\n        active = inspector.active() or {}\n        reserved = inspector.reserved() or {}\n        stats = inspector.stats() or {}\n        \n        workers = []\n        for worker_name in set(list(active.keys()) + list(reserved.keys()) + list(stats.keys())):\n            worker_active = active.get(worker_name, [])\n            worker_reserved = reserved.get(worker_name, [])\n            worker_stats = stats.get(worker_name, {})\n            \n            workers.append({\n                \"name\": worker_name,\n                \"active_tasks\": [\n                    {\n                        \"id\": t.get(\"id\"),\n                        \"name\": t.get(\"name\"),\n                        \"args\": t.get(\"args\", []),\n                        \"time_start\": t.get(\"time_start\"),\n                    }\n                    for t in worker_active\n                ],\n                \"reserved_tasks\": len(worker_reserved),\n                \"total_completed\": worker_stats.get(\"total\", {}).get(\"tasks.pipeline.stages.stage2_segmentation\", 0)\n                    + worker_stats.get(\"total\", {}).get(\"tasks.pipeline.stages.stage3_extraction\", 0)\n                    + worker_stats.get(\"total\", {}).get(\"tasks.pipeline.stages.stage4_classification\", 0)\n                    + worker_stats.get(\"total\", {}).get(\"tasks.pipeline.stages.stage5_synthesis\", 0),\n                \"uptime\": worker_stats.get(\"clock\", None),\n                \"pool_size\": worker_stats.get(\"pool\", {}).get(\"max-concurrency\") if isinstance(worker_stats.get(\"pool\"), dict) else None,\n            })\n        \n        return {\n            \"online\": len(workers) > 0,\n            \"workers\": workers,\n        }\n    except Exception as exc:\n        logger.warning(\"Failed to inspect Celery workers: %s\", exc)\n        return {\n            \"online\": False,\n            \"workers\": [],\n            \"error\": str(exc),\n        }\n"}],"isError":false,"timestamp":1775180883580}}
{"type":"message","id":"d02307d0","parentId":"dbfbb8f8","timestamp":"2026-04-03T01:48:03.593Z","message":{"role":"toolResult","toolCallId":"toolu_013n4VAyKF4KWhYoAN8tW7oa","toolName":"read","content":[{"type":"text","text":"/**\n * Pipeline admin dashboard — video list with status, retrigger/revoke,\n * expandable event log with token usage and collapsible JSON viewer.\n */\n\nimport React, { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport { useSearchParams } from \"react-router-dom\";\nimport { useDocumentTitle } from \"../hooks/useDocumentTitle\";\nimport {\n  fetchPipelineVideos,\n  fetchPipelineEvents,\n  fetchPipelineRuns,\n  fetchWorkerStatus,\n  fetchDebugMode,\n  setDebugMode,\n  triggerPipeline,\n  revokePipeline,\n  cleanRetriggerPipeline,\n  rerunStage,\n  fetchChunkingData,\n  fetchStalePages,\n  bulkResynthesize,\n  wipeAllOutput,\n  fetchCreators,\n  fetchRecentActivity,\n  type PipelineVideoItem,\n  type PipelineEvent,\n  type PipelineRunItem,\n  type WorkerStatusResponse,\n  type RecentActivityItem,\n} from \"../api/public-client\";\n\n// ── Helpers ──────────────────────────────────────────────────────────────────\n\nfunction formatDate(iso: string | null): string {\n  if (!iso) return \"—\";\n  return new Date(iso).toLocaleString(undefined, {\n    month: \"short\",\n    day: \"numeric\",\n    hour: \"2-digit\",\n    minute: \"2-digit\",\n    second: \"2-digit\",\n  });\n}\n\nfunction formatTokens(n: number): string {\n  if (n === 0) return \"0\";\n  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;\n  if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;\n  return String(n);\n}\n\n\nfunction formatElapsed(isoStart: string | null): string {\n  if (!isoStart) return \"\";\n  const ms = Date.now() - new Date(isoStart).getTime();\n  if (ms < 0) return \"\";\n  const secs = Math.floor(ms / 1000);\n  if (secs < 60) return `${secs}s`;\n  const mins = Math.floor(secs / 60);\n  const remainSecs = secs % 60;\n  if (mins < 60) return `${mins}m ${remainSecs}s`;\n  const hrs = Math.floor(mins / 60);\n  return `${hrs}h ${mins % 60}m`;\n}\nconst STATUS_LABELS: Record<string, string> = {\n  not_started: \"Not Started\",\n  queued: \"Queued\",\n  processing: \"In Progress\",\n  error: \"Errored\",\n  complete: \"Complete\",\n};\n\nfunction statusBadgeClass(status: string): string {\n  switch (status) {\n    case \"complete\":\n      return \"pipeline-badge--success\";\n    case \"processing\":\n      return \"pipeline-badge--active\";\n    case \"error\":\n      return \"pipeline-badge--error\";\n    case \"not_started\":\n    case \"queued\":\n      return \"pipeline-badge--pending\";\n    default:\n      return \"\";\n  }\n}\n\nfunction eventTypeIcon(eventType: string): string {\n  switch (eventType) {\n    case \"start\":\n      return \"▶\";\n    case \"complete\":\n      return \"✓\";\n    case \"error\":\n      return \"✗\";\n    case \"llm_call\":\n      return \"⚙\";\n    default:\n      return \"·\";\n  }\n}\n\n// ── Collapsible JSON ─────────────────────────────────────────────────────────\n\nfunction JsonViewer({ data }: { data: Record<string, unknown> | null }) {\n  const [open, setOpen] = useState(false);\n  if (!data || Object.keys(data).length === 0) return null;\n\n  return (\n    <div className=\"json-viewer\">\n      <button\n        className=\"json-viewer__toggle\"\n        onClick={() => setOpen((v) => !v)}\n        aria-expanded={open}\n        title=\"Raw JSON event data logged by the pipeline stage\"\n      >\n        {open ? \"▾ Hide payload\" : \"▸ Show payload\"}\n      </button>\n      {open && (\n        <pre className=\"json-viewer__content\">\n          {JSON.stringify(data, null, 2)}\n        </pre>\n      )}\n    </div>\n  );\n}\n\n// ── Debug Payload Viewer ─────────────────────────────────────────────────────\n\ninterface DebugSection {\n  label: string;\n  content: string;\n}\n\nfunction DebugPayloadViewer({ event }: { event: PipelineEvent }) {\n  const sections: DebugSection[] = [];\n  if (event.payload?.request_params) sections.push({ label: \"Request Params\", content: JSON.stringify(event.payload.request_params, null, 2) });\n  if (event.system_prompt_text) sections.push({ label: \"System Prompt\", content: event.system_prompt_text });\n  if (event.user_prompt_text) sections.push({ label: \"User Prompt\", content: event.user_prompt_text });\n  if (event.response_text) sections.push({ label: \"Response\", content: event.response_text });\n\n  const [openSections, setOpenSections] = useState<Record<string, boolean>>({});\n  const [copiedKey, setCopiedKey] = useState<string | null>(null);\n\n  const SECTION_TOOLTIPS: Record<string, string> = {\n    \"System Prompt\": \"Instructions sent to the LLM defining its role and output format for this pipeline stage\",\n    \"User Prompt\": \"The actual transcript content and extraction request sent to the LLM\",\n    \"Response\": \"Raw LLM output before parsing — the extracted data that feeds the next stage\",\n  };\n\n  if (sections.length === 0) return null;\n\n  const toggleSection = (label: string) => {\n    setOpenSections((prev) => ({ ...prev, [label]: !prev[label] }));\n  };\n\n  const copyToClipboard = async (label: string, text: string) => {\n    try {\n      await navigator.clipboard.writeText(text);\n      setCopiedKey(label);\n      setTimeout(() => setCopiedKey(null), 1500);\n    } catch {\n      // Fallback for non-HTTPS contexts\n      const ta = document.createElement(\"textarea\");\n      ta.value = text;\n      ta.style.position = \"fixed\";\n      ta.style.opacity = \"0\";\n      document.body.appendChild(ta);\n      ta.select();\n      document.execCommand(\"copy\");\n      document.body.removeChild(ta);\n      setCopiedKey(label);\n      setTimeout(() => setCopiedKey(null), 1500);\n    }\n  };\n\n  const exportAsJson = () => {\n    // Dump everything — full rawdog debug payload with request params\n    const data: Record<string, unknown> = {\n      event_id: event.id,\n      stage: event.stage,\n      event_type: event.event_type,\n      model: event.model,\n      prompt_tokens: event.prompt_tokens,\n      completion_tokens: event.completion_tokens,\n      total_tokens: event.total_tokens,\n      duration_ms: event.duration_ms,\n      created_at: event.created_at,\n      payload: event.payload,\n      request_params: event.payload?.request_params ?? null,\n      system_prompt_text: event.system_prompt_text,\n      user_prompt_text: event.user_prompt_text,\n      response_text: event.response_text,\n    };\n    const blob = new Blob([JSON.stringify(data, null, 2)], { type: \"application/json\" });\n    const url = URL.createObjectURL(blob);\n    const a = document.createElement(\"a\");\n    a.href = url;\n    a.download = `debug-${event.stage}-${event.id.slice(0, 8)}.json`;\n    a.click();\n    URL.revokeObjectURL(url);\n  };\n\n  return (\n    <div className=\"debug-viewer\">\n      <div className=\"debug-viewer__header\">\n        <span className=\"debug-viewer__label\">LLM Debug</span>\n        <button className=\"debug-viewer__export\" onClick={exportAsJson} title=\"Export debug payload as JSON\">\n          ↓ JSON\n        </button>\n      </div>\n      {sections.map((sec) => {\n        const isOpen = !!openSections[sec.label];\n        return (\n          <div key={sec.label} className=\"debug-viewer__section\">\n            <div className=\"debug-viewer__section-header\">\n              <button\n                className=\"debug-viewer__section-toggle\"\n                onClick={() => toggleSection(sec.label)}\n                aria-expanded={isOpen}\n                title={SECTION_TOOLTIPS[sec.label] ?? \"\"}\n              >\n                {isOpen ? \"▾\" : \"▸\"} {sec.label}\n              </button>\n              <button\n                className=\"debug-viewer__copy\"\n                onClick={() => void copyToClipboard(sec.label, sec.content)}\n                title={`Copy ${sec.label}`}\n              >\n                {copiedKey === sec.label ? \"✓ Copied\" : \"Copy\"}\n              </button>\n            </div>\n            {isOpen && (\n              <pre className=\"debug-viewer__content\">{sec.content}</pre>\n            )}\n          </div>\n        );\n      })}\n    </div>\n  );\n}\n\n// ── Event Log ────────────────────────────────────────────────────────────────\n\nfunction EventLog({ videoId, status, runId }: { videoId: string; status: string; runId?: string }) {\n  const [events, setEvents] = useState<PipelineEvent[]>([]);\n  const [total, setTotal] = useState(0);\n  const [loading, setLoading] = useState(true);\n  const [error, setError] = useState<string | null>(null);\n  const [offset, setOffset] = useState(0);\n  const [viewMode, setViewMode] = useState<\"head\" | \"tail\">(\"tail\");\n  const limit = 50;\n  const initialLoaded = useRef(false);\n\n  const load = useCallback(async (silent = false) => {\n    if (!silent) setLoading(true);\n    setError(null);\n    try {\n      const res = await fetchPipelineEvents(videoId, {\n        offset,\n        limit,\n        order: viewMode === \"head\" ? \"asc\" : \"desc\",\n        run_id: runId,\n      });\n      setEvents(res.items);\n      setTotal(res.total);\n      initialLoaded.current = true;\n    } catch (err) {\n      if (!silent) setError(err instanceof Error ? err.message : \"Failed to load events\");\n    } finally {\n      if (!silent) setLoading(false);\n    }\n  }, [videoId, offset, viewMode]);\n\n  useEffect(() => {\n    void load();\n  }, [load]);\n\n  // Auto-refresh when video is actively processing\n  useEffect(() => {\n    if (status !== \"processing\" && status !== \"queued\") return;\n    const id = setInterval(() => void load(true), 10_000);\n    return () => clearInterval(id);\n  }, [status, load]);\n\n  if (loading) return <div className=\"loading\">Loading events…</div>;\n  if (error) return <div className=\"loading error-text\">Error: {error}</div>;\n  if (events.length === 0) return <div className=\"pipeline-events__empty\">No events recorded.</div>;\n\n  const hasNext = offset + limit < total;\n  const hasPrev = offset > 0;\n\n  return (\n    <div className=\"pipeline-events\">\n      <div className=\"pipeline-events__header\">\n        <span className=\"pipeline-events__count\">{total} event{total !== 1 ? \"s\" : \"\"}</span>\n        <div className=\"pipeline-events__view-toggle\">\n          <button\n            className={`pipeline-events__view-btn${viewMode === \"head\" ? \" pipeline-events__view-btn--active\" : \"\"}`}\n            onClick={() => { setViewMode(\"head\"); setOffset(0); }}\n          >\n            Oldest first\n          </button>\n          <button\n            className={`pipeline-events__view-btn${viewMode === \"tail\" ? \" pipeline-events__view-btn--active\" : \"\"}`}\n            onClick={() => { setViewMode(\"tail\"); setOffset(0); }}\n          >\n            Newest first\n          </button>\n        </div>\n      </div>\n\n      <div className=\"pipeline-events__list\">\n        {events.map((evt) => (\n          <div key={evt.id} className={`pipeline-event pipeline-event--${evt.event_type}`}>\n            <div className=\"pipeline-event__row\">\n              <span className=\"pipeline-event__icon\">{eventTypeIcon(evt.event_type)}</span>\n              <span className=\"pipeline-event__stage\">{evt.stage}</span>\n              <span className={`pipeline-badge pipeline-badge--event-${evt.event_type}`}>\n                {evt.event_type}\n              </span>\n              {typeof evt.payload?.context === \"string\" && (\n                <span className=\"pipeline-event__context\" title=\"Processing context\">\n                  {evt.payload.context}\n                </span>\n              )}\n              {evt.model && <span className=\"pipeline-event__model\">{evt.model}</span>}\n              {evt.total_tokens != null && evt.total_tokens > 0 && (\n                <span className=\"pipeline-event__tokens\" title={`prompt: ${evt.prompt_tokens ?? 0} / completion: ${evt.completion_tokens ?? 0}`}>\n                  {formatTokens(evt.total_tokens)} tok\n                </span>\n              )}\n              {evt.duration_ms != null && (\n                <span className=\"pipeline-event__duration\">{evt.duration_ms}ms</span>\n              )}\n              <span className=\"pipeline-event__time\">{formatDate(evt.created_at)}</span>\n            </div>\n            <JsonViewer data={evt.payload} />\n            <DebugPayloadViewer event={evt} />\n          </div>\n        ))}\n      </div>\n\n      {(hasPrev || hasNext) && (\n        <div className=\"pipeline-events__pager\">\n          <button\n            className=\"btn btn--small btn--secondary\"\n            disabled={!hasPrev}\n            onClick={() => setOffset((o) => Math.max(0, o - limit))}\n          >\n            ← Prev\n          </button>\n          <span className=\"pipeline-events__pager-info\">\n            {offset + 1}–{Math.min(offset + limit, total)} of {total}\n          </span>\n          <button\n            className=\"btn btn--small btn--secondary\"\n            disabled={!hasNext}\n            onClick={() => setOffset((o) => o + limit)}\n          >\n            Next →\n          </button>\n        </div>\n      )}\n    </div>\n  );\n}\n\n// ── Worker Status ────────────────────────────────────────────────────────────\n\nconst WorkerStatus = React.memo(function WorkerStatus() {\n  const [status, setStatus] = useState<WorkerStatusResponse | null>(null);\n  const [error, setError] = useState<string | null>(null);\n\n  const load = useCallback(async () => {\n    try {\n      setError(null);\n      const res = await fetchWorkerStatus();\n      setStatus(res);\n    } catch (err) {\n      setError(err instanceof Error ? err.message : \"Failed\");\n    }\n  }, []);\n\n  useEffect(() => {\n    void load();\n    const id = setInterval(() => void load(), 15_000);\n    return () => clearInterval(id);\n  }, [load]);\n\n  if (error) {\n    return (\n      <div className=\"worker-status worker-status--error\">\n        <span className=\"worker-status__dot worker-status__dot--offline\" />\n        Worker: error ({error})\n      </div>\n    );\n  }\n\n  if (!status) {\n    return (\n      <div className=\"worker-status\">\n        <span className=\"worker-status__dot worker-status__dot--unknown\" />\n        Worker: checking…\n      </div>\n    );\n  }\n\n  return (\n    <div className={`worker-status ${status.online ? \"worker-status--online\" : \"worker-status--offline\"}`}>\n      <span className={`worker-status__dot ${status.online ? \"worker-status__dot--online\" : \"worker-status__dot--offline\"}`} />\n      <span className=\"worker-status__label\">\n        {status.online ? `${status.workers.length} worker${status.workers.length !== 1 ? \"s\" : \"\"} online` : \"Workers offline\"}\n      </span>\n      {status.workers.map((w) => (\n        <span key={w.name} className=\"worker-status__detail\" title={w.name}>\n          {w.active_tasks.length > 0\n            ? `${w.active_tasks.length} active`\n            : \"idle\"}\n          {w.pool_size != null && ` · pool ${w.pool_size}`}\n        </span>\n      ))}\n    </div>\n  );\n});\n\n// ── Debug Mode Toggle ────────────────────────────────────────────────────────\n\nfunction DebugModeToggle({\n  debugMode,\n  onDebugModeChange,\n}: {\n  debugMode: boolean | null;\n  onDebugModeChange: (mode: boolean) => void;\n}) {\n  const [debugLoading, setDebugLoading] = useState(false);\n\n  async function handleToggle() {\n    if (debugMode === null || debugLoading) return;\n    setDebugLoading(true);\n    try {\n      const res = await setDebugMode(!debugMode);\n      onDebugModeChange(res.debug_mode);\n    } catch {\n      // swallow — leave previous state\n    } finally {\n      setDebugLoading(false);\n    }\n  }\n\n  if (debugMode === null) return null;\n\n  return (\n    <div className=\"debug-toggle\" title=\"When enabled, pipeline runs capture full LLM prompts and responses for inspection in the event log\">\n      <span className=\"debug-toggle__label\">\n        Debug {debugMode ? \"On\" : \"Off\"}\n      </span>\n      <button\n        type=\"button\"\n        className={`debug-toggle__switch ${debugMode ? \"debug-toggle__switch--active\" : \"\"}`}\n        onClick={handleToggle}\n        disabled={debugLoading}\n        aria-label={`Turn debug mode ${debugMode ? \"off\" : \"on\"}`}\n      />\n    </div>\n  );\n}\n\n// ── Status Filter ────────────────────────────────────────────────────────────\n\nconst StatusFilter = React.memo(function StatusFilter({\n  videos,\n  activeFilter,\n  onFilterChange,\n}: {\n  videos: PipelineVideoItem[];\n  activeFilter: string | null;\n  onFilterChange: (filter: string | null) => void;\n}) {\n  // Fixed display order for pipeline lifecycle\n  const STATUS_ORDER = [\"not_started\", \"queued\", \"processing\", \"error\", \"complete\"];\n  const present = new Set(videos.map((v) => v.processing_status));\n  const ordered = STATUS_ORDER.filter((s) => present.has(s));\n\n  if (ordered.length <= 1) return null;\n\n  return (\n    <div className=\"filter-tabs\">\n      <button\n        type=\"button\"\n        className={`filter-tab ${activeFilter === null ? \"filter-tab--active\" : \"\"}`}\n        onClick={() => onFilterChange(null)}\n      >\n        All ({videos.length})\n      </button>\n      {ordered.map((status) => {\n        const count = videos.filter((v) => v.processing_status === status).length;\n        return (\n          <button\n            key={status}\n            type=\"button\"\n            className={`filter-tab ${activeFilter === status ? \"filter-tab--active\" : \"\"}`}\n            onClick={() => onFilterChange(status)}\n          >\n            {STATUS_LABELS[status] ?? status} ({count})\n          </button>\n        );\n      })}\n    </div>\n  );\n});\n\n// ── Run List ─────────────────────────────────────────────────────────────────\n\nconst TRIGGER_LABELS: Record<string, string> = {\n  manual: \"Manual\",\n  clean_reprocess: \"Clean Reprocess\",\n  auto_ingest: \"Auto Ingest\",\n  bulk: \"Bulk\",\n};\n\nconst RUN_STATUS_CLASS: Record<string, string> = {\n  running: \"pipeline-badge--active\",\n  complete: \"pipeline-badge--success\",\n  error: \"pipeline-badge--error\",\n  cancelled: \"pipeline-badge--pending\",\n};\n\nfunction RunList({ videoId, videoStatus }: { videoId: string; videoStatus: string }) {\n  const [runs, setRuns] = useState<PipelineRunItem[]>([]);\n  const [legacyCount, setLegacyCount] = useState(0);\n  const [loading, setLoading] = useState(true);\n  const [expandedRunId, setExpandedRunId] = useState<string | null>(null);\n  const [showLegacy, setShowLegacy] = useState(false);\n\n  const load = useCallback(async (silent = false) => {\n    if (!silent) setLoading(true);\n    try {\n      const res = await fetchPipelineRuns(videoId);\n      setRuns(res.items);\n      setLegacyCount(res.legacy_event_count);\n      // Auto-expand the latest run on first load\n      if (!silent && res.items.length > 0 && expandedRunId === null) {\n        const firstRun = res.items[0];\n        if (firstRun) setExpandedRunId(firstRun.id);\n      }\n    } catch {\n      // silently fail\n    } finally {\n      if (!silent) setLoading(false);\n    }\n  }, [videoId, expandedRunId]);\n\n  useEffect(() => {\n    void load();\n  }, [load]);\n\n  // Auto-refresh when video is processing\n  useEffect(() => {\n    if (videoStatus !== \"processing\" && videoStatus !== \"queued\") return;\n    const id = setInterval(() => void load(true), 10_000);\n    return () => clearInterval(id);\n  }, [videoStatus, load]);\n\n  if (loading) return <div className=\"loading\">Loading runs…</div>;\n\n  if (runs.length === 0 && legacyCount === 0) {\n    return <div className=\"pipeline-events__empty\">No pipeline runs recorded.</div>;\n  }\n\n  return (\n    <div className=\"run-list\">\n      {runs.map((run) => {\n        const isExpanded = expandedRunId === run.id;\n        return (\n          <div key={run.id} className={`run-card run-card--${run.status}`}>\n            <button\n              className=\"run-card__header\"\n              onClick={() => setExpandedRunId(isExpanded ? null : run.id)}\n              aria-expanded={isExpanded}\n            >\n              <span className=\"run-card__arrow\">{isExpanded ? \"▾\" : \"▸\"}</span>\n              <span className=\"run-card__number\">Run #{run.run_number}</span>\n              <span className=\"run-card__trigger\">{TRIGGER_LABELS[run.trigger] ?? run.trigger}</span>\n              <span className={`pipeline-badge ${RUN_STATUS_CLASS[run.status] ?? \"\"}`}>\n                {run.status}\n              </span>\n              <span className=\"run-card__time\">{formatDate(run.started_at)}</span>\n              {run.finished_at && (\n                <span className=\"run-card__duration\" title={`Finished: ${formatDate(run.finished_at)}`}>\n                  → {formatDate(run.finished_at)}\n                </span>\n              )}\n              <span className=\"run-card__tokens\">{formatTokens(run.total_tokens)} tokens</span>\n              <span className=\"run-card__events\">{run.event_count} events</span>\n              {run.error_stage && (\n                <span className=\"run-card__error-stage\">Failed at: {run.error_stage}</span>\n              )}\n            </button>\n            {isExpanded && (\n              <div className=\"run-card__body\">\n                <EventLog videoId={videoId} status={run.status} runId={run.id} />\n              </div>\n            )}\n          </div>\n        );\n      })}\n\n      {legacyCount > 0 && (\n        <div className=\"run-card run-card--legacy\">\n          <button\n            className=\"run-card__header\"\n            onClick={() => setShowLegacy((v) => !v)}\n            aria-expanded={showLegacy}\n          >\n            <span className=\"run-card__arrow\">{showLegacy ? \"▾\" : \"▸\"}</span>\n            <span className=\"run-card__number\">Legacy</span>\n            <span className=\"run-card__trigger\">Pre-run tracking</span>\n            <span className=\"run-card__events\">{legacyCount} events</span>\n          </button>\n          {showLegacy && (\n            <div className=\"run-card__body\">\n              <EventLog videoId={videoId} status=\"complete\" />\n            </div>\n          )}\n        </div>\n      )}\n    </div>\n  );\n}\n\n// ── Stage Timeline ───────────────────────────────────────────────────────────\n\nconst PIPELINE_STAGES = [\n  { key: \"stage2_segmentation\", label: \"Segment\" },\n  { key: \"stage3_extraction\", label: \"Extract\" },\n  { key: \"stage4_classification\", label: \"Classify\" },\n  { key: \"stage5_synthesis\", label: \"Synthesize\" },\n  { key: \"stage6_embed\", label: \"Embed\" },\n];\n\nconst StageTimeline = React.memo(function StageTimeline({ video }: { video: PipelineVideoItem }) {\n  const [, setTick] = useState(0);\n  const isProcessing = video.processing_status === \"processing\";\n\n  // Tick every second while processing to update elapsed time\n  useEffect(() => {\n    if (!isProcessing) return;\n    const id = setInterval(() => setTick((t) => t + 1), 1000);\n    return () => clearInterval(id);\n  }, [isProcessing]);\n\n  if (!isProcessing && video.processing_status !== \"complete\" && video.processing_status !== \"error\") {\n    return null;\n  }\n\n  const activeStage = video.active_stage;\n  const activeStatus = video.active_stage_status;\n  const activeIdx = PIPELINE_STAGES.findIndex((s) => s.key === activeStage);\n\n  return (\n    <div className=\"stage-timeline\">\n      {PIPELINE_STAGES.map((stage, i) => {\n        let stateClass = \"stage-timeline__step--future\";\n        if (activeIdx >= 0) {\n          if (i < activeIdx) {\n            stateClass = \"stage-timeline__step--done\";\n          } else if (i === activeIdx) {\n            if (activeStatus === \"start\") stateClass = \"stage-timeline__step--active\";\n            else if (activeStatus === \"complete\") stateClass = \"stage-timeline__step--done\";\n            else if (activeStatus === \"error\") stateClass = \"stage-timeline__step--error\";\n          }\n        }\n        if (video.processing_status === \"complete\") {\n          stateClass = \"stage-timeline__step--done\";\n        }\n\n        return (\n          <div key={stage.key} className={`stage-timeline__step ${stateClass}`}>\n            <div className=\"stage-timeline__dot\" title={stage.label} />\n            <span className=\"stage-timeline__label\">{stage.label}</span>\n          </div>\n        );\n      })}\n      {isProcessing && activeStatus === \"start\" && (\n        <span className=\"stage-timeline__elapsed\">{formatElapsed(video.stage_started_at)}</span>\n      )}\n      {isProcessing && video.latest_run && video.latest_run.total_tokens > 0 && (\n        <span className=\"stage-timeline__tokens\">{formatTokens(video.latest_run.total_tokens)} tok</span>\n      )}\n    </div>\n  );\n})\n\n// ── Recent Activity Feed ─────────────────────────────────────────────────────\n\nconst RecentActivityFeed = React.memo(function RecentActivityFeed() {\n  const [items, setItems] = useState<RecentActivityItem[]>([]);\n  const [collapsed, setCollapsed] = useState(false);\n  const [loading, setLoading] = useState(true);\n\n  const load = useCallback(async () => {\n    try {\n      const res = await fetchRecentActivity(8);\n      setItems(res.items);\n    } catch {\n      // silently fail\n    } finally {\n      setLoading(false);\n    }\n  }, []);\n\n  useEffect(() => {\n    void load();\n    const id = setInterval(() => void load(), 15_000);\n    return () => clearInterval(id);\n  }, [load]);\n\n  if (loading || items.length === 0) return null;\n\n  return (\n    <div className=\"recent-activity\">\n      <button\n        className=\"recent-activity__toggle\"\n        onClick={() => setCollapsed((v) => !v)}\n        aria-expanded={!collapsed}\n      >\n        <span className=\"recent-activity__title\">Recent Activity</span>\n        <span className=\"recent-activity__arrow\">{collapsed ? \"▸\" : \"▾\"}</span>\n      </button>\n      {!collapsed && (\n        <div className=\"recent-activity__list\">\n          {items.map((item) => (\n            <div key={item.id} className={`recent-activity__item recent-activity__item--${item.event_type}`}>\n              <span className=\"recent-activity__icon\">\n                {item.event_type === \"complete\" ? \"✓\" : \"✗\"}\n              </span>\n              <span className=\"recent-activity__stage\">{item.stage.replace(\"stage\", \"S\").replace(\"_\", \" \")}</span>\n              <span className=\"recent-activity__file\" title={item.filename}>\n                {item.filename}\n              </span>\n              <span className=\"recent-activity__creator\">{item.creator_name}</span>\n              {item.duration_ms != null && (\n                <span className=\"recent-activity__duration\">\n                  {item.duration_ms > 60000\n                    ? `${(item.duration_ms / 60000).toFixed(1)}m`\n                    : item.duration_ms > 1000\n                      ? `${(item.duration_ms / 1000).toFixed(1)}s`\n                      : `${item.duration_ms}ms`}\n                </span>\n              )}\n              <span className=\"recent-activity__time\">{formatDate(item.created_at)}</span>\n            </div>\n          ))}\n        </div>\n      )}\n    </div>\n  );\n});\n\n// ── Main Page ────────────────────────────────────────────────────────────────\n\ninterface BulkProgress {\n  total: number;\n  completed: number;\n  failed: number;\n  current: string | null;\n  active: boolean;\n}\n\ninterface BulkLogEntry {\n  filename: string;\n  ok: boolean;\n  message: string;\n}\n\n// ── Chunking Inspector ─────────────────────────────────────────────────────\n\nfunction ChunkingInspector({ videoId }: { videoId: string }) {\n  const [data, setData] = useState<Awaited<ReturnType<typeof fetchChunkingData>> | null>(null);\n  const [loading, setLoading] = useState(true);\n  const [error, setError] = useState<string | null>(null);\n  const [expanded, setExpanded] = useState(false);\n\n  useEffect(() => {\n    if (!expanded) return;\n    let cancelled = false;\n    setLoading(true);\n    fetchChunkingData(videoId)\n      .then((res) => { if (!cancelled) { setData(res); setError(null); } })\n      .catch((err) => { if (!cancelled) setError(err instanceof Error ? err.message : \"Failed\"); })\n      .finally(() => { if (!cancelled) setLoading(false); });\n    return () => { cancelled = true; };\n  }, [videoId, expanded]);\n\n  return (\n    <div className=\"chunking-inspector\">\n      <button\n        className=\"chunking-inspector__toggle\"\n        onClick={() => setExpanded(!expanded)}\n      >\n        {expanded ? \"▾\" : \"▸\"} Chunking Inspector\n      </button>\n\n      {expanded && loading && <div className=\"chunking-inspector__loading\">Loading chunking data...</div>}\n      {expanded && error && <div className=\"chunking-inspector__error\">{error}</div>}\n\n      {expanded && data && (\n        <div className=\"chunking-inspector__body\">\n          <div className=\"chunking-inspector__summary\">\n            <span>{data.total_segments} segments</span>\n            <span>{data.total_moments} moments</span>\n            <span>Classification: {data.classification_source}</span>\n            <span>Chunk size: {data.synthesis_chunk_size}</span>\n          </div>\n\n          {/* Topic Boundaries */}\n          <div className=\"chunking-inspector__section\">\n            <h4>Topic Boundaries (Stage 2)</h4>\n            <div className=\"chunking-inspector__topics\">\n              {data.topic_boundaries.map((tb, i) => (\n                <div key={i} className=\"chunking-topic\" title={tb.topic_label}>\n                  <span className=\"chunking-topic__label\">{tb.topic_label}</span>\n                  <span className=\"chunking-topic__meta\">\n                    {tb.segment_count} segs &middot; {tb.start_time.toFixed(0)}s-{tb.end_time.toFixed(0)}s\n                  </span>\n                </div>\n              ))}\n            </div>\n          </div>\n\n          {/* Synthesis Groups */}\n          <div className=\"chunking-inspector__section\">\n            <h4>Synthesis Groups (Stage 5 Input)</h4>\n            <div className=\"chunking-inspector__groups\">\n              {data.synthesis_groups.map((g) => (\n                <div\n                  key={g.category}\n                  className={`chunking-group ${g.exceeds_chunk_threshold ? \"chunking-group--split\" : \"\"}`}\n                >\n                  <span className=\"chunking-group__category\">{g.category}</span>\n                  <span className=\"chunking-group__count\">{g.moment_count} moments</span>\n                  {g.exceeds_chunk_threshold && (\n                    <span className=\"chunking-group__warn\">\n                      Will split into {g.chunks_needed} chunks\n                    </span>\n                  )}\n                </div>\n              ))}\n            </div>\n          </div>\n        </div>\n      )}\n    </div>\n  );\n}\n\n\nexport default function AdminPipeline() {\n  useDocumentTitle(\"Pipeline Management — Chrysopedia\");\n  const [searchParams] = useSearchParams();\n  const [videos, setVideos] = useState<PipelineVideoItem[]>([]);\n  const [loading, setLoading] = useState(true);\n  const [error, setError] = useState<string | null>(null);\n  const [expandedId, setExpandedId] = useState<string | null>(null);\n  const [actionLoading, setActionLoading] = useState<string | null>(null);\n  const [actionMessage, setActionMessage] = useState<{ id: string; text: string; ok: boolean } | null>(null);\n  const [activeFilter, setActiveFilter] = useState<string | null>(null);\n  const [debugMode, setDebugModeState] = useState<boolean | null>(null);\n  const [creators, setCreators] = useState<{ name: string; slug: string }[]>([]);\n  const [creatorFilter, setCreatorFilter] = useState<string | null>(null);\n  const [searchQuery, setSearchQuery] = useState(\"\");\n  const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());\n  // Re-run stage modal state\n  const [rerunModalVideo, setRerunModalVideo] = useState<string | null>(null);\n  const [rerunStageSelect, setRerunStageSelect] = useState(\"stage5_synthesis\");\n  const [rerunPromptOverride, setRerunPromptOverride] = useState(\"\");\n  // Stale pages state\n  const [stalePagesCount, setStalePagesCount] = useState<number | null>(null);\n  const [bulkProgress, setBulkProgress] = useState<BulkProgress | null>(null);\n  const [bulkLog, setBulkLog] = useState<BulkLogEntry[]>([]);\n  const [changedIds, setChangedIds] = useState<Set<string>>(new Set());\n  const [autoRefresh, setAutoRefresh] = useState(true);\n  const bulkCancelRef = useRef(false);\n  const prevStatusRef = useRef<Map<string, string>>(new Map());\n  const videoRefs = useRef<Map<string, HTMLDivElement>>(new Map());\n  const deepLinked = useRef(false);\n\n  // Compute filtered list (status + creator) — memoized to avoid recomputing on unrelated state changes\n  const filteredVideos = useMemo(() => videos.filter((v) => {\n    if (activeFilter !== null && v.processing_status !== activeFilter) return false;\n    if (creatorFilter !== null && v.creator_name !== creatorFilter) return false;\n    if (searchQuery) {\n      const q = searchQuery.toLowerCase();\n      if (!v.filename.toLowerCase().includes(q) && !v.creator_name.toLowerCase().includes(q)) return false;\n    }\n    return true;\n  }), [videos, activeFilter, creatorFilter, searchQuery]);\n\n  const load = useCallback(async (isAutoRefresh = false) => {\n    if (!isAutoRefresh) setLoading(true);\n    setError(null);\n    try {\n      const res = await fetchPipelineVideos();\n      // Detect status changes for highlight animation\n      if (prevStatusRef.current.size > 0) {\n        const changed = new Set<string>();\n        for (const v of res.items) {\n          const prev = prevStatusRef.current.get(v.id);\n          if (prev && prev !== v.processing_status) {\n            changed.add(v.id);\n          }\n        }\n        if (changed.size > 0) {\n          setChangedIds(changed);\n          // Clear highlights after animation\n          setTimeout(() => setChangedIds(new Set()), 2000);\n        }\n      }\n      // Store current statuses for next comparison\n      const statusMap = new Map<string, string>();\n      for (const v of res.items) {\n        statusMap.set(v.id, v.processing_status);\n      }\n      prevStatusRef.current = statusMap;\n      setVideos(res.items);\n    } catch (err) {\n      if (!isAutoRefresh) {\n        setError(err instanceof Error ? err.message : \"Failed to load videos\");\n      }\n    } finally {\n      if (!isAutoRefresh) setLoading(false);\n    }\n  }, []);\n\n  useEffect(() => {\n    void load();\n  }, [load]);\n\n  // Auto-refresh every 15 seconds\n  useEffect(() => {\n    if (!autoRefresh) return;\n    const id = setInterval(() => void load(true), 15_000);\n    return () => clearInterval(id);\n  }, [autoRefresh, load]);\n\n  // Load creators for filter dropdown\n  useEffect(() => {\n    fetchCreators({ limit: 200 })\n      .then((res) => {\n        const list = res.items.map((c: { name: string; slug: string }) => ({\n          name: c.name,\n          slug: c.slug,\n        }));\n        list.sort((a: { name: string }, b: { name: string }) => a.name.localeCompare(b.name));\n        setCreators(list);\n      })\n      .catch(() => {\n        // silently fail — filter stays hidden\n      });\n  }, []);\n\n  // Load stale page count\n  useEffect(() => {\n    fetchStalePages()\n      .then((res) => setStalePagesCount(res.stale_pages))\n      .catch(() => { /* silently fail */ });\n  }, []);\n\n  const handleBulkResynth = async () => {\n    if (!confirm(\"Re-run stage 5 synthesis on all completed videos with the current prompt?\")) return;\n    try {\n      const res = await bulkResynthesize();\n      setActionMessage({\n        id: \"__bulk__\",\n        text: `Bulk re-synthesize dispatched: ${res.dispatched}/${res.total} videos`,\n        ok: true,\n      });\n      setStalePagesCount(null); // will refresh\n    } catch (err) {\n      setActionMessage({\n        id: \"__bulk__\",\n        text: err instanceof Error ? err.message : \"Bulk re-synth failed\",\n        ok: false,\n      });\n    }\n  };\n\n  const handleWipeAll = async () => {\n    if (!confirm(\"WIPE ALL pipeline output? This deletes all technique pages, key moments, pipeline events, runs, and Qdrant vectors. Creators, videos, and transcripts are preserved. This cannot be undone.\")) return;\n    if (!confirm(\"Are you sure? This is irreversible.\")) return;\n    try {\n      const res = await wipeAllOutput();\n      setActionMessage({\n        id: \"__wipe__\",\n        text: `Wiped: ${JSON.stringify(res.deleted)}`,\n        ok: true,\n      });\n      setStalePagesCount(null);\n      void load();\n    } catch (err) {\n      setActionMessage({\n        id: \"__wipe__\",\n        text: err instanceof Error ? err.message : \"Wipe failed\",\n        ok: false,\n      });\n    }\n  };\n\n  // Deep-link: auto-expand and scroll to ?video=<id> on first load\n  useEffect(() => {\n    if (deepLinked.current || loading || videos.length === 0) return;\n    const targetVideoId = searchParams.get(\"video\");\n    if (!targetVideoId) return;\n    const match = videos.find((v) => v.id === targetVideoId);\n    if (!match) return;\n    deepLinked.current = true;\n    setExpandedId(targetVideoId);\n    requestAnimationFrame(() => {\n      const el = videoRefs.current.get(targetVideoId);\n      if (el) el.scrollIntoView({ behavior: \"smooth\", block: \"start\" });\n    });\n  }, [loading, videos, searchParams]);\n\n  useEffect(() => {\n    let cancelled = false;\n    fetchDebugMode()\n      .then((res) => {\n        if (!cancelled) setDebugModeState(res.debug_mode);\n      })\n      .catch(() => {});\n    return () => { cancelled = true; };\n  }, []);\n\n  // Clear selection when filters change\n  useEffect(() => {\n    setSelectedIds(new Set());\n  }, [activeFilter, creatorFilter, searchQuery]);\n\n  const handleTrigger = async (videoId: string) => {\n    setActionLoading(videoId);\n    setActionMessage(null);\n    try {\n      const res = await cleanRetriggerPipeline(videoId);\n      setActionMessage({ id: videoId, text: `Retriggered (${res.status})`, ok: true });\n      setTimeout(() => void load(), 2000);\n    } catch (err) {\n      setActionMessage({\n        id: videoId,\n        text: err instanceof Error ? err.message : \"Retrigger failed\",\n        ok: false,\n      });\n    } finally {\n      setActionLoading(null);\n    }\n  };\n\n  const handleRevoke = async (videoId: string) => {\n    setActionLoading(videoId);\n    setActionMessage(null);\n    try {\n      const res = await revokePipeline(videoId);\n      setActionMessage({\n        id: videoId,\n        text: res.tasks_revoked > 0\n          ? `Revoked ${res.tasks_revoked} task${res.tasks_revoked !== 1 ? \"s\" : \"\"}`\n          : \"No active tasks\",\n        ok: true,\n      });\n      setTimeout(() => void load(), 2000);\n    } catch (err) {\n      setActionMessage({\n        id: videoId,\n        text: err instanceof Error ? err.message : \"Revoke failed\",\n        ok: false,\n      });\n    } finally {\n      setActionLoading(null);\n    }\n  };\n\n  const handleRerunStage = async (videoId: string) => {\n    setActionLoading(videoId);\n    setActionMessage(null);\n    try {\n      const res = await rerunStage(\n        videoId,\n        rerunStageSelect,\n        rerunPromptOverride.trim() || undefined,\n      );\n      setActionMessage({\n        id: videoId,\n        text: `Stage re-run dispatched: ${res.stage}${res.prompt_override ? \" (with prompt override)\" : \"\"}`,\n        ok: true,\n      });\n      setRerunModalVideo(null);\n      setRerunPromptOverride(\"\");\n      setTimeout(() => void load(), 2000);\n    } catch (err) {\n      setActionMessage({\n        id: videoId,\n        text: err instanceof Error ? err.message : \"Stage re-run failed\",\n        ok: false,\n      });\n    } finally {\n      setActionLoading(null);\n    }\n  };\n\n  const toggleExpand = (id: string) => {\n    setExpandedId((prev) => (prev === id ? null : id));\n  };\n\n  // ── Selection ───────────────────────────────────────────────────────────\n\n  const toggleSelect = (id: string) => {\n    setSelectedIds((prev) => {\n      const next = new Set(prev);\n      if (next.has(id)) next.delete(id);\n      else next.add(id);\n      return next;\n    });\n  };\n\n  const toggleSelectAll = () => {\n    const visibleIds = filteredVideos.map((v) => v.id);\n    const allSelected = visibleIds.every((id) => selectedIds.has(id));\n    if (allSelected) {\n      setSelectedIds(new Set());\n    } else {\n      setSelectedIds(new Set(visibleIds));\n    }\n  };\n\n  const allVisibleSelected = filteredVideos.length > 0 && filteredVideos.every((v) => selectedIds.has(v.id));\n\n  // ── Bulk Actions ────────────────────────────────────────────────────────\n\n  const runBulk = async (clean: boolean) => {\n    const ids = Array.from(selectedIds);\n    if (ids.length === 0) return;\n\n    bulkCancelRef.current = false;\n    setBulkLog([]);\n    setBulkProgress({ total: ids.length, completed: 0, failed: 0, current: null, active: true });\n\n    let completed = 0;\n    let failed = 0;\n\n    for (const id of ids) {\n      if (bulkCancelRef.current) break;\n\n      const video = videos.find((v) => v.id === id);\n      const filename = video?.filename ?? id;\n      setBulkProgress((p) => p ? { ...p, current: filename } : null);\n\n      try {\n        if (clean) {\n          await cleanRetriggerPipeline(id);\n        } else {\n          await triggerPipeline(id);\n        }\n        completed++;\n        setBulkLog((prev) => [...prev, { filename, ok: true, message: clean ? \"clean retriggered\" : \"triggered\" }]);\n      } catch (err) {\n        failed++;\n        setBulkLog((prev) => [...prev, {\n          filename,\n          ok: false,\n          message: err instanceof Error ? err.message : \"failed\",\n        }]);\n      }\n\n      setBulkProgress((p) => p ? { ...p, completed: completed, failed } : null);\n\n      // Brief pause between dispatches to avoid slamming the API\n      if (!bulkCancelRef.current && ids.indexOf(id) < ids.length - 1) {\n        await new Promise((r) => setTimeout(r, 500));\n      }\n    }\n\n    setBulkProgress((p) => p ? { ...p, active: false, current: null } : null);\n    setSelectedIds(new Set());\n    // Refresh video list after bulk completes\n    setTimeout(() => void load(), 2000);\n  };\n\n  const cancelBulk = () => {\n    bulkCancelRef.current = true;\n  };\n\n  return (\n    <div className=\"admin-pipeline\">\n      <div className=\"admin-pipeline__header\">\n        <div>\n          <h1 className=\"admin-pipeline__title\">Pipeline Management</h1>\n          <p className=\"admin-pipeline__subtitle\">\n            {videos.length} video{videos.length !== 1 ? \"s\" : \"\"}\n          </p>\n        </div>\n        <div className=\"admin-pipeline__header-right\">\n          {stalePagesCount !== null && stalePagesCount > 0 && (\n            <button\n              className=\"btn btn--small btn--warning\"\n              onClick={() => void handleBulkResynth()}\n              title={`${stalePagesCount} pages synthesized with an older prompt`}\n            >\n              {stalePagesCount} stale pages\n            </button>\n          )}\n          <button\n            className=\"btn btn--small btn--danger\"\n            onClick={() => void handleWipeAll()}\n            title=\"Delete all pipeline output (technique pages, moments, events). Preserves videos and transcripts.\"\n          >\n            Wipe All Output\n          </button>\n          <DebugModeToggle debugMode={debugMode} onDebugModeChange={setDebugModeState} />\n          <WorkerStatus />\n          <label className=\"auto-refresh-toggle\" title=\"Auto-refresh video list every 15 seconds\">\n            <input\n              type=\"checkbox\"\n              checked={autoRefresh}\n              onChange={(e) => setAutoRefresh(e.target.checked)}\n            />\n            Auto-refresh\n          </label>\n          <button className=\"btn btn--secondary\" onClick={() => void load()} disabled={loading}>\n            ↻ Refresh\n          </button>\n        </div>\n      </div>\n\n      {loading ? (\n        <div className=\"loading\">Loading videos…</div>\n      ) : error ? (\n        <div className=\"loading error-text\">Error: {error}</div>\n      ) : videos.length === 0 ? (\n        <div className=\"empty-state\">No videos in pipeline.</div>\n      ) : (\n        <>\n          <RecentActivityFeed />\n          <div className=\"admin-pipeline__filters\">\n            <StatusFilter\n              videos={videos}\n              activeFilter={activeFilter}\n              onFilterChange={setActiveFilter}\n            />\n            {creators.length > 1 && (\n              <div className=\"creator-filter\">\n                <select\n                  className=\"creator-filter__select\"\n                  value={creatorFilter ?? \"\"}\n                  onChange={(e) => setCreatorFilter(e.target.value || null)}\n                >\n                  <option value=\"\">All Creators</option>\n                  {creators.map((c) => (\n                    <option key={c.slug} value={c.name}>{c.name}</option>\n                  ))}\n                </select>\n              </div>\n            )}\n            <div className=\"pipeline-search\">\n              <input\n                type=\"text\"\n                className=\"pipeline-search__input\"\n                placeholder=\"Filter by filename or creator…\"\n                value={searchQuery}\n                onChange={(e) => setSearchQuery(e.target.value)}\n              />\n              {searchQuery && (\n                <button\n                  className=\"pipeline-search__clear\"\n                  onClick={() => setSearchQuery(\"\")}\n                  aria-label=\"Clear search\"\n                >\n                  ✕\n                </button>\n              )}\n            </div>\n          </div>\n\n          {/* Bulk toolbar */}\n          {selectedIds.size > 0 && (\n            <div className=\"bulk-toolbar\">\n              <span className=\"bulk-toolbar__count\">\n                {selectedIds.size} selected\n              </span>\n              {bulkProgress?.active ? (\n                <>\n                  <div className=\"bulk-toolbar__progress\">\n                    <div className=\"bulk-toolbar__progress-bar\">\n                      <div\n                        className=\"bulk-toolbar__progress-fill\"\n                        style={{ width: `${((bulkProgress.completed + bulkProgress.failed) / bulkProgress.total) * 100}%` }}\n                      />\n                    </div>\n                    <span className=\"bulk-toolbar__progress-text\">\n                      {bulkProgress.completed + bulkProgress.failed}/{bulkProgress.total}\n                      {bulkProgress.failed > 0 && <span className=\"error-text\"> ({bulkProgress.failed} failed)</span>}\n                    </span>\n                    {bulkProgress.current && (\n                      <span className=\"bulk-toolbar__current\" title={bulkProgress.current}>\n                        {bulkProgress.current}\n                      </span>\n                    )}\n                  </div>\n                  <button className=\"btn btn--small btn--danger\" onClick={cancelBulk}>\n                    Cancel\n                  </button>\n                </>\n              ) : (\n                <>\n                  <button\n                    className=\"btn btn--small btn--primary\"\n                    onClick={() => void runBulk(false)}\n                    title=\"Retrigger pipeline for selected videos\"\n                  >\n                    ▶ Retrigger Selected\n                  </button>\n                  <button\n                    className=\"btn btn--small btn--warning\"\n                    onClick={() => void runBulk(true)}\n                    title=\"Wipe pipeline output (events, moments, segments) then retrigger\"\n                  >\n                    🧹 Clean Reprocess\n                  </button>\n                  <button\n                    className=\"btn btn--small btn--secondary\"\n                    onClick={() => setSelectedIds(new Set())}\n                  >\n                    Clear\n                  </button>\n                </>\n              )}\n            </div>\n          )}\n\n          {/* Bulk completion message */}\n          {bulkProgress && !bulkProgress.active && (\n            <div className={`bulk-toolbar__done ${bulkProgress.failed > 0 ? \"bulk-toolbar__done--warn\" : \"bulk-toolbar__done--ok\"}`}>\n              Bulk operation complete: {bulkProgress.completed} succeeded\n              {bulkProgress.failed > 0 && `, ${bulkProgress.failed} failed`}\n              {bulkCancelRef.current && \" (cancelled)\"}\n              <button className=\"bulk-toolbar__dismiss\" onClick={() => setBulkProgress(null)}>✕</button>\n            </div>\n          )}\n\n          {/* Bulk operation log */}\n          {bulkLog.length > 0 && (bulkProgress?.active || (bulkProgress && !bulkProgress.active)) && (\n            <div className=\"bulk-log\">\n              <div className=\"bulk-log__list\">\n                {bulkLog.map((entry, i) => (\n                  <div key={i} className={`bulk-log__entry ${entry.ok ? \"bulk-log__entry--ok\" : \"bulk-log__entry--err\"}`}>\n                    <span className=\"bulk-log__icon\">{entry.ok ? \"✓\" : \"✗\"}</span>\n                    <span className=\"bulk-log__file\">{entry.filename}</span>\n                    <span className=\"bulk-log__msg\">{entry.message}</span>\n                  </div>\n                ))}\n              </div>\n            </div>\n          )}\n\n          <div className=\"admin-pipeline__list\">\n            {filteredVideos.map((video) => (\n            <div key={video.id} className={`pipeline-video${changedIds.has(video.id) ? \" pipeline-video--changed\" : \"\"}`} ref={(el) => { if (el) videoRefs.current.set(video.id, el); }}>\n              <div\n                className=\"pipeline-video__header\"\n                onClick={() => toggleExpand(video.id)}\n              >\n                <div className=\"pipeline-video__checkbox\" onClick={(e) => e.stopPropagation()}>\n                  <input\n                    type=\"checkbox\"\n                    checked={selectedIds.has(video.id)}\n                    onChange={() => toggleSelect(video.id)}\n                    disabled={bulkProgress?.active ?? false}\n                    aria-label={`Select ${video.filename}`}\n                  />\n                </div>\n                <div className=\"pipeline-video__info\">\n                  <span className=\"pipeline-video__filename\" title={video.filename}>\n                    {video.filename}\n                  </span>\n                  <span className=\"pipeline-video__creator\">{video.creator_name}</span>\n                </div>\n\n                <div className=\"pipeline-video__meta\">\n                  <span className={`pipeline-badge ${statusBadgeClass(video.processing_status)}`}>\n\n[Showing lines 1-1390 of 1523 (50.0KB limit). Use offset=1391 to continue.]"}],"details":{"truncation":{"content":"/**\n * Pipeline admin dashboard — video list with status, retrigger/revoke,\n * expandable event log with token usage and collapsible JSON viewer.\n */\n\nimport React, { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport { useSearchParams } from \"react-router-dom\";\nimport { useDocumentTitle } from \"../hooks/useDocumentTitle\";\nimport {\n  fetchPipelineVideos,\n  fetchPipelineEvents,\n  fetchPipelineRuns,\n  fetchWorkerStatus,\n  fetchDebugMode,\n  setDebugMode,\n  triggerPipeline,\n  revokePipeline,\n  cleanRetriggerPipeline,\n  rerunStage,\n  fetchChunkingData,\n  fetchStalePages,\n  bulkResynthesize,\n  wipeAllOutput,\n  fetchCreators,\n  fetchRecentActivity,\n  type PipelineVideoItem,\n  type PipelineEvent,\n  type PipelineRunItem,\n  type WorkerStatusResponse,\n  type RecentActivityItem,\n} from \"../api/public-client\";\n\n// ── Helpers ──────────────────────────────────────────────────────────────────\n\nfunction formatDate(iso: string | null): string {\n  if (!iso) return \"—\";\n  return new Date(iso).toLocaleString(undefined, {\n    month: \"short\",\n    day: \"numeric\",\n    hour: \"2-digit\",\n    minute: \"2-digit\",\n    second: \"2-digit\",\n  });\n}\n\nfunction formatTokens(n: number): string {\n  if (n === 0) return \"0\";\n  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;\n  if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;\n  return String(n);\n}\n\n\nfunction formatElapsed(isoStart: string | null): string {\n  if (!isoStart) return \"\";\n  const ms = Date.now() - new Date(isoStart).getTime();\n  if (ms < 0) return \"\";\n  const secs = Math.floor(ms / 1000);\n  if (secs < 60) return `${secs}s`;\n  const mins = Math.floor(secs / 60);\n  const remainSecs = secs % 60;\n  if (mins < 60) return `${mins}m ${remainSecs}s`;\n  const hrs = Math.floor(mins / 60);\n  return `${hrs}h ${mins % 60}m`;\n}\nconst STATUS_LABELS: Record<string, string> = {\n  not_started: \"Not Started\",\n  queued: \"Queued\",\n  processing: \"In Progress\",\n  error: \"Errored\",\n  complete: \"Complete\",\n};\n\nfunction statusBadgeClass(status: string): string {\n  switch (status) {\n    case \"complete\":\n      return \"pipeline-badge--success\";\n    case \"processing\":\n      return \"pipeline-badge--active\";\n    case \"error\":\n      return \"pipeline-badge--error\";\n    case \"not_started\":\n    case \"queued\":\n      return \"pipeline-badge--pending\";\n    default:\n      return \"\";\n  }\n}\n\nfunction eventTypeIcon(eventType: string): string {\n  switch (eventType) {\n    case \"start\":\n      return \"▶\";\n    case \"complete\":\n      return \"✓\";\n    case \"error\":\n      return \"✗\";\n    case \"llm_call\":\n      return \"⚙\";\n    default:\n      return \"·\";\n  }\n}\n\n// ── Collapsible JSON ─────────────────────────────────────────────────────────\n\nfunction JsonViewer({ data }: { data: Record<string, unknown> | null }) {\n  const [open, setOpen] = useState(false);\n  if (!data || Object.keys(data).length === 0) return null;\n\n  return (\n    <div className=\"json-viewer\">\n      <button\n        className=\"json-viewer__toggle\"\n        onClick={() => setOpen((v) => !v)}\n        aria-expanded={open}\n        title=\"Raw JSON event data logged by the pipeline stage\"\n      >\n        {open ? \"▾ Hide payload\" : \"▸ Show payload\"}\n      </button>\n      {open && (\n        <pre className=\"json-viewer__content\">\n          {JSON.stringify(data, null, 2)}\n        </pre>\n      )}\n    </div>\n  );\n}\n\n// ── Debug Payload Viewer ─────────────────────────────────────────────────────\n\ninterface DebugSection {\n  label: string;\n  content: string;\n}\n\nfunction DebugPayloadViewer({ event }: { event: PipelineEvent }) {\n  const sections: DebugSection[] = [];\n  if (event.payload?.request_params) sections.push({ label: \"Request Params\", content: JSON.stringify(event.payload.request_params, null, 2) });\n  if (event.system_prompt_text) sections.push({ label: \"System Prompt\", content: event.system_prompt_text });\n  if (event.user_prompt_text) sections.push({ label: \"User Prompt\", content: event.user_prompt_text });\n  if (event.response_text) sections.push({ label: \"Response\", content: event.response_text });\n\n  const [openSections, setOpenSections] = useState<Record<string, boolean>>({});\n  const [copiedKey, setCopiedKey] = useState<string | null>(null);\n\n  const SECTION_TOOLTIPS: Record<string, string> = {\n    \"System Prompt\": \"Instructions sent to the LLM defining its role and output format for this pipeline stage\",\n    \"User Prompt\": \"The actual transcript content and extraction request sent to the LLM\",\n    \"Response\": \"Raw LLM output before parsing — the extracted data that feeds the next stage\",\n  };\n\n  if (sections.length === 0) return null;\n\n  const toggleSection = (label: string) => {\n    setOpenSections((prev) => ({ ...prev, [label]: !prev[label] }));\n  };\n\n  const copyToClipboard = async (label: string, text: string) => {\n    try {\n      await navigator.clipboard.writeText(text);\n      setCopiedKey(label);\n      setTimeout(() => setCopiedKey(null), 1500);\n    } catch {\n      // Fallback for non-HTTPS contexts\n      const ta = document.createElement(\"textarea\");\n      ta.value = text;\n      ta.style.position = \"fixed\";\n      ta.style.opacity = \"0\";\n      document.body.appendChild(ta);\n      ta.select();\n      document.execCommand(\"copy\");\n      document.body.removeChild(ta);\n      setCopiedKey(label);\n      setTimeout(() => setCopiedKey(null), 1500);\n    }\n  };\n\n  const exportAsJson = () => {\n    // Dump everything — full rawdog debug payload with request params\n    const data: Record<string, unknown> = {\n      event_id: event.id,\n      stage: event.stage,\n      event_type: event.event_type,\n      model: event.model,\n      prompt_tokens: event.prompt_tokens,\n      completion_tokens: event.completion_tokens,\n      total_tokens: event.total_tokens,\n      duration_ms: event.duration_ms,\n      created_at: event.created_at,\n      payload: event.payload,\n      request_params: event.payload?.request_params ?? null,\n      system_prompt_text: event.system_prompt_text,\n      user_prompt_text: event.user_prompt_text,\n      response_text: event.response_text,\n    };\n    const blob = new Blob([JSON.stringify(data, null, 2)], { type: \"application/json\" });\n    const url = URL.createObjectURL(blob);\n    const a = document.createElement(\"a\");\n    a.href = url;\n    a.download = `debug-${event.stage}-${event.id.slice(0, 8)}.json`;\n    a.click();\n    URL.revokeObjectURL(url);\n  };\n\n  return (\n    <div className=\"debug-viewer\">\n      <div className=\"debug-viewer__header\">\n        <span className=\"debug-viewer__label\">LLM Debug</span>\n        <button className=\"debug-viewer__export\" onClick={exportAsJson} title=\"Export debug payload as JSON\">\n          ↓ JSON\n        </button>\n      </div>\n      {sections.map((sec) => {\n        const isOpen = !!openSections[sec.label];\n        return (\n          <div key={sec.label} className=\"debug-viewer__section\">\n            <div className=\"debug-viewer__section-header\">\n              <button\n                className=\"debug-viewer__section-toggle\"\n                onClick={() => toggleSection(sec.label)}\n                aria-expanded={isOpen}\n                title={SECTION_TOOLTIPS[sec.label] ?? \"\"}\n              >\n                {isOpen ? \"▾\" : \"▸\"} {sec.label}\n              </button>\n              <button\n                className=\"debug-viewer__copy\"\n                onClick={() => void copyToClipboard(sec.label, sec.content)}\n                title={`Copy ${sec.label}`}\n              >\n                {copiedKey === sec.label ? \"✓ Copied\" : \"Copy\"}\n              </button>\n            </div>\n            {isOpen && (\n              <pre className=\"debug-viewer__content\">{sec.content}</pre>\n            )}\n          </div>\n        );\n      })}\n    </div>\n  );\n}\n\n// ── Event Log ────────────────────────────────────────────────────────────────\n\nfunction EventLog({ videoId, status, runId }: { videoId: string; status: string; runId?: string }) {\n  const [events, setEvents] = useState<PipelineEvent[]>([]);\n  const [total, setTotal] = useState(0);\n  const [loading, setLoading] = useState(true);\n  const [error, setError] = useState<string | null>(null);\n  const [offset, setOffset] = useState(0);\n  const [viewMode, setViewMode] = useState<\"head\" | \"tail\">(\"tail\");\n  const limit = 50;\n  const initialLoaded = useRef(false);\n\n  const load = useCallback(async (silent = false) => {\n    if (!silent) setLoading(true);\n    setError(null);\n    try {\n      const res = await fetchPipelineEvents(videoId, {\n        offset,\n        limit,\n        order: viewMode === \"head\" ? \"asc\" : \"desc\",\n        run_id: runId,\n      });\n      setEvents(res.items);\n      setTotal(res.total);\n      initialLoaded.current = true;\n    } catch (err) {\n      if (!silent) setError(err instanceof Error ? err.message : \"Failed to load events\");\n    } finally {\n      if (!silent) setLoading(false);\n    }\n  }, [videoId, offset, viewMode]);\n\n  useEffect(() => {\n    void load();\n  }, [load]);\n\n  // Auto-refresh when video is actively processing\n  useEffect(() => {\n    if (status !== \"processing\" && status !== \"queued\") return;\n    const id = setInterval(() => void load(true), 10_000);\n    return () => clearInterval(id);\n  }, [status, load]);\n\n  if (loading) return <div className=\"loading\">Loading events…</div>;\n  if (error) return <div className=\"loading error-text\">Error: {error}</div>;\n  if (events.length === 0) return <div className=\"pipeline-events__empty\">No events recorded.</div>;\n\n  const hasNext = offset + limit < total;\n  const hasPrev = offset > 0;\n\n  return (\n    <div className=\"pipeline-events\">\n      <div className=\"pipeline-events__header\">\n        <span className=\"pipeline-events__count\">{total} event{total !== 1 ? \"s\" : \"\"}</span>\n        <div className=\"pipeline-events__view-toggle\">\n          <button\n            className={`pipeline-events__view-btn${viewMode === \"head\" ? \" pipeline-events__view-btn--active\" : \"\"}`}\n            onClick={() => { setViewMode(\"head\"); setOffset(0); }}\n          >\n            Oldest first\n          </button>\n          <button\n            className={`pipeline-events__view-btn${viewMode === \"tail\" ? \" pipeline-events__view-btn--active\" : \"\"}`}\n            onClick={() => { setViewMode(\"tail\"); setOffset(0); }}\n          >\n            Newest first\n          </button>\n        </div>\n      </div>\n\n      <div className=\"pipeline-events__list\">\n        {events.map((evt) => (\n          <div key={evt.id} className={`pipeline-event pipeline-event--${evt.event_type}`}>\n            <div className=\"pipeline-event__row\">\n              <span className=\"pipeline-event__icon\">{eventTypeIcon(evt.event_type)}</span>\n              <span className=\"pipeline-event__stage\">{evt.stage}</span>\n              <span className={`pipeline-badge pipeline-badge--event-${evt.event_type}`}>\n                {evt.event_type}\n              </span>\n              {typeof evt.payload?.context === \"string\" && (\n                <span className=\"pipeline-event__context\" title=\"Processing context\">\n                  {evt.payload.context}\n                </span>\n              )}\n              {evt.model && <span className=\"pipeline-event__model\">{evt.model}</span>}\n              {evt.total_tokens != null && evt.total_tokens > 0 && (\n                <span className=\"pipeline-event__tokens\" title={`prompt: ${evt.prompt_tokens ?? 0} / completion: ${evt.completion_tokens ?? 0}`}>\n                  {formatTokens(evt.total_tokens)} tok\n                </span>\n              )}\n              {evt.duration_ms != null && (\n                <span className=\"pipeline-event__duration\">{evt.duration_ms}ms</span>\n              )}\n              <span className=\"pipeline-event__time\">{formatDate(evt.created_at)}</span>\n            </div>\n            <JsonViewer data={evt.payload} />\n            <DebugPayloadViewer event={evt} />\n          </div>\n        ))}\n      </div>\n\n      {(hasPrev || hasNext) && (\n        <div className=\"pipeline-events__pager\">\n          <button\n            className=\"btn btn--small btn--secondary\"\n            disabled={!hasPrev}\n            onClick={() => setOffset((o) => Math.max(0, o - limit))}\n          >\n            ← Prev\n          </button>\n          <span className=\"pipeline-events__pager-info\">\n            {offset + 1}–{Math.min(offset + limit, total)} of {total}\n          </span>\n          <button\n            className=\"btn btn--small btn--secondary\"\n            disabled={!hasNext}\n            onClick={() => setOffset((o) => o + limit)}\n          >\n            Next →\n          </button>\n        </div>\n      )}\n    </div>\n  );\n}\n\n// ── Worker Status ────────────────────────────────────────────────────────────\n\nconst WorkerStatus = React.memo(function WorkerStatus() {\n  const [status, setStatus] = useState<WorkerStatusResponse | null>(null);\n  const [error, setError] = useState<string | null>(null);\n\n  const load = useCallback(async () => {\n    try {\n      setError(null);\n      const res = await fetchWorkerStatus();\n      setStatus(res);\n    } catch (err) {\n      setError(err instanceof Error ? err.message : \"Failed\");\n    }\n  }, []);\n\n  useEffect(() => {\n    void load();\n    const id = setInterval(() => void load(), 15_000);\n    return () => clearInterval(id);\n  }, [load]);\n\n  if (error) {\n    return (\n      <div className=\"worker-status worker-status--error\">\n        <span className=\"worker-status__dot worker-status__dot--offline\" />\n        Worker: error ({error})\n      </div>\n    );\n  }\n\n  if (!status) {\n    return (\n      <div className=\"worker-status\">\n        <span className=\"worker-status__dot worker-status__dot--unknown\" />\n        Worker: checking…\n      </div>\n    );\n  }\n\n  return (\n    <div className={`worker-status ${status.online ? \"worker-status--online\" : \"worker-status--offline\"}`}>\n      <span className={`worker-status__dot ${status.online ? \"worker-status__dot--online\" : \"worker-status__dot--offline\"}`} />\n      <span className=\"worker-status__label\">\n        {status.online ? `${status.workers.length} worker${status.workers.length !== 1 ? \"s\" : \"\"} online` : \"Workers offline\"}\n      </span>\n      {status.workers.map((w) => (\n        <span key={w.name} className=\"worker-status__detail\" title={w.name}>\n          {w.active_tasks.length > 0\n            ? `${w.active_tasks.length} active`\n            : \"idle\"}\n          {w.pool_size != null && ` · pool ${w.pool_size}`}\n        </span>\n      ))}\n    </div>\n  );\n});\n\n// ── Debug Mode Toggle ────────────────────────────────────────────────────────\n\nfunction DebugModeToggle({\n  debugMode,\n  onDebugModeChange,\n}: {\n  debugMode: boolean | null;\n  onDebugModeChange: (mode: boolean) => void;\n}) {\n  const [debugLoading, setDebugLoading] = useState(false);\n\n  async function handleToggle() {\n    if (debugMode === null || debugLoading) return;\n    setDebugLoading(true);\n    try {\n      const res = await setDebugMode(!debugMode);\n      onDebugModeChange(res.debug_mode);\n    } catch {\n      // swallow — leave previous state\n    } finally {\n      setDebugLoading(false);\n    }\n  }\n\n  if (debugMode === null) return null;\n\n  return (\n    <div className=\"debug-toggle\" title=\"When enabled, pipeline runs capture full LLM prompts and responses for inspection in the event log\">\n      <span className=\"debug-toggle__label\">\n        Debug {debugMode ? \"On\" : \"Off\"}\n      </span>\n      <button\n        type=\"button\"\n        className={`debug-toggle__switch ${debugMode ? \"debug-toggle__switch--active\" : \"\"}`}\n        onClick={handleToggle}\n        disabled={debugLoading}\n        aria-label={`Turn debug mode ${debugMode ? \"off\" : \"on\"}`}\n      />\n    </div>\n  );\n}\n\n// ── Status Filter ────────────────────────────────────────────────────────────\n\nconst StatusFilter = React.memo(function StatusFilter({\n  videos,\n  activeFilter,\n  onFilterChange,\n}: {\n  videos: PipelineVideoItem[];\n  activeFilter: string | null;\n  onFilterChange: (filter: string | null) => void;\n}) {\n  // Fixed display order for pipeline lifecycle\n  const STATUS_ORDER = [\"not_started\", \"queued\", \"processing\", \"error\", \"complete\"];\n  const present = new Set(videos.map((v) => v.processing_status));\n  const ordered = STATUS_ORDER.filter((s) => present.has(s));\n\n  if (ordered.length <= 1) return null;\n\n  return (\n    <div className=\"filter-tabs\">\n      <button\n        type=\"button\"\n        className={`filter-tab ${activeFilter === null ? \"filter-tab--active\" : \"\"}`}\n        onClick={() => onFilterChange(null)}\n      >\n        All ({videos.length})\n      </button>\n      {ordered.map((status) => {\n        const count = videos.filter((v) => v.processing_status === status).length;\n        return (\n          <button\n            key={status}\n            type=\"button\"\n            className={`filter-tab ${activeFilter === status ? \"filter-tab--active\" : \"\"}`}\n            onClick={() => onFilterChange(status)}\n          >\n            {STATUS_LABELS[status] ?? status} ({count})\n          </button>\n        );\n      })}\n    </div>\n  );\n});\n\n// ── Run List ─────────────────────────────────────────────────────────────────\n\nconst TRIGGER_LABELS: Record<string, string> = {\n  manual: \"Manual\",\n  clean_reprocess: \"Clean Reprocess\",\n  auto_ingest: \"Auto Ingest\",\n  bulk: \"Bulk\",\n};\n\nconst RUN_STATUS_CLASS: Record<string, string> = {\n  running: \"pipeline-badge--active\",\n  complete: \"pipeline-badge--success\",\n  error: \"pipeline-badge--error\",\n  cancelled: \"pipeline-badge--pending\",\n};\n\nfunction RunList({ videoId, videoStatus }: { videoId: string; videoStatus: string }) {\n  const [runs, setRuns] = useState<PipelineRunItem[]>([]);\n  const [legacyCount, setLegacyCount] = useState(0);\n  const [loading, setLoading] = useState(true);\n  const [expandedRunId, setExpandedRunId] = useState<string | null>(null);\n  const [showLegacy, setShowLegacy] = useState(false);\n\n  const load = useCallback(async (silent = false) => {\n    if (!silent) setLoading(true);\n    try {\n      const res = await fetchPipelineRuns(videoId);\n      setRuns(res.items);\n      setLegacyCount(res.legacy_event_count);\n      // Auto-expand the latest run on first load\n      if (!silent && res.items.length > 0 && expandedRunId === null) {\n        const firstRun = res.items[0];\n        if (firstRun) setExpandedRunId(firstRun.id);\n      }\n    } catch {\n      // silently fail\n    } finally {\n      if (!silent) setLoading(false);\n    }\n  }, [videoId, expandedRunId]);\n\n  useEffect(() => {\n    void load();\n  }, [load]);\n\n  // Auto-refresh when video is processing\n  useEffect(() => {\n    if (videoStatus !== \"processing\" && videoStatus !== \"queued\") return;\n    const id = setInterval(() => void load(true), 10_000);\n    return () => clearInterval(id);\n  }, [videoStatus, load]);\n\n  if (loading) return <div className=\"loading\">Loading runs…</div>;\n\n  if (runs.length === 0 && legacyCount === 0) {\n    return <div className=\"pipeline-events__empty\">No pipeline runs recorded.</div>;\n  }\n\n  return (\n    <div className=\"run-list\">\n      {runs.map((run) => {\n        const isExpanded = expandedRunId === run.id;\n        return (\n          <div key={run.id} className={`run-card run-card--${run.status}`}>\n            <button\n              className=\"run-card__header\"\n              onClick={() => setExpandedRunId(isExpanded ? null : run.id)}\n              aria-expanded={isExpanded}\n            >\n              <span className=\"run-card__arrow\">{isExpanded ? \"▾\" : \"▸\"}</span>\n              <span className=\"run-card__number\">Run #{run.run_number}</span>\n              <span className=\"run-card__trigger\">{TRIGGER_LABELS[run.trigger] ?? run.trigger}</span>\n              <span className={`pipeline-badge ${RUN_STATUS_CLASS[run.status] ?? \"\"}`}>\n                {run.status}\n              </span>\n              <span className=\"run-card__time\">{formatDate(run.started_at)}</span>\n              {run.finished_at && (\n                <span className=\"run-card__duration\" title={`Finished: ${formatDate(run.finished_at)}`}>\n                  → {formatDate(run.finished_at)}\n                </span>\n              )}\n              <span className=\"run-card__tokens\">{formatTokens(run.total_tokens)} tokens</span>\n              <span className=\"run-card__events\">{run.event_count} events</span>\n              {run.error_stage && (\n                <span className=\"run-card__error-stage\">Failed at: {run.error_stage}</span>\n              )}\n            </button>\n            {isExpanded && (\n              <div className=\"run-card__body\">\n                <EventLog videoId={videoId} status={run.status} runId={run.id} />\n              </div>\n            )}\n          </div>\n        );\n      })}\n\n      {legacyCount > 0 && (\n        <div className=\"run-card run-card--legacy\">\n          <button\n            className=\"run-card__header\"\n            onClick={() => setShowLegacy((v) => !v)}\n            aria-expanded={showLegacy}\n          >\n            <span className=\"run-card__arrow\">{showLegacy ? \"▾\" : \"▸\"}</span>\n            <span className=\"run-card__number\">Legacy</span>\n            <span className=\"run-card__trigger\">Pre-run tracking</span>\n            <span className=\"run-card__events\">{legacyCount} events</span>\n          </button>\n          {showLegacy && (\n            <div className=\"run-card__body\">\n              <EventLog videoId={videoId} status=\"complete\" />\n            </div>\n          )}\n        </div>\n      )}\n    </div>\n  );\n}\n\n// ── Stage Timeline ───────────────────────────────────────────────────────────\n\nconst PIPELINE_STAGES = [\n  { key: \"stage2_segmentation\", label: \"Segment\" },\n  { key: \"stage3_extraction\", label: \"Extract\" },\n  { key: \"stage4_classification\", label: \"Classify\" },\n  { key: \"stage5_synthesis\", label: \"Synthesize\" },\n  { key: \"stage6_embed\", label: \"Embed\" },\n];\n\nconst StageTimeline = React.memo(function StageTimeline({ video }: { video: PipelineVideoItem }) {\n  const [, setTick] = useState(0);\n  const isProcessing = video.processing_status === \"processing\";\n\n  // Tick every second while processing to update elapsed time\n  useEffect(() => {\n    if (!isProcessing) return;\n    const id = setInterval(() => setTick((t) => t + 1), 1000);\n    return () => clearInterval(id);\n  }, [isProcessing]);\n\n  if (!isProcessing && video.processing_status !== \"complete\" && video.processing_status !== \"error\") {\n    return null;\n  }\n\n  const activeStage = video.active_stage;\n  const activeStatus = video.active_stage_status;\n  const activeIdx = PIPELINE_STAGES.findIndex((s) => s.key === activeStage);\n\n  return (\n    <div className=\"stage-timeline\">\n      {PIPELINE_STAGES.map((stage, i) => {\n        let stateClass = \"stage-timeline__step--future\";\n        if (activeIdx >= 0) {\n          if (i < activeIdx) {\n            stateClass = \"stage-timeline__step--done\";\n          } else if (i === activeIdx) {\n            if (activeStatus === \"start\") stateClass = \"stage-timeline__step--active\";\n            else if (activeStatus === \"complete\") stateClass = \"stage-timeline__step--done\";\n            else if (activeStatus === \"error\") stateClass = \"stage-timeline__step--error\";\n          }\n        }\n        if (video.processing_status === \"complete\") {\n          stateClass = \"stage-timeline__step--done\";\n        }\n\n        return (\n          <div key={stage.key} className={`stage-timeline__step ${stateClass}`}>\n            <div className=\"stage-timeline__dot\" title={stage.label} />\n            <span className=\"stage-timeline__label\">{stage.label}</span>\n          </div>\n        );\n      })}\n      {isProcessing && activeStatus === \"start\" && (\n        <span className=\"stage-timeline__elapsed\">{formatElapsed(video.stage_started_at)}</span>\n      )}\n      {isProcessing && video.latest_run && video.latest_run.total_tokens > 0 && (\n        <span className=\"stage-timeline__tokens\">{formatTokens(video.latest_run.total_tokens)} tok</span>\n      )}\n    </div>\n  );\n})\n\n// ── Recent Activity Feed ─────────────────────────────────────────────────────\n\nconst RecentActivityFeed = React.memo(function RecentActivityFeed() {\n  const [items, setItems] = useState<RecentActivityItem[]>([]);\n  const [collapsed, setCollapsed] = useState(false);\n  const [loading, setLoading] = useState(true);\n\n  const load = useCallback(async () => {\n    try {\n      const res = await fetchRecentActivity(8);\n      setItems(res.items);\n    } catch {\n      // silently fail\n    } finally {\n      setLoading(false);\n    }\n  }, []);\n\n  useEffect(() => {\n    void load();\n    const id = setInterval(() => void load(), 15_000);\n    return () => clearInterval(id);\n  }, [load]);\n\n  if (loading || items.length === 0) return null;\n\n  return (\n    <div className=\"recent-activity\">\n      <button\n        className=\"recent-activity__toggle\"\n        onClick={() => setCollapsed((v) => !v)}\n        aria-expanded={!collapsed}\n      >\n        <span className=\"recent-activity__title\">Recent Activity</span>\n        <span className=\"recent-activity__arrow\">{collapsed ? \"▸\" : \"▾\"}</span>\n      </button>\n      {!collapsed && (\n        <div className=\"recent-activity__list\">\n          {items.map((item) => (\n            <div key={item.id} className={`recent-activity__item recent-activity__item--${item.event_type}`}>\n              <span className=\"recent-activity__icon\">\n                {item.event_type === \"complete\" ? \"✓\" : \"✗\"}\n              </span>\n              <span className=\"recent-activity__stage\">{item.stage.replace(\"stage\", \"S\").replace(\"_\", \" \")}</span>\n              <span className=\"recent-activity__file\" title={item.filename}>\n                {item.filename}\n              </span>\n              <span className=\"recent-activity__creator\">{item.creator_name}</span>\n              {item.duration_ms != null && (\n                <span className=\"recent-activity__duration\">\n                  {item.duration_ms > 60000\n                    ? `${(item.duration_ms / 60000).toFixed(1)}m`\n                    : item.duration_ms > 1000\n                      ? `${(item.duration_ms / 1000).toFixed(1)}s`\n                      : `${item.duration_ms}ms`}\n                </span>\n              )}\n              <span className=\"recent-activity__time\">{formatDate(item.created_at)}</span>\n            </div>\n          ))}\n        </div>\n      )}\n    </div>\n  );\n});\n\n// ── Main Page ────────────────────────────────────────────────────────────────\n\ninterface BulkProgress {\n  total: number;\n  completed: number;\n  failed: number;\n  current: string | null;\n  active: boolean;\n}\n\ninterface BulkLogEntry {\n  filename: string;\n  ok: boolean;\n  message: string;\n}\n\n// ── Chunking Inspector ─────────────────────────────────────────────────────\n\nfunction ChunkingInspector({ videoId }: { videoId: string }) {\n  const [data, setData] = useState<Awaited<ReturnType<typeof fetchChunkingData>> | null>(null);\n  const [loading, setLoading] = useState(true);\n  const [error, setError] = useState<string | null>(null);\n  const [expanded, setExpanded] = useState(false);\n\n  useEffect(() => {\n    if (!expanded) return;\n    let cancelled = false;\n    setLoading(true);\n    fetchChunkingData(videoId)\n      .then((res) => { if (!cancelled) { setData(res); setError(null); } })\n      .catch((err) => { if (!cancelled) setError(err instanceof Error ? err.message : \"Failed\"); })\n      .finally(() => { if (!cancelled) setLoading(false); });\n    return () => { cancelled = true; };\n  }, [videoId, expanded]);\n\n  return (\n    <div className=\"chunking-inspector\">\n      <button\n        className=\"chunking-inspector__toggle\"\n        onClick={() => setExpanded(!expanded)}\n      >\n        {expanded ? \"▾\" : \"▸\"} Chunking Inspector\n      </button>\n\n      {expanded && loading && <div className=\"chunking-inspector__loading\">Loading chunking data...</div>}\n      {expanded && error && <div className=\"chunking-inspector__error\">{error}</div>}\n\n      {expanded && data && (\n        <div className=\"chunking-inspector__body\">\n          <div className=\"chunking-inspector__summary\">\n            <span>{data.total_segments} segments</span>\n            <span>{data.total_moments} moments</span>\n            <span>Classification: {data.classification_source}</span>\n            <span>Chunk size: {data.synthesis_chunk_size}</span>\n          </div>\n\n          {/* Topic Boundaries */}\n          <div className=\"chunking-inspector__section\">\n            <h4>Topic Boundaries (Stage 2)</h4>\n            <div className=\"chunking-inspector__topics\">\n              {data.topic_boundaries.map((tb, i) => (\n                <div key={i} className=\"chunking-topic\" title={tb.topic_label}>\n                  <span className=\"chunking-topic__label\">{tb.topic_label}</span>\n                  <span className=\"chunking-topic__meta\">\n                    {tb.segment_count} segs &middot; {tb.start_time.toFixed(0)}s-{tb.end_time.toFixed(0)}s\n                  </span>\n                </div>\n              ))}\n            </div>\n          </div>\n\n          {/* Synthesis Groups */}\n          <div className=\"chunking-inspector__section\">\n            <h4>Synthesis Groups (Stage 5 Input)</h4>\n            <div className=\"chunking-inspector__groups\">\n              {data.synthesis_groups.map((g) => (\n                <div\n                  key={g.category}\n                  className={`chunking-group ${g.exceeds_chunk_threshold ? \"chunking-group--split\" : \"\"}`}\n                >\n                  <span className=\"chunking-group__category\">{g.category}</span>\n                  <span className=\"chunking-group__count\">{g.moment_count} moments</span>\n                  {g.exceeds_chunk_threshold && (\n                    <span className=\"chunking-group__warn\">\n                      Will split into {g.chunks_needed} chunks\n                    </span>\n                  )}\n                </div>\n              ))}\n            </div>\n          </div>\n        </div>\n      )}\n    </div>\n  );\n}\n\n\nexport default function AdminPipeline() {\n  useDocumentTitle(\"Pipeline Management — Chrysopedia\");\n  const [searchParams] = useSearchParams();\n  const [videos, setVideos] = useState<PipelineVideoItem[]>([]);\n  const [loading, setLoading] = useState(true);\n  const [error, setError] = useState<string | null>(null);\n  const [expandedId, setExpandedId] = useState<string | null>(null);\n  const [actionLoading, setActionLoading] = useState<string | null>(null);\n  const [actionMessage, setActionMessage] = useState<{ id: string; text: string; ok: boolean } | null>(null);\n  const [activeFilter, setActiveFilter] = useState<string | null>(null);\n  const [debugMode, setDebugModeState] = useState<boolean | null>(null);\n  const [creators, setCreators] = useState<{ name: string; slug: string }[]>([]);\n  const [creatorFilter, setCreatorFilter] = useState<string | null>(null);\n  const [searchQuery, setSearchQuery] = useState(\"\");\n  const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());\n  // Re-run stage modal state\n  const [rerunModalVideo, setRerunModalVideo] = useState<string | null>(null);\n  const [rerunStageSelect, setRerunStageSelect] = useState(\"stage5_synthesis\");\n  const [rerunPromptOverride, setRerunPromptOverride] = useState(\"\");\n  // Stale pages state\n  const [stalePagesCount, setStalePagesCount] = useState<number | null>(null);\n  const [bulkProgress, setBulkProgress] = useState<BulkProgress | null>(null);\n  const [bulkLog, setBulkLog] = useState<BulkLogEntry[]>([]);\n  const [changedIds, setChangedIds] = useState<Set<string>>(new Set());\n  const [autoRefresh, setAutoRefresh] = useState(true);\n  const bulkCancelRef = useRef(false);\n  const prevStatusRef = useRef<Map<string, string>>(new Map());\n  const videoRefs = useRef<Map<string, HTMLDivElement>>(new Map());\n  const deepLinked = useRef(false);\n\n  // Compute filtered list (status + creator) — memoized to avoid recomputing on unrelated state changes\n  const filteredVideos = useMemo(() => videos.filter((v) => {\n    if (activeFilter !== null && v.processing_status !== activeFilter) return false;\n    if (creatorFilter !== null && v.creator_name !== creatorFilter) return false;\n    if (searchQuery) {\n      const q = searchQuery.toLowerCase();\n      if (!v.filename.toLowerCase().includes(q) && !v.creator_name.toLowerCase().includes(q)) return false;\n    }\n    return true;\n  }), [videos, activeFilter, creatorFilter, searchQuery]);\n\n  const load = useCallback(async (isAutoRefresh = false) => {\n    if (!isAutoRefresh) setLoading(true);\n    setError(null);\n    try {\n      const res = await fetchPipelineVideos();\n      // Detect status changes for highlight animation\n      if (prevStatusRef.current.size > 0) {\n        const changed = new Set<string>();\n        for (const v of res.items) {\n          const prev = prevStatusRef.current.get(v.id);\n          if (prev && prev !== v.processing_status) {\n            changed.add(v.id);\n          }\n        }\n        if (changed.size > 0) {\n          setChangedIds(changed);\n          // Clear highlights after animation\n          setTimeout(() => setChangedIds(new Set()), 2000);\n        }\n      }\n      // Store current statuses for next comparison\n      const statusMap = new Map<string, string>();\n      for (const v of res.items) {\n        statusMap.set(v.id, v.processing_status);\n      }\n      prevStatusRef.current = statusMap;\n      setVideos(res.items);\n    } catch (err) {\n      if (!isAutoRefresh) {\n        setError(err instanceof Error ? err.message : \"Failed to load videos\");\n      }\n    } finally {\n      if (!isAutoRefresh) setLoading(false);\n    }\n  }, []);\n\n  useEffect(() => {\n    void load();\n  }, [load]);\n\n  // Auto-refresh every 15 seconds\n  useEffect(() => {\n    if (!autoRefresh) return;\n    const id = setInterval(() => void load(true), 15_000);\n    return () => clearInterval(id);\n  }, [autoRefresh, load]);\n\n  // Load creators for filter dropdown\n  useEffect(() => {\n    fetchCreators({ limit: 200 })\n      .then((res) => {\n        const list = res.items.map((c: { name: string; slug: string }) => ({\n          name: c.name,\n          slug: c.slug,\n        }));\n        list.sort((a: { name: string }, b: { name: string }) => a.name.localeCompare(b.name));\n        setCreators(list);\n      })\n      .catch(() => {\n        // silently fail — filter stays hidden\n      });\n  }, []);\n\n  // Load stale page count\n  useEffect(() => {\n    fetchStalePages()\n      .then((res) => setStalePagesCount(res.stale_pages))\n      .catch(() => { /* silently fail */ });\n  }, []);\n\n  const handleBulkResynth = async () => {\n    if (!confirm(\"Re-run stage 5 synthesis on all completed videos with the current prompt?\")) return;\n    try {\n      const res = await bulkResynthesize();\n      setActionMessage({\n        id: \"__bulk__\",\n        text: `Bulk re-synthesize dispatched: ${res.dispatched}/${res.total} videos`,\n        ok: true,\n      });\n      setStalePagesCount(null); // will refresh\n    } catch (err) {\n      setActionMessage({\n        id: \"__bulk__\",\n        text: err instanceof Error ? err.message : \"Bulk re-synth failed\",\n        ok: false,\n      });\n    }\n  };\n\n  const handleWipeAll = async () => {\n    if (!confirm(\"WIPE ALL pipeline output? This deletes all technique pages, key moments, pipeline events, runs, and Qdrant vectors. Creators, videos, and transcripts are preserved. This cannot be undone.\")) return;\n    if (!confirm(\"Are you sure? This is irreversible.\")) return;\n    try {\n      const res = await wipeAllOutput();\n      setActionMessage({\n        id: \"__wipe__\",\n        text: `Wiped: ${JSON.stringify(res.deleted)}`,\n        ok: true,\n      });\n      setStalePagesCount(null);\n      void load();\n    } catch (err) {\n      setActionMessage({\n        id: \"__wipe__\",\n        text: err instanceof Error ? err.message : \"Wipe failed\",\n        ok: false,\n      });\n    }\n  };\n\n  // Deep-link: auto-expand and scroll to ?video=<id> on first load\n  useEffect(() => {\n    if (deepLinked.current || loading || videos.length === 0) return;\n    const targetVideoId = searchParams.get(\"video\");\n    if (!targetVideoId) return;\n    const match = videos.find((v) => v.id === targetVideoId);\n    if (!match) return;\n    deepLinked.current = true;\n    setExpandedId(targetVideoId);\n    requestAnimationFrame(() => {\n      const el = videoRefs.current.get(targetVideoId);\n      if (el) el.scrollIntoView({ behavior: \"smooth\", block: \"start\" });\n    });\n  }, [loading, videos, searchParams]);\n\n  useEffect(() => {\n    let cancelled = false;\n    fetchDebugMode()\n      .then((res) => {\n        if (!cancelled) setDebugModeState(res.debug_mode);\n      })\n      .catch(() => {});\n    return () => { cancelled = true; };\n  }, []);\n\n  // Clear selection when filters change\n  useEffect(() => {\n    setSelectedIds(new Set());\n  }, [activeFilter, creatorFilter, searchQuery]);\n\n  const handleTrigger = async (videoId: string) => {\n    setActionLoading(videoId);\n    setActionMessage(null);\n    try {\n      const res = await cleanRetriggerPipeline(videoId);\n      setActionMessage({ id: videoId, text: `Retriggered (${res.status})`, ok: true });\n      setTimeout(() => void load(), 2000);\n    } catch (err) {\n      setActionMessage({\n        id: videoId,\n        text: err instanceof Error ? err.message : \"Retrigger failed\",\n        ok: false,\n      });\n    } finally {\n      setActionLoading(null);\n    }\n  };\n\n  const handleRevoke = async (videoId: string) => {\n    setActionLoading(videoId);\n    setActionMessage(null);\n    try {\n      const res = await revokePipeline(videoId);\n      setActionMessage({\n        id: videoId,\n        text: res.tasks_revoked > 0\n          ? `Revoked ${res.tasks_revoked} task${res.tasks_revoked !== 1 ? \"s\" : \"\"}`\n          : \"No active tasks\",\n        ok: true,\n      });\n      setTimeout(() => void load(), 2000);\n    } catch (err) {\n      setActionMessage({\n        id: videoId,\n        text: err instanceof Error ? err.message : \"Revoke failed\",\n        ok: false,\n      });\n    } finally {\n      setActionLoading(null);\n    }\n  };\n\n  const handleRerunStage = async (videoId: string) => {\n    setActionLoading(videoId);\n    setActionMessage(null);\n    try {\n      const res = await rerunStage(\n        videoId,\n        rerunStageSelect,\n        rerunPromptOverride.trim() || undefined,\n      );\n      setActionMessage({\n        id: videoId,\n        text: `Stage re-run dispatched: ${res.stage}${res.prompt_override ? \" (with prompt override)\" : \"\"}`,\n        ok: true,\n      });\n      setRerunModalVideo(null);\n      setRerunPromptOverride(\"\");\n      setTimeout(() => void load(), 2000);\n    } catch (err) {\n      setActionMessage({\n        id: videoId,\n        text: err instanceof Error ? err.message : \"Stage re-run failed\",\n        ok: false,\n      });\n    } finally {\n      setActionLoading(null);\n    }\n  };\n\n  const toggleExpand = (id: string) => {\n    setExpandedId((prev) => (prev === id ? null : id));\n  };\n\n  // ── Selection ───────────────────────────────────────────────────────────\n\n  const toggleSelect = (id: string) => {\n    setSelectedIds((prev) => {\n      const next = new Set(prev);\n      if (next.has(id)) next.delete(id);\n      else next.add(id);\n      return next;\n    });\n  };\n\n  const toggleSelectAll = () => {\n    const visibleIds = filteredVideos.map((v) => v.id);\n    const allSelected = visibleIds.every((id) => selectedIds.has(id));\n    if (allSelected) {\n      setSelectedIds(new Set());\n    } else {\n      setSelectedIds(new Set(visibleIds));\n    }\n  };\n\n  const allVisibleSelected = filteredVideos.length > 0 && filteredVideos.every((v) => selectedIds.has(v.id));\n\n  // ── Bulk Actions ────────────────────────────────────────────────────────\n\n  const runBulk = async (clean: boolean) => {\n    const ids = Array.from(selectedIds);\n    if (ids.length === 0) return;\n\n    bulkCancelRef.current = false;\n    setBulkLog([]);\n    setBulkProgress({ total: ids.length, completed: 0, failed: 0, current: null, active: true });\n\n    let completed = 0;\n    let failed = 0;\n\n    for (const id of ids) {\n      if (bulkCancelRef.current) break;\n\n      const video = videos.find((v) => v.id === id);\n      const filename = video?.filename ?? id;\n      setBulkProgress((p) => p ? { ...p, current: filename } : null);\n\n      try {\n        if (clean) {\n          await cleanRetriggerPipeline(id);\n        } else {\n          await triggerPipeline(id);\n        }\n        completed++;\n        setBulkLog((prev) => [...prev, { filename, ok: true, message: clean ? \"clean retriggered\" : \"triggered\" }]);\n      } catch (err) {\n        failed++;\n        setBulkLog((prev) => [...prev, {\n          filename,\n          ok: false,\n          message: err instanceof Error ? err.message : \"failed\",\n        }]);\n      }\n\n      setBulkProgress((p) => p ? { ...p, completed: completed, failed } : null);\n\n      // Brief pause between dispatches to avoid slamming the API\n      if (!bulkCancelRef.current && ids.indexOf(id) < ids.length - 1) {\n        await new Promise((r) => setTimeout(r, 500));\n      }\n    }\n\n    setBulkProgress((p) => p ? { ...p, active: false, current: null } : null);\n    setSelectedIds(new Set());\n    // Refresh video list after bulk completes\n    setTimeout(() => void load(), 2000);\n  };\n\n  const cancelBulk = () => {\n    bulkCancelRef.current = true;\n  };\n\n  return (\n    <div className=\"admin-pipeline\">\n      <div className=\"admin-pipeline__header\">\n        <div>\n          <h1 className=\"admin-pipeline__title\">Pipeline Management</h1>\n          <p className=\"admin-pipeline__subtitle\">\n            {videos.length} video{videos.length !== 1 ? \"s\" : \"\"}\n          </p>\n        </div>\n        <div className=\"admin-pipeline__header-right\">\n          {stalePagesCount !== null && stalePagesCount > 0 && (\n            <button\n              className=\"btn btn--small btn--warning\"\n              onClick={() => void handleBulkResynth()}\n              title={`${stalePagesCount} pages synthesized with an older prompt`}\n            >\n              {stalePagesCount} stale pages\n            </button>\n          )}\n          <button\n            className=\"btn btn--small btn--danger\"\n            onClick={() => void handleWipeAll()}\n            title=\"Delete all pipeline output (technique pages, moments, events). Preserves videos and transcripts.\"\n          >\n            Wipe All Output\n          </button>\n          <DebugModeToggle debugMode={debugMode} onDebugModeChange={setDebugModeState} />\n          <WorkerStatus />\n          <label className=\"auto-refresh-toggle\" title=\"Auto-refresh video list every 15 seconds\">\n            <input\n              type=\"checkbox\"\n              checked={autoRefresh}\n              onChange={(e) => setAutoRefresh(e.target.checked)}\n            />\n            Auto-refresh\n          </label>\n          <button className=\"btn btn--secondary\" onClick={() => void load()} disabled={loading}>\n            ↻ Refresh\n          </button>\n        </div>\n      </div>\n\n      {loading ? (\n        <div className=\"loading\">Loading videos…</div>\n      ) : error ? (\n        <div className=\"loading error-text\">Error: {error}</div>\n      ) : videos.length === 0 ? (\n        <div className=\"empty-state\">No videos in pipeline.</div>\n      ) : (\n        <>\n          <RecentActivityFeed />\n          <div className=\"admin-pipeline__filters\">\n            <StatusFilter\n              videos={videos}\n              activeFilter={activeFilter}\n              onFilterChange={setActiveFilter}\n            />\n            {creators.length > 1 && (\n              <div className=\"creator-filter\">\n                <select\n                  className=\"creator-filter__select\"\n                  value={creatorFilter ?? \"\"}\n                  onChange={(e) => setCreatorFilter(e.target.value || null)}\n                >\n                  <option value=\"\">All Creators</option>\n                  {creators.map((c) => (\n                    <option key={c.slug} value={c.name}>{c.name}</option>\n                  ))}\n                </select>\n              </div>\n            )}\n            <div className=\"pipeline-search\">\n              <input\n                type=\"text\"\n                className=\"pipeline-search__input\"\n                placeholder=\"Filter by filename or creator…\"\n                value={searchQuery}\n                onChange={(e) => setSearchQuery(e.target.value)}\n              />\n              {searchQuery && (\n                <button\n                  className=\"pipeline-search__clear\"\n                  onClick={() => setSearchQuery(\"\")}\n                  aria-label=\"Clear search\"\n                >\n                  ✕\n                </button>\n              )}\n            </div>\n          </div>\n\n          {/* Bulk toolbar */}\n          {selectedIds.size > 0 && (\n            <div className=\"bulk-toolbar\">\n              <span className=\"bulk-toolbar__count\">\n                {selectedIds.size} selected\n              </span>\n              {bulkProgress?.active ? (\n                <>\n                  <div className=\"bulk-toolbar__progress\">\n                    <div className=\"bulk-toolbar__progress-bar\">\n                      <div\n                        className=\"bulk-toolbar__progress-fill\"\n                        style={{ width: `${((bulkProgress.completed + bulkProgress.failed) / bulkProgress.total) * 100}%` }}\n                      />\n                    </div>\n                    <span className=\"bulk-toolbar__progress-text\">\n                      {bulkProgress.completed + bulkProgress.failed}/{bulkProgress.total}\n                      {bulkProgress.failed > 0 && <span className=\"error-text\"> ({bulkProgress.failed} failed)</span>}\n                    </span>\n                    {bulkProgress.current && (\n                      <span className=\"bulk-toolbar__current\" title={bulkProgress.current}>\n                        {bulkProgress.current}\n                      </span>\n                    )}\n                  </div>\n                  <button className=\"btn btn--small btn--danger\" onClick={cancelBulk}>\n                    Cancel\n                  </button>\n                </>\n              ) : (\n                <>\n                  <button\n                    className=\"btn btn--small btn--primary\"\n                    onClick={() => void runBulk(false)}\n                    title=\"Retrigger pipeline for selected videos\"\n                  >\n                    ▶ Retrigger Selected\n                  </button>\n                  <button\n                    className=\"btn btn--small btn--warning\"\n                    onClick={() => void runBulk(true)}\n                    title=\"Wipe pipeline output (events, moments, segments) then retrigger\"\n                  >\n                    🧹 Clean Reprocess\n                  </button>\n                  <button\n                    className=\"btn btn--small btn--secondary\"\n                    onClick={() => setSelectedIds(new Set())}\n                  >\n                    Clear\n                  </button>\n                </>\n              )}\n            </div>\n          )}\n\n          {/* Bulk completion message */}\n          {bulkProgress && !bulkProgress.active && (\n            <div className={`bulk-toolbar__done ${bulkProgress.failed > 0 ? \"bulk-toolbar__done--warn\" : \"bulk-toolbar__done--ok\"}`}>\n              Bulk operation complete: {bulkProgress.completed} succeeded\n              {bulkProgress.failed > 0 && `, ${bulkProgress.failed} failed`}\n              {bulkCancelRef.current && \" (cancelled)\"}\n              <button className=\"bulk-toolbar__dismiss\" onClick={() => setBulkProgress(null)}>✕</button>\n            </div>\n          )}\n\n          {/* Bulk operation log */}\n          {bulkLog.length > 0 && (bulkProgress?.active || (bulkProgress && !bulkProgress.active)) && (\n            <div className=\"bulk-log\">\n              <div className=\"bulk-log__list\">\n                {bulkLog.map((entry, i) => (\n                  <div key={i} className={`bulk-log__entry ${entry.ok ? \"bulk-log__entry--ok\" : \"bulk-log__entry--err\"}`}>\n                    <span className=\"bulk-log__icon\">{entry.ok ? \"✓\" : \"✗\"}</span>\n                    <span className=\"bulk-log__file\">{entry.filename}</span>\n                    <span className=\"bulk-log__msg\">{entry.message}</span>\n                  </div>\n                ))}\n              </div>\n            </div>\n          )}\n\n          <div className=\"admin-pipeline__list\">\n            {filteredVideos.map((video) => (\n            <div key={video.id} className={`pipeline-video${changedIds.has(video.id) ? \" pipeline-video--changed\" : \"\"}`} ref={(el) => { if (el) videoRefs.current.set(video.id, el); }}>\n              <div\n                className=\"pipeline-video__header\"\n                onClick={() => toggleExpand(video.id)}\n              >\n                <div className=\"pipeline-video__checkbox\" onClick={(e) => e.stopPropagation()}>\n                  <input\n                    type=\"checkbox\"\n                    checked={selectedIds.has(video.id)}\n                    onChange={() => toggleSelect(video.id)}\n                    disabled={bulkProgress?.active ?? false}\n                    aria-label={`Select ${video.filename}`}\n                  />\n                </div>\n                <div className=\"pipeline-video__info\">\n                  <span className=\"pipeline-video__filename\" title={video.filename}>\n                    {video.filename}\n                  </span>\n                  <span className=\"pipeline-video__creator\">{video.creator_name}</span>\n                </div>\n\n                <div className=\"pipeline-video__meta\">\n                  <span className={`pipeline-badge ${statusBadgeClass(video.processing_status)}`}>","truncated":true,"truncatedBy":"bytes","totalLines":1523,"totalBytes":56779,"outputLines":1390,"outputBytes":51142,"lastLinePartial":false,"firstLineExceedsLimit":false,"maxLines":2000,"maxBytes":51200}},"isError":false,"timestamp":1775180883586}}
{"type":"message","id":"43b1fb21","parentId":"d02307d0","timestamp":"2026-04-03T01:48:12.666Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"Let me see the rest of this file and check what models/schemas are relevant for multi-source management.","thinkingSignature":"EqMCClkIDBgCKkD9oQFE/fNxCFMUZ/iRBVoznAUYvcJyvPL6BNxx5CeHI2bwEX4gleN94N6pn6mlz9Mk7UEYhK2kuI1UPI1ZFMToMg9jbGF1ZGUtb3B1cy00LTY4ABIM5JGuLLSucbmvU1ajGgwBCrOeYBv6LyUpYcUiMFltAwdcMEqzUfxV+kMvKQ0zEjoMNH4BCBcjsydtSVcomG4fWii6Jt+7SLiSgWCMtCp42gLG8/qBrgmKihdFgO32kUlboFmlxposOczHJUSu1qJDcqxjskfxC4VgsgQELVqAZ8OP9C/Njy+qdUBY3K3MXKM+pfJSATbKvkm/0xJFEwo8QWS7ZSMo95xyIGPwwsNFAP7rta821azJ32bvhlswQr70Nqx6sVNkGAE="},{"type":"text","text":"Now let me check the TechniquePageVideo model and the relevant schemas, plus what the frontend API client exposes."},{"type":"toolCall","id":"toolu_01V6bMdrmckHi2cZwGFu161w","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'TechniquePageVideo\\|technique_page_videos\\|source_video_links\\|body_sections_format' backend/models.py | head -20"}},{"type":"toolCall","id":"toolu_01LPnH9sETTQBRUnwKMb5qrC","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'source_videos\\|SourceVideoSummary\\|body_sections_format\\|TechniquePageDetail' backend/schemas.py | head -20"}},{"type":"toolCall","id":"toolu_01X6jPDH3RkjEWEp8dhPfoNt","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'compose\\|Compose\\|composition\\|multi.source\\|source_video' backend/routers/techniques.py | head -20"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":353,"cacheRead":68310,"cacheWrite":27580,"totalTokens":96244,"cost":{"input":0.000005,"output":0.008825000000000001,"cacheRead":0.034155,"cacheWrite":0.172375,"total":0.21536}},"stopReason":"toolUse","timestamp":1775180883591}}
{"type":"message","id":"e1852aaa","parentId":"43b1fb21","timestamp":"2026-04-03T01:48:12.689Z","message":{"role":"toolResult","toolCallId":"toolu_01V6bMdrmckHi2cZwGFu161w","toolName":"bash","content":[{"type":"text","text":"219:    body_sections_format: Mapped[str] = mapped_column(\n250:    source_video_links: Mapped[list[TechniquePageVideo]] = sa_relationship(\n312:class TechniquePageVideo(Base):\n314:    __tablename__ = \"technique_page_videos\"\n332:        back_populates=\"source_video_links\"\n"}],"isError":false,"timestamp":1775180892688}}
{"type":"message","id":"310a1dfb","parentId":"e1852aaa","timestamp":"2026-04-03T01:48:12.689Z","message":{"role":"toolResult","toolCallId":"toolu_01LPnH9sETTQBRUnwKMb5qrC","toolName":"bash","content":[{"type":"text","text":"126:    body_sections_format: str = \"v1\"\n279:class SourceVideoSummary(BaseModel):\n289:class TechniquePageDetail(TechniquePageRead):\n295:    source_videos: list[SourceVideoSummary] = Field(default_factory=list)\n"}],"isError":false,"timestamp":1775180892688}}
{"type":"message","id":"b99493fd","parentId":"310a1dfb","timestamp":"2026-04-03T01:48:12.695Z","message":{"role":"toolResult","toolCallId":"toolu_01X6jPDH3RkjEWEp8dhPfoNt","toolName":"bash","content":[{"type":"text","text":"219:            selectinload(TechniquePage.key_moments).selectinload(KeyMoment.source_video),\n227:            selectinload(TechniquePage.source_video_links).selectinload(\n228:                TechniquePageVideo.source_video\n243:        item.video_filename = km.source_video.filename if km.source_video else \"\"\n303:    source_videos = [\n305:            id=link.source_video.id,\n306:            filename=link.source_video.filename,\n307:            content_type=link.source_video.content_type.value if hasattr(link.source_video.content_type, 'value') else str(link.source_video.content_type),\n310:        for link in page.source_video_links\n311:        if link.source_video is not None\n320:        source_videos=source_videos,\n"}],"isError":false,"timestamp":1775180892692}}
{"type":"message","id":"ff5804a3","parentId":"b99493fd","timestamp":"2026-04-03T01:48:17.278Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_01DyVkVoTf3FSSf7w8i5nsnE","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'TechniquePageVersion\\|pipeline_metadata\\|content_snapshot' backend/models.py | head -15"}},{"type":"toolCall","id":"toolu_017FzAfBWDP5GeesyrF2hrrN","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -rn 'composition\\|compose.*history\\|compose.*log' backend/ --include='*.py' | head -15"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":180,"cacheRead":95890,"cacheWrite":784,"totalTokens":96855,"cost":{"input":0.000005,"output":0.0045000000000000005,"cacheRead":0.047944999999999995,"cacheWrite":0.0049,"total":0.05735}},"stopReason":"toolUse","timestamp":1775180892693}}
{"type":"message","id":"9f15f949","parentId":"ff5804a3","timestamp":"2026-04-03T01:48:17.292Z","message":{"role":"toolResult","toolCallId":"toolu_01DyVkVoTf3FSSf7w8i5nsnE","toolName":"bash","content":[{"type":"text","text":"241:    versions: Mapped[list[TechniquePageVersion]] = sa_relationship(\n242:        back_populates=\"technique_page\", order_by=\"TechniquePageVersion.version_number\"\n282:class TechniquePageVersion(Base):\n291:    content_snapshot: Mapped[dict] = mapped_column(JSONB, nullable=False)\n292:    pipeline_metadata: Mapped[dict | None] = mapped_column(JSONB, nullable=True)\n"}],"isError":false,"timestamp":1775180897292}}
{"type":"message","id":"b2b2c656","parentId":"9f15f949","timestamp":"2026-04-03T01:48:17.300Z","message":{"role":"toolResult","toolCallId":"toolu_017FzAfBWDP5GeesyrF2hrrN","toolName":"bash","content":[{"type":"text","text":"backend/pipeline/test_harness.py:338:    \"\"\"Build the user prompt for composition (merging new moments into an existing page).\nbackend/pipeline/test_harness.py:385:    \"\"\"Run composition: merge new fixture moments into an existing page.\nbackend/pipeline/test_compose_pipeline.py:1:\"\"\"Unit tests for compose pipeline logic in stage5_synthesis.\nbackend/routers/pipeline.py:1121:    Use after changing embedding text composition or Qdrant payload fields\n"}],"isError":false,"timestamp":1775180897297}}
{"type":"message","id":"f2b34332","parentId":"b2b2c656","timestamp":"2026-04-03T01:48:22.463Z","message":{"role":"assistant","content":[{"type":"text","text":"Let me check the roadmap demo text for S06 more carefully, and what the existing API already provides in terms of multi-source info."},{"type":"toolCall","id":"toolu_0141cGRWVnJcfUigTjyEF76A","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'source_video' backend/routers/techniques.py | head -20"}},{"type":"toolCall","id":"toolu_01Xr1oRtGwE5LJiqmr88u78u","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -rn 'TechniquePageVersion' backend/routers/ --include='*.py' | head -10"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":199,"cacheRead":96674,"cacheWrite":474,"totalTokens":97348,"cost":{"input":0.000005,"output":0.004975,"cacheRead":0.048337,"cacheWrite":0.0029625000000000003,"total":0.056279499999999996}},"stopReason":"toolUse","timestamp":1775180897298}}
{"type":"message","id":"e6964091","parentId":"f2b34332","timestamp":"2026-04-03T01:48:22.480Z","message":{"role":"toolResult","toolCallId":"toolu_0141cGRWVnJcfUigTjyEF76A","toolName":"bash","content":[{"type":"text","text":"219:            selectinload(TechniquePage.key_moments).selectinload(KeyMoment.source_video),\n227:            selectinload(TechniquePage.source_video_links).selectinload(\n228:                TechniquePageVideo.source_video\n243:        item.video_filename = km.source_video.filename if km.source_video else \"\"\n303:    source_videos = [\n305:            id=link.source_video.id,\n306:            filename=link.source_video.filename,\n307:            content_type=link.source_video.content_type.value if hasattr(link.source_video.content_type, 'value') else str(link.source_video.content_type),\n310:        for link in page.source_video_links\n311:        if link.source_video is not None\n320:        source_videos=source_videos,\n"}],"isError":false,"timestamp":1775180902479}}
{"type":"message","id":"6c6f741c","parentId":"e6964091","timestamp":"2026-04-03T01:48:22.486Z","message":{"role":"toolResult","toolCallId":"toolu_01Xr1oRtGwE5LJiqmr88u78u","toolName":"bash","content":[{"type":"text","text":"backend/routers/techniques.py:14:from models import Creator, KeyMoment, RelatedTechniqueLink, SourceVideo, TechniquePage, TechniquePageVersion, TechniquePageVideo\nbackend/routers/techniques.py:23:    TechniquePageVersionDetail,\nbackend/routers/techniques.py:24:    TechniquePageVersionListResponse,\nbackend/routers/techniques.py:25:    TechniquePageVersionSummary,\nbackend/routers/techniques.py:297:        TechniquePageVersion.technique_page_id == page.id\nbackend/routers/techniques.py:324:@router.get(\"/{slug}/versions\", response_model=TechniquePageVersionListResponse)\nbackend/routers/techniques.py:328:) -> TechniquePageVersionListResponse:\nbackend/routers/techniques.py:339:        select(TechniquePageVersion)\nbackend/routers/techniques.py:340:        .where(TechniquePageVersion.technique_page_id == page.id)\nbackend/routers/techniques.py:341:        .order_by(TechniquePageVersion.version_number.desc())\n"}],"isError":false,"timestamp":1775180902483}}
{"type":"message","id":"84e87cec","parentId":"6c6f741c","timestamp":"2026-04-03T01:48:26.053Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_013URkXSS87a8ZmjEtZP1JJn","name":"read","arguments":{"limit":100,"offset":280,"path":"backend/routers/techniques.py"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":92,"cacheRead":97148,"cacheWrite":769,"totalTokens":98010,"cost":{"input":0.000005,"output":0.0023,"cacheRead":0.048574,"cacheWrite":0.004806250000000001,"total":0.05568525}},"stopReason":"toolUse","timestamp":1775180902484}}
{"type":"message","id":"3362e2de","parentId":"84e87cec","timestamp":"2026-04-03T01:48:26.058Z","message":{"role":"toolResult","toolCallId":"toolu_013URkXSS87a8ZmjEtZP1JJn","toolName":"read","content":[{"type":"text","text":"        remaining = max_related - len(related_links)\n        try:\n            dynamic_links = await _find_dynamic_related(\n                db, page, curated_slugs, remaining\n            )\n            related_links.extend(dynamic_links)\n        except Exception:\n            logger.warning(\n                \"Dynamic related query failed for %s, continuing with curated only\",\n                slug,\n                exc_info=True,\n            )\n\n    base = TechniquePageRead.model_validate(page)\n\n    # Count versions for this page\n    version_count_stmt = select(func.count()).where(\n        TechniquePageVersion.technique_page_id == page.id\n    )\n    version_count_result = await db.execute(version_count_stmt)\n    version_count = version_count_result.scalar() or 0\n\n    # Build source video list from association table\n    source_videos = [\n        SourceVideoSummary(\n            id=link.source_video.id,\n            filename=link.source_video.filename,\n            content_type=link.source_video.content_type.value if hasattr(link.source_video.content_type, 'value') else str(link.source_video.content_type),\n            added_at=link.added_at,\n        )\n        for link in page.source_video_links\n        if link.source_video is not None\n    ]\n\n    return TechniquePageDetail(\n        **base.model_dump(),\n        key_moments=key_moment_items,\n        creator_info=creator_info,\n        related_links=related_links,\n        version_count=version_count,\n        source_videos=source_videos,\n    )\n\n\n@router.get(\"/{slug}/versions\", response_model=TechniquePageVersionListResponse)\nasync def list_technique_versions(\n    slug: str,\n    db: AsyncSession = Depends(get_session),\n) -> TechniquePageVersionListResponse:\n    \"\"\"List all version snapshots for a technique page, newest first.\"\"\"\n    # Resolve the technique page\n    page_stmt = select(TechniquePage).where(TechniquePage.slug == slug)\n    page_result = await db.execute(page_stmt)\n    page = page_result.scalar_one_or_none()\n    if page is None:\n        raise HTTPException(status_code=404, detail=f\"Technique '{slug}' not found\")\n\n    # Fetch versions ordered by version_number DESC\n    versions_stmt = (\n        select(TechniquePageVersion)\n        .where(TechniquePageVersion.technique_page_id == page.id)\n        .order_by(TechniquePageVersion.version_number.desc())\n    )\n    versions_result = await db.execute(versions_stmt)\n    versions = versions_result.scalars().all()\n\n    items = [TechniquePageVersionSummary.model_validate(v) for v in versions]\n    return TechniquePageVersionListResponse(items=items, total=len(items))\n\n\n@router.get(\"/{slug}/versions/{version_number}\", response_model=TechniquePageVersionDetail)\nasync def get_technique_version(\n    slug: str,\n    version_number: int,\n    db: AsyncSession = Depends(get_session),\n) -> TechniquePageVersionDetail:\n    \"\"\"Get a specific version snapshot by version number.\"\"\"\n    # Resolve the technique page\n    page_stmt = select(TechniquePage).where(TechniquePage.slug == slug)\n    page_result = await db.execute(page_stmt)\n    page = page_result.scalar_one_or_none()\n    if page is None:\n        raise HTTPException(status_code=404, detail=f\"Technique '{slug}' not found\")\n\n    # Fetch the specific version\n    version_stmt = (\n        select(TechniquePageVersion)\n        .where(\n            TechniquePageVersion.technique_page_id == page.id,\n            TechniquePageVersion.version_number == version_number,\n        )\n    )\n    version_result = await db.execute(version_stmt)\n    version = version_result.scalar_one_or_none()\n    if version is None:\n        raise HTTPException(\n            status_code=404,\n            detail=f\"Version {version_number} not found for technique '{slug}'\",\n        )\n\n\n[2 more lines in file. Use offset=380 to continue.]"}],"isError":false,"timestamp":1775180906056}}
{"type":"message","id":"b937dd2b","parentId":"3362e2de","timestamp":"2026-04-03T01:48:31.749Z","message":{"role":"assistant","content":[{"type":"text","text":"Now let me check the frontend API client for existing technique/composition-related fetchers."},{"type":"toolCall","id":"toolu_01FM8QzeAGcCyXmFfQFWcGKS","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'source_video\\|composition\\|compose\\|multi.source\\|chunking\\|TechniquePage\\|technique.*version' frontend/src/api/public-client.ts | head -25"}},{"type":"toolCall","id":"toolu_013UrhamWAbP5PF2i4aXk8yF","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'fetchChunking\\|fetchStale\\|PipelineVideoItem' frontend/src/api/public-client.ts | head -10"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":225,"cacheRead":97917,"cacheWrite":1191,"totalTokens":99334,"cost":{"input":0.000005,"output":0.005625000000000001,"cacheRead":0.048958499999999995,"cacheWrite":0.007443750000000001,"total":0.06203225}},"stopReason":"toolUse","timestamp":1775180906056}}
{"type":"message","id":"7684264a","parentId":"b937dd2b","timestamp":"2026-04-03T01:48:31.763Z","message":{"role":"toolResult","toolCallId":"toolu_01FM8QzeAGcCyXmFfQFWcGKS","toolName":"bash","content":[{"type":"text","text":"4: * Mirrors backend schemas: SearchResponse, TechniquePageDetail, TopicCategory, CreatorBrowseItem.\n40:  source_video_id: string;\n77:export interface TechniquePageDetail {\n97:  source_videos: SourceVideoSummary[];\n100:export interface TechniquePageVersionSummary {\n106:export interface TechniquePageVersionListResponse {\n107:  items: TechniquePageVersionSummary[];\n111:export interface TechniquePageVersionDetail {\n281:): Promise<TechniquePageDetail> {\n282:  return request<TechniquePageDetail>(`${BASE}/techniques/${slug}`);\n291:): Promise<TechniquePageVersionListResponse> {\n292:  return request<TechniquePageVersionListResponse>(\n293:    `${BASE}/techniques/${slug}/versions`,\n300:): Promise<TechniquePageVersionDetail> {\n301:  return request<TechniquePageVersionDetail>(\n302:    `${BASE}/techniques/${slug}/versions/${versionNumber}`,\n645:  return request<ChunkingDataResponse>(`${BASE}/admin/pipeline/chunking/${videoId}`);\n"}],"isError":false,"timestamp":1775180911763}}
{"type":"message","id":"a848bc9e","parentId":"7684264a","timestamp":"2026-04-03T01:48:31.767Z","message":{"role":"toolResult","toolCallId":"toolu_013UrhamWAbP5PF2i4aXk8yF","toolName":"bash","content":[{"type":"text","text":"428:export interface PipelineVideoItem {\n454:  items: PipelineVideoItem[];\n644:export async function fetchChunkingData(videoId: string): Promise<ChunkingDataResponse> {\n691:export async function fetchStalePages(): Promise<StalePagesResponse> {\n"}],"isError":false,"timestamp":1775180911765}}
{"type":"message","id":"53b45456","parentId":"a848bc9e","timestamp":"2026-04-03T01:48:38.024Z","message":{"role":"assistant","content":[{"type":"text","text":"Let me check what `SourceVideoSummary` looks like in the frontend, and what the existing AdminPipeline page already has for per-video chunking inspection."},{"type":"toolCall","id":"toolu_01RZYGTacPdH7YmnHz8NF56K","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'SourceVideoSummary\\|source_videos' frontend/src/api/public-client.ts"}},{"type":"toolCall","id":"toolu_01DxAUybsdRkCmyNWR7ic7rL","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'AdminDropdown\\|admin.*route\\|/admin' frontend/src/App.tsx 2>/dev/null || grep -rn 'AdminPipeline\\|AdminReport\\|route.*admin' frontend/src/App.tsx frontend/src/main.tsx 2>/dev/null | head -15"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":261,"cacheRead":99108,"cacheWrite":642,"totalTokens":100012,"cost":{"input":0.000005,"output":0.006525,"cacheRead":0.049554,"cacheWrite":0.0040125000000000004,"total":0.060096500000000004}},"stopReason":"toolUse","timestamp":1775180911765}}
{"type":"message","id":"2101f64b","parentId":"53b45456","timestamp":"2026-04-03T01:48:38.050Z","message":{"role":"toolResult","toolCallId":"toolu_01RZYGTacPdH7YmnHz8NF56K","toolName":"bash","content":[{"type":"text","text":"70:export interface SourceVideoSummary {\n97:  source_videos: SourceVideoSummary[];\n"}],"isError":false,"timestamp":1775180918050}}
{"type":"message","id":"9234dd58","parentId":"2101f64b","timestamp":"2026-04-03T01:48:38.057Z","message":{"role":"toolResult","toolCallId":"toolu_01DxAUybsdRkCmyNWR7ic7rL","toolName":"bash","content":[{"type":"text","text":"13:import AdminDropdown from \"./components/AdminDropdown\";\n100:            <AdminDropdown />\n129:          <Route path=\"/admin/reports\" element={<AdminReports />} />\n130:          <Route path=\"/admin/pipeline\" element={<AdminPipeline />} />\n"}],"isError":false,"timestamp":1775180918054}}
{"type":"message","id":"0cc8140a","parentId":"9234dd58","timestamp":"2026-04-03T01:48:41.688Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_01GfqGyd6CG71e61XV5aQueH","name":"read","arguments":{"limit":50,"offset":65,"path":"frontend/src/api/public-client.ts"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":95,"cacheRead":99750,"cacheWrite":416,"totalTokens":100262,"cost":{"input":0.000005,"output":0.002375,"cacheRead":0.049874999999999996,"cacheWrite":0.0026000000000000003,"total":0.054854999999999994}},"stopReason":"toolUse","timestamp":1775180918055}}
{"type":"message","id":"f28a86fa","parentId":"0cc8140a","timestamp":"2026-04-03T01:48:41.693Z","message":{"role":"toolResult","toolCallId":"toolu_01GfqGyd6CG71e61XV5aQueH","toolName":"read","content":[{"type":"text","text":"  heading: string;\n  content: string;\n  subsections: BodySubSectionV2[];\n}\n\nexport interface SourceVideoSummary {\n  id: string;\n  filename: string;\n  content_type: string;\n  added_at: string | null;\n}\n\nexport interface TechniquePageDetail {\n  id: string;\n  title: string;\n  slug: string;\n  topic_category: string;\n  topic_tags: string[] | null;\n  summary: string | null;\n  body_sections: BodySectionV2[] | Record<string, unknown> | null;\n  body_sections_format: string;\n  signal_chains: unknown[] | null;\n  plugins: string[] | null;\n  creator_id: string;\n  source_quality: string | null;\n  view_count: number;\n  created_at: string;\n  updated_at: string;\n  key_moments: KeyMomentSummary[];\n  creator_info: CreatorInfo | null;\n  related_links: RelatedLinkItem[];\n  version_count: number;\n  source_videos: SourceVideoSummary[];\n}\n\nexport interface TechniquePageVersionSummary {\n  version_number: number;\n  created_at: string;\n  pipeline_metadata: Record<string, unknown> | null;\n}\n\nexport interface TechniquePageVersionListResponse {\n  items: TechniquePageVersionSummary[];\n  total: number;\n}\n\nexport interface TechniquePageVersionDetail {\n  version_number: number;\n  content_snapshot: Record<string, unknown>;\n  pipeline_metadata: Record<string, unknown> | null;\n\n[628 more lines in file. Use offset=115 to continue.]"}],"isError":false,"timestamp":1775180921690}}
{"type":"message","id":"571bcdbd","parentId":"f28a86fa","timestamp":"2026-04-03T01:48:47.068Z","message":{"role":"assistant","content":[{"type":"text","text":"Now I understand the full picture. Let me check what frontend technique detail page shows for source_videos currently."},{"type":"toolCall","id":"toolu_017pxadm8UfJvbFNRrMcMp7U","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'source_video\\|sourceVideo\\|source.video' frontend/src/pages/TechniqueDetail.tsx | head -15"}},{"type":"toolCall","id":"toolu_0163LDm116QtihSHeVoTvbSh","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -rn 'version_count\\|versions\\|version' frontend/src/pages/TechniqueDetail.tsx | head -10"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":210,"cacheRead":100166,"cacheWrite":529,"totalTokens":100906,"cost":{"input":0.000005,"output":0.00525,"cacheRead":0.050082999999999996,"cacheWrite":0.00330625,"total":0.058644249999999995}},"stopReason":"toolUse","timestamp":1775180921691}}
{"type":"message","id":"8bd68910","parentId":"571bcdbd","timestamp":"2026-04-03T01:48:47.086Z","message":{"role":"toolResult","toolCallId":"toolu_017pxadm8UfJvbFNRrMcMp7U","toolName":"bash","content":[{"type":"text","text":"grep: frontend/src/pages/TechniqueDetail.tsx: No such file or directory\n"}],"isError":false,"timestamp":1775180927086}}
{"type":"message","id":"6b6a236f","parentId":"8bd68910","timestamp":"2026-04-03T01:48:47.093Z","message":{"role":"toolResult","toolCallId":"toolu_0163LDm116QtihSHeVoTvbSh","toolName":"bash","content":[{"type":"text","text":"grep: frontend/src/pages/TechniqueDetail.tsx: No such file or directory\n"}],"isError":false,"timestamp":1775180927090}}
{"type":"message","id":"7152aec4","parentId":"6b6a236f","timestamp":"2026-04-03T01:48:50.911Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_01Jhf6xbicjEfr4Lgtmasdnn","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && find frontend/src -name '*echnique*' -o -name '*detail*' | head -10"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":92,"cacheRead":100695,"cacheWrite":315,"totalTokens":101103,"cost":{"input":0.000005,"output":0.0023,"cacheRead":0.050347499999999996,"cacheWrite":0.00196875,"total":0.054621249999999996}},"stopReason":"toolUse","timestamp":1775180927091}}
{"type":"message","id":"3d42e908","parentId":"7152aec4","timestamp":"2026-04-03T01:48:50.927Z","message":{"role":"toolResult","toolCallId":"toolu_01Jhf6xbicjEfr4Lgtmasdnn","toolName":"bash","content":[{"type":"text","text":"frontend/src/pages/TechniquePage.tsx\n"}],"isError":false,"timestamp":1775180930925}}
{"type":"message","id":"9e333349","parentId":"3d42e908","timestamp":"2026-04-03T01:48:55.206Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_01Eva6aYq3X5y3ZVgB4qEZMX","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'source_video\\|sourceVideo\\|version' frontend/src/pages/TechniquePage.tsx | head -15"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":100,"cacheRead":100695,"cacheWrite":432,"totalTokens":101228,"cost":{"input":0.000005,"output":0.0025,"cacheRead":0.050347499999999996,"cacheWrite":0.0027,"total":0.0555525}},"stopReason":"toolUse","timestamp":1775180930926}}
{"type":"message","id":"604fdb2a","parentId":"9e333349","timestamp":"2026-04-03T01:48:55.222Z","message":{"role":"toolResult","toolCallId":"toolu_01Eva6aYq3X5y3ZVgB4qEZMX","toolName":"bash","content":[{"type":"text","text":"2: * Technique page detail view with version switching.\n4: * Fetches a single technique by slug. When historical versions exist,\n5: * shows a version switcher that lets admins view previous snapshots\n43:/** Extract the displayable fields from a version snapshot, matching TechniqueDetail shape. */\n92:  const [versions, setVersions] = useState<TechniquePageVersionSummary[]>([]);\n94:  const [versionDetail, setVersionDetail] =\n96:  const [versionLoading, setVersionLoading] = useState(false);\n98:  // Load technique + version list\n115:          // Load versions if any exist\n116:          if (data.version_count > 0) {\n121:              // Non-critical — version list fails silently\n145:  // Load version detail when selection changes\n210:  // Overlay snapshot fields when viewing a historical version\n211:  const isHistorical = selectedVersion !== \"current\" && versionDetail != null;\n213:    ? snapshotToOverlay(versionDetail.content_snapshot)\n"}],"isError":false,"timestamp":1775180935219}}
{"type":"message","id":"cf15e2ba","parentId":"604fdb2a","timestamp":"2026-04-03T01:48:58.910Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_01JjJd7KxneYoZ5FZx7oz5vX","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'source_video' frontend/src/pages/TechniquePage.tsx"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":89,"cacheRead":101127,"cacheWrite":365,"totalTokens":101582,"cost":{"input":0.000005,"output":0.002225,"cacheRead":0.0505635,"cacheWrite":0.0022812500000000003,"total":0.05507475}},"stopReason":"toolUse","timestamp":1775180935220}}
{"type":"message","id":"73ea3c1b","parentId":"cf15e2ba","timestamp":"2026-04-03T01:48:58.927Z","message":{"role":"toolResult","toolCallId":"toolu_01JjJd7KxneYoZ5FZx7oz5vX","toolName":"bash","content":[{"type":"text","text":"574:        {technique.key_moments.length > 0 && technique.key_moments[0]?.source_video_id && (\n576:            to={`/admin/pipeline?video=${technique.key_moments[0].source_video_id}`}\n"}],"isError":false,"timestamp":1775180938923}}
{"type":"message","id":"0394ae72","parentId":"73ea3c1b","timestamp":"2026-04-03T01:49:04.278Z","message":{"role":"assistant","content":[{"type":"text","text":"Good — the technique page currently has a minimal link to the pipeline admin for the first source video, but doesn't display multi-source info. Let me check if there are any existing admin endpoints that specifically deal with composition history or technique page management from an admin perspective."},{"type":"toolCall","id":"toolu_01XdaiJYm1wvXacq1qqqNk78","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'technique_page\\|source_video_links\\|TechniquePageVideo' backend/routers/pipeline.py | head -10"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":159,"cacheRead":101492,"cacheWrite":163,"totalTokens":101815,"cost":{"input":0.000005,"output":0.003975,"cacheRead":0.050746,"cacheWrite":0.0010187500000000001,"total":0.055744749999999996}},"stopReason":"toolUse","timestamp":1775180938925}}
{"type":"message","id":"550f0ff4","parentId":"0394ae72","timestamp":"2026-04-03T01:49:04.295Z","message":{"role":"toolResult","toolCallId":"toolu_01XdaiJYm1wvXacq1qqqNk78","toolName":"bash","content":[{"type":"text","text":"213:    Does NOT delete technique_pages — the pipeline re-synthesizes via upsert.\n436:            \"technique_page_id\": str(m.technique_page_id) if m.technique_page_id else None,\n840:                .where(TechniquePageVersion.technique_page_id == page.id)\n935:    Deletes: technique_page_versions, related_technique_links, technique_pages,\n948:    counts[\"technique_page_versions\"] = result.rowcount\n954:    # 3. Key moments (FK to technique_pages, must clear before pages)\n960:    counts[\"technique_pages\"] = result.rowcount\n"}],"isError":false,"timestamp":1775180944292}}
{"type":"message","id":"c680655c","parentId":"550f0ff4","timestamp":"2026-04-03T01:49:11.640Z","message":{"role":"assistant","content":[{"type":"text","text":"Now let me understand the S06 scope from the roadmap demo text: \"Admin view for multi-source page shows source dropdown, composition history, per-video chunking inspection.\"\n\nThis needs:\n1. **Source dropdown** — view which videos contributed to a technique page\n2. **Composition history** — view version history showing what was composed when (TechniquePageVersion already exists)\n3. **Per-video chunking inspection** — already exists as ChunkingInspector in AdminPipeline\n\nLet me check what new API endpoints might be needed."},{"type":"toolCall","id":"toolu_01VPej3gnuZWWEjKibeWSb9D","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'TechniquePageVersion\\|pipeline_metadata\\|content_snapshot' backend/schemas.py | head -15"}},{"type":"toolCall","id":"toolu_01UDhtsXwQ4A9ZmmtZM4UcfF","name":"read","arguments":{"limit":30,"offset":280,"path":"backend/schemas.py"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":295,"cacheRead":101655,"cacheWrite":327,"totalTokens":102278,"cost":{"input":0.000005,"output":0.0073750000000000005,"cacheRead":0.0508275,"cacheWrite":0.00204375,"total":0.06025124999999999}},"stopReason":"toolUse","timestamp":1775180944293}}
{"type":"message","id":"39431eb5","parentId":"c680655c","timestamp":"2026-04-03T01:49:11.655Z","message":{"role":"toolResult","toolCallId":"toolu_01VPej3gnuZWWEjKibeWSb9D","toolName":"bash","content":[{"type":"text","text":"300:class TechniquePageVersionSummary(BaseModel):\n306:    pipeline_metadata: dict | None = None\n309:class TechniquePageVersionDetail(BaseModel):\n314:    content_snapshot: dict\n315:    pipeline_metadata: dict | None = None\n319:class TechniquePageVersionListResponse(BaseModel):\n321:    items: list[TechniquePageVersionSummary] = Field(default_factory=list)\n"}],"isError":false,"timestamp":1775180951654}}
{"type":"message","id":"390f8529","parentId":"39431eb5","timestamp":"2026-04-03T01:49:11.658Z","message":{"role":"toolResult","toolCallId":"toolu_01UDhtsXwQ4A9ZmmtZM4UcfF","toolName":"read","content":[{"type":"text","text":"    \"\"\"Lightweight source video info for technique page detail.\"\"\"\n    model_config = ConfigDict(from_attributes=True)\n\n    id: uuid.UUID\n    filename: str\n    content_type: str\n    added_at: datetime | None = None\n\n\nclass TechniquePageDetail(TechniquePageRead):\n    \"\"\"Technique page with nested key moments, creator, and related links.\"\"\"\n    key_moments: list[KeyMomentSummary] = Field(default_factory=list)\n    creator_info: CreatorInfo | None = None\n    related_links: list[RelatedLinkItem] = Field(default_factory=list)\n    version_count: int = 0\n    source_videos: list[SourceVideoSummary] = Field(default_factory=list)\n\n\n# ── Technique Page Versions ──────────────────────────────────────────────────\n\nclass TechniquePageVersionSummary(BaseModel):\n    \"\"\"Lightweight version entry for list responses.\"\"\"\n    model_config = ConfigDict(from_attributes=True)\n\n    version_number: int\n    created_at: datetime\n    pipeline_metadata: dict | None = None\n\n\nclass TechniquePageVersionDetail(BaseModel):\n\n[126 more lines in file. Use offset=310 to continue.]"}],"isError":false,"timestamp":1775180951654}}
{"type":"message","id":"6fb992ef","parentId":"390f8529","timestamp":"2026-04-03T01:49:16.649Z","message":{"role":"assistant","content":[{"type":"text","text":"Now I have a clear picture of what exists. Let me check the list of technique pages endpoint for admin context."},{"type":"toolCall","id":"toolu_01FGbYSzSLXjUcLCCQqEbiBH","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'def list_techniques\\|def get_technique\\|@router' backend/routers/techniques.py | head -15"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":124,"cacheRead":101982,"cacheWrite":769,"totalTokens":102876,"cost":{"input":0.000005,"output":0.0031000000000000003,"cacheRead":0.050990999999999995,"cacheWrite":0.004806250000000001,"total":0.05890224999999999}},"stopReason":"toolUse","timestamp":1775180951655}}
{"type":"message","id":"28b02b98","parentId":"6fb992ef","timestamp":"2026-04-03T01:49:16.661Z","message":{"role":"toolResult","toolCallId":"toolu_01FGbYSzSLXjUcLCCQqEbiBH","toolName":"bash","content":[{"type":"text","text":"114:@router.get(\"\", response_model=PaginatedResponse)\n115:async def list_techniques(\n196:@router.get(\"/random\")\n209:@router.get(\"/{slug}\", response_model=TechniquePageDetail)\n210:async def get_technique(\n324:@router.get(\"/{slug}/versions\", response_model=TechniquePageVersionListResponse)\n350:@router.get(\"/{slug}/versions/{version_number}\", response_model=TechniquePageVersionDetail)\n351:async def get_technique_version(\n"}],"isError":false,"timestamp":1775180956659}}
{"type":"message","id":"b4d52b4a","parentId":"28b02b98","timestamp":"2026-04-03T01:49:19.981Z","message":{"role":"assistant","content":[{"type":"toolCall","id":"toolu_01YRJ8YUbLXXcQVzUyTuDL5z","name":"read","arguments":{"limit":50,"offset":115,"path":"backend/routers/techniques.py"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":92,"cacheRead":102751,"cacheWrite":270,"totalTokens":103114,"cost":{"input":0.000005,"output":0.0023,"cacheRead":0.0513755,"cacheWrite":0.0016875,"total":0.055368}},"stopReason":"toolUse","timestamp":1775180956659}}
{"type":"message","id":"40bc8d45","parentId":"b4d52b4a","timestamp":"2026-04-03T01:49:19.987Z","message":{"role":"toolResult","toolCallId":"toolu_01YRJ8YUbLXXcQVzUyTuDL5z","toolName":"read","content":[{"type":"text","text":"async def list_techniques(\n    category: Annotated[str | None, Query()] = None,\n    creator_slug: Annotated[str | None, Query()] = None,\n    sort: Annotated[str, Query()] = \"recent\",\n    offset: Annotated[int, Query(ge=0)] = 0,\n    limit: Annotated[int, Query(ge=1, le=100)] = 50,\n    db: AsyncSession = Depends(get_session),\n) -> PaginatedResponse:\n    \"\"\"List technique pages with optional category/creator filtering.\"\"\"\n    # Correlated subquery for key moment count (same pattern as creators.py)\n    key_moment_count_sq = (\n        select(func.count())\n        .where(KeyMoment.technique_page_id == TechniquePage.id)\n        .correlate(TechniquePage)\n        .scalar_subquery()\n    )\n\n    # Build base query with filters\n    base_stmt = select(TechniquePage.id)\n    if category:\n        base_stmt = base_stmt.where(TechniquePage.topic_category == category)\n    if creator_slug:\n        base_stmt = base_stmt.join(Creator, TechniquePage.creator_id == Creator.id).where(\n            Creator.slug == creator_slug\n        )\n\n    # Count total before pagination\n    count_stmt = select(func.count()).select_from(base_stmt.subquery())\n    count_result = await db.execute(count_stmt)\n    total = count_result.scalar() or 0\n\n    # Main query with subquery column\n    stmt = select(\n        TechniquePage,\n        key_moment_count_sq.label(\"key_moment_count\"),\n    )\n    if category:\n        stmt = stmt.where(TechniquePage.topic_category == category)\n    if creator_slug:\n        stmt = stmt.join(Creator, TechniquePage.creator_id == Creator.id).where(\n            Creator.slug == creator_slug\n        )\n\n    stmt = stmt.options(selectinload(TechniquePage.creator))\n    if sort == \"random\":\n        stmt = stmt.order_by(func.random())\n    elif sort == \"oldest\":\n        stmt = stmt.order_by(TechniquePage.created_at.asc())\n    elif sort == \"alpha\":\n        stmt = stmt.order_by(TechniquePage.title.asc())\n\n[217 more lines in file. Use offset=165 to continue.]"}],"isError":false,"timestamp":1775180959984}}
{"type":"message","id":"7fc55437","parentId":"40bc8d45","timestamp":"2026-04-03T01:49:24.188Z","message":{"role":"assistant","content":[{"type":"text","text":"I now have enough context. Let me also quickly check the existing frontend pages list to see what route patterns are established."},{"type":"toolCall","id":"toolu_019jVK5UUHLhDPkdSvs89Sty","name":"bash","arguments":{"command":"cd /home/aux/projects/content-to-kb-automator && grep -n 'Route\\|route' frontend/src/App.tsx | head -20"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":114,"cacheRead":102751,"cacheWrite":982,"totalTokens":103848,"cost":{"input":0.000005,"output":0.00285,"cacheRead":0.0513755,"cacheWrite":0.006137500000000001,"total":0.060368000000000005}},"stopReason":"toolUse","timestamp":1775180959985}}
{"type":"message","id":"c43a49b5","parentId":"7fc55437","timestamp":"2026-04-03T01:49:24.206Z","message":{"role":"toolResult","toolCallId":"toolu_019jVK5UUHLhDPkdSvs89Sty","toolName":"bash","content":[{"type":"text","text":"2:import { Link, Navigate, Route, Routes, useLocation, useNavigate } from \"react-router-dom\";\n24:  // Close menu on route change\n116:        <Routes>\n117:          {/* Public routes */}\n118:          <Route path=\"/\" element={<Home />} />\n119:          <Route path=\"/search\" element={<SearchResults />} />\n120:          <Route path=\"/techniques/:slug\" element={<TechniquePage />} />\n122:          {/* Browse routes */}\n123:          <Route path=\"/creators\" element={<CreatorsBrowse />} />\n124:          <Route path=\"/creators/:slug\" element={<CreatorDetail />} />\n125:          <Route path=\"/topics/:category/:subtopic\" element={<SubTopicPage />} />\n126:          <Route path=\"/topics\" element={<TopicsBrowse />} />\n128:          {/* Admin routes */}\n129:          <Route path=\"/admin/reports\" element={<AdminReports />} />\n130:          <Route path=\"/admin/pipeline\" element={<AdminPipeline />} />\n132:          {/* Info routes */}\n133:          <Route path=\"/about\" element={<About />} />\n136:          <Route path=\"*\" element={<Navigate to=\"/\" replace />} />\n137:        </Routes>\n"}],"isError":false,"timestamp":1775180964202}}
{"type":"message","id":"9c17e27b","parentId":"c43a49b5","timestamp":"2026-04-03T01:49:57.542Z","message":{"role":"assistant","content":[{"type":"text","text":"Good. Now I have a complete picture. Let me write the research."},{"type":"toolCall","id":"toolu_0112BPyWkDc4zJLxUDDGNnk5","name":"gsd_summary_save","arguments":{"artifact_type":"RESEARCH","content":"# S06 Research: Admin UI — Multi-Source Pipeline Management\n\n## Depth: Light Research\n\nThis is straightforward admin UI work using established patterns already in the codebase. The AdminPipeline page (1500+ lines) is a mature component with video list, event logs, chunking inspector, bulk actions, and run history. The data model (S03) and compose logic (S04) are complete. The needed API surface mostly exists already.\n\n## Summary\n\nThe roadmap demo: \"Admin view for multi-source page shows source dropdown, composition history, per-video chunking inspection.\"\n\nThree capabilities are needed:\n\n1. **Multi-source technique page list** — Admin needs a view of technique pages showing which source videos contributed to each, grouped/filterable by creator. No such admin endpoint or page exists yet.\n\n2. **Composition history** — Version history showing what was composed when. The `TechniquePageVersion` model and `/{slug}/versions` API endpoint already exist. The frontend `TechniquePage.tsx` already has version switching. What's missing is surfacing this from an admin management perspective (which pages have multiple sources, which were composed vs created fresh).\n\n3. **Per-video chunking inspection** — Already exists as `ChunkingInspector` component in `AdminPipeline.tsx` and the `GET /admin/pipeline/chunking/{video_id}` backend endpoint. This is done.\n\n## Implementation Landscape\n\n### What Exists\n\n**Backend:**\n- `GET /techniques` — list with category/creator filter, pagination, sort\n- `GET /techniques/{slug}` — detail with `source_videos` and `version_count`\n- `GET /techniques/{slug}/versions` — version list with `pipeline_metadata`\n- `GET /techniques/{slug}/versions/{n}` — version detail with `content_snapshot`\n- `GET /admin/pipeline/chunking/{video_id}` — full chunking data\n- `TechniquePageVideo` association table tracking page↔video links with `added_at`\n- `TechniquePageVersion` with `pipeline_metadata` JSONB (stores prompt hashes, model config)\n\n**Frontend:**\n- `AdminPipeline.tsx` — 1500-line page with video management, chunking inspector, bulk actions\n- `TechniquePage.tsx` — version switcher already built\n- `public-client.ts` — types for `SourceVideoSummary`, `TechniquePageVersionSummary`, fetchers for techniques/versions\n- Route: `/admin/pipeline` (video management), no technique admin page yet\n\n**Schemas:**\n- `SourceVideoSummary`: id, filename, content_type, added_at\n- `TechniquePageVersionSummary`: version_number, created_at, pipeline_metadata\n- `TechniquePageDetail` already includes `source_videos[]` and `version_count`\n\n### What's Needed\n\n**New backend endpoint:**\n- `GET /admin/pipeline/technique-pages` — Admin-focused technique page list with source video counts, body_sections_format, version counts, and latest composition date. Needs joins to `technique_page_videos` and `technique_page_versions`. Filterable by multi-source (>1 video) and by creator.\n\n**New frontend page or section:**\n- Either a new page at `/admin/techniques` or a new tab/section in `AdminPipeline.tsx`. Given that AdminPipeline is already 1500 lines, a separate page makes more sense.\n- Shows technique pages in a table: title, creator, source video count, format (v1/v2), version count, last updated\n- Expandable row showing source videos list (from existing `/techniques/{slug}` response) and link to version history\n- Filter: \"Multi-source only\" toggle, creator dropdown\n- Link to pipeline admin filtered by video ID (already supported via `?video=` query param)\n\n**Frontend wiring:**\n- New route `/admin/techniques`\n- Entry in `AdminDropdown` component\n- New fetch function in `public-client.ts`\n\n### Natural Seams\n\n1. **T01: Backend endpoint** — `GET /admin/pipeline/technique-pages` with multi-source filtering. Small, self-contained.\n2. **T02: Frontend page** — New `AdminTechniquePages.tsx` with table, expand/collapse for source videos and composition history, filters. Uses existing API fetchers for version detail.\n3. **T03: Integration** — Route, dropdown entry, cross-links between pipeline admin ↔ technique admin.\n\n### Constraints\n\n- The existing `GET /techniques` endpoint returns paginated public data. The admin endpoint needs additional fields (source video count, format, version count) without modifying the public response — so a separate admin endpoint is cleaner.\n- `AdminPipeline.tsx` already links to pipeline admin via `?video=` — the technique admin should link back using the same pattern.\n- ChunkingInspector already exists in AdminPipeline — the technique admin page should link to it rather than duplicate it.\n\n## Recommendation\n\nBuild a lightweight admin technique pages endpoint and a new frontend page. ~3 tasks: backend endpoint, frontend page, integration wiring. The heaviest task is the frontend page, but it follows the exact same patterns as AdminPipeline (table with expandable rows, filters, badges).\n","milestone_id":"M014","slice_id":"S06"}}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":1362,"cacheRead":103733,"cacheWrite":443,"totalTokens":105539,"cost":{"input":0.000005,"output":0.034050000000000004,"cacheRead":0.051866499999999996,"cacheWrite":0.00276875,"total":0.08869025}},"stopReason":"toolUse","timestamp":1775180964204}}
{"type":"message","id":"97b2110e","parentId":"9c17e27b","timestamp":"2026-04-03T01:49:57.549Z","message":{"role":"toolResult","toolCallId":"toolu_0112BPyWkDc4zJLxUDDGNnk5","toolName":"gsd_summary_save","content":[{"type":"text","text":"Saved RESEARCH artifact to milestones/M014/slices/S06/S06-RESEARCH.md"}],"details":{"operation":"save_summary","path":"milestones/M014/slices/S06/S06-RESEARCH.md","artifact_type":"RESEARCH"},"isError":false,"timestamp":1775180997545}}
{"type":"message","id":"58503588","parentId":"97b2110e","timestamp":"2026-04-03T01:50:00.686Z","message":{"role":"assistant","content":[{"type":"text","text":"Slice S06 researched."}],"api":"anthropic-messages","provider":"anthropic","model":"claude-opus-4-6","usage":{"input":1,"output":11,"cacheRead":104176,"cacheWrite":1400,"totalTokens":105588,"cost":{"input":0.000005,"output":0.000275,"cacheRead":0.052087999999999995,"cacheWrite":0.00875,"total":0.061118}},"stopReason":"stop","timestamp":1775180997546}}
