# Jinja2 Templates

## Environment configuration

| Setting | Value |
| --- | --- |
| Templates directory | `server/fishtest/templates/` |
| File extension | `.html.j2` |
| Autoescape | Enabled for HTML, XML, and `.j2` files |
| Undefined behavior | `StrictUndefined` (missing variables raise errors) |
| Extensions | `jinja2.ext.do` |
| Instance | Single `Environment` built by `http/jinja.py` on the first render (`_jinja_templates()` in `http/template_renderer.py` is cached) |

Custom filters and global functions are registered via `http/template_helpers.py` and `http/jinja.py`.

### Custom filters

| Filter | Description |
| --- | --- |
| `urlencode` | URL-encodes a value (`quote_plus`) |
| `split` | Splits a string (`str.split`) |
| `string` | Converts to string (`str()`) |

### Global functions

| Global | Source | Description |
| --- | --- | --- |
| `static_url(path)` | `http/jinja.py` | Static asset URL with cache-buster token |
| `diff_url(...)` | `util.py` | GitHub diff URL builder |
| `format_bounds(...)` | `util.py` | SPRT bounds formatter |
| `format_date(dt)` | `util.py` | Date formatter |
| `format_group(groups)` | `util.py` | User group label formatter |
| `format_results(...)` | `util.py` | Game results formatter |
| `format_time_ago(dt)` | `util.py` | Relative time formatter |
| `display_residual(r)` | `util.py` | Chi-squared residual display |
| `is_active_sprt_ltc(run)` | `util.py` | Checks if run is active SPRT LTC |
| `is_elo_pentanomial_run(run)` | `template_helpers.py` | Checks pentanomial display |
| `nelo_pentanomial_summary(...)` | `template_helpers.py` | Pentanomial ELO summary (returns `Markup`) |
| `results_pre_attrs(...)` | `template_helpers.py` | HTML attributes for results display (returns `Markup`) |
| `run_tables_prefix(username)` | `template_helpers.py` | Toggle prefix for run tables |
| `tests_run_setup(...)` | `template_helpers.py` | Test form default values |
| `tests_repo(run)` | `util.py` | Extract the canonical tests repo URL from run |
| `get_tc_ratio(...)` | `util.py` | Time control ratio of a run |
| `worker_name(info)` | `util.py` | Format worker name from info dict |
| `list_to_string(values, decimals)` | `template_helpers.py` | Formats a list of floats as a string |
| `pdf_to_string(...)` | `template_helpers.py` | Formats a probability density function |
| `t_conf(...)` | `template_helpers.py` | Confidence interval formatting |
| `poll` | `http/jinja.py` | Shared htmx polling intervals exposed to templates |
| `finished` | `http/jinja.py` | Finished-run result caps (`filter_max_count_anon`, `filter_max_count_auth`) |
| `htmx` | `http/jinja.py` | Shared htmx timing defaults exposed to templates |
| `cookies` | `http/jinja.py` | Shared cookie max-age values for page scripts/templates |
| `fishtest` | `fishtest` package | Package module (version, metadata) |
| `gh` | `github_api.py` | GitHub API module (commit\_url, etc.) |
| `math`, `datetime`, `copy`, `float` | Python stdlib | Standard library access |
| `urllib` | Python stdlib | `urllib.parse` |

## Rendering flow

1. The view handler returns a context dict; the template is the route's `renderer` in `_VIEW_ROUTES`.
2. `_dispatch_view()` calls `render_template_to_response()` in the threadpool.
3. `build_template_context()` in `http/boundary.py` constructs the shared base context (CSRF token, current user, flash messages, navigation URLs).
4. The handler-specific context is merged with the base context.
5. `Jinja2Templates.TemplateResponse(request, name, context)` renders the template and returns an HTML response.
6. Rendering is synchronous and executes in the threadpool.

## Shared base context

Every template receives these keys from `build_template_context()`:

| Key | Type | Description |
| --- | --- | --- |
| `request` | `Request` | Starlette request object (templates read `request.url`) |
| `csrf_token` | string | CSRF token for forms and meta tags |
| `current_user` | `{"username": str}` or `None` | Authenticated user |
| `open_graph` | dict | Server-owned Open Graph fields rendered by `base.html.j2` |
| `theme` | string | `"dark"`, `"light"`, or empty (from cookie) |
| `theme_color` | string or `None` | Optional `theme-color` meta value rendered by `base.html.j2` |
| `pending_users_count` | int | Pending-user count used by the sidebar `Users` link |
| `static_url` | callable | `static_url(path)` function |
| `flash` | dict | `{"error": [...], "warning": [...], "info": [...]}` |
| `urls` | dict | Navigation URLs (see below) |

## Page metadata contract

`base.html.j2` renders the page head from shared context.

Full-page handlers may override these keys:

- `open_graph`: dictionary with `site_name`, `type`, `title`, `description`, and `url`
- `theme_color`: CSS color string or `None`

For `/tests/view/{id}`, `open_graph['description']` is plain multi-line text so Discord link previews preserve the run summary layout, including pentanomial lines when present, without shipping literal backticks.

For `/actions`, the full page overrides `open_graph` with the first visible action row. Its `og:url` keeps the current query string because filters, timeline cursors, and explicit `max_count` values define the shared events-log slice.

Fragment-only templates do not own page-head metadata. The `/tests/view/{id}` detail page follows this rule: the full page renders the head tags, while the live `/tests/view/{id}/detail` fragment updates body content only.

`actions_content_fragment.html.j2` follows the same rule: the full `/actions` page owns the Open Graph tags, while the htmx fragment updates only `#actions-content`.

### Navigation URLs (`urls` dict)

```
{
    "home": "/",
    "login": "/login",
    "logout": "/logout",
    "signup": "/signup",
    "user_profile": "/user",
    "tests": "/tests",
   "tests_finished": "/tests/finished",
    "tests_run": "/tests/run",
    "tests_user_prefix": "/tests/user/",
    "tests_machines": "/tests/machines",
    "nn_upload": "/upload",
    "nns": "/nns",
    "contributors": "/contributors",
    "contributors_monthly": "/contributors/monthly",
    "actions": "/actions",
    "user_management": "/user_management",
    "workers_blocked": "/workers/show",
    "sprt_calc": "/sprt_calc",
    "rate_limits": "/rate_limits",
    "api_rate_limit": "/api/rate_limit",
}
```

### Shared sidebar status links

- `base.html.j2` includes `pending_users_nav_fragment.html.j2` inside a stable htmx poll wrapper. The wrapper owns the `load` and `every` triggers, while the fragment owns only the rendered anchor HTML and current count markup.
- `base.html.j2` renders the sidebar `GitHub Rate Limits` anchor directly. `static/js/application.js` updates that link, not a server fragment endpoint, because the browser-side GitHub token lives in local storage and is not part of server session state.
- `rate_limits.html.j2` exposes the same browser-side client poll cadence via `data-poll-seconds` on `#client_rate_limit`, so the page row and the sidebar link read the same client limit state.

## Client-side behavior pattern

Behavior-heavy page scripts should prefer static assets plus `data-*` configuration over large inline `<script>` blocks.

Current examples:

- `contributors.html.j2` -\> `static/js/contributors.js`
- `tests.html.j2` -\> `static/js/tests_homepage.js`
- `user.html.j2` (profile mode) -\> `static/js/user_profile.js`

`tests_homepage.js` owns the homepage Workers panel cookie state and triggers an immediate `machines:load` refresh whenever Bootstrap reports that the panel has been opened.

Shared behavior that spans multiple pages belongs in shared assets instead of page-local inline scripts. Search/filter inputs remain plain `<input type="search">` controls, and any shared behavior or styling should live in shared assets instead of page-local inline scripts.

This keeps templates focused on structure and server-provided state, aligns with MDN separation-of-concerns guidance, and makes the JS contract testable without embedding implementation details in the template body.

## Sortable table accessibility baseline

Seven content-fragment templates define inline `sort_header` Jinja2 macros for sortable data tables. All sortable tables must satisfy:

1. **`scope="col"`** on every `<th>` in `<thead>` (including non-sortable column headers like the `#` row number). Helps assistive technologies associate headers with data cells.
2. **`<caption class="visually-hidden">`** as the first child of each `<table>`. Provides a programmatic name for the table so screen readers can announce it before reading cells.
3. **`class="sticky-top"`** on `<thead>` so column headers remain visible when scrolling long tables.
4. **`aria-sort`** only on the currently active sort column header. Values: `ascending` or `descending`.
5. **`aria-hidden="true"`** on the decorative sort-indicator icon span.

Templates that follow this contract:

| Template | Table ID | Caption text |
| --- | --- | --- |
| `actions_content_fragment.html.j2` | `actions_table` | Events log results |
| `contributors_content_fragment.html.j2` | `contributors_table` | Contributors |
| `machines_fragment.html.j2` | `machines_table` | Machines |
| `nns_content_fragment.html.j2` | `nns_table` | Neural networks |
| `tasks_content_fragment.html.j2` | `tasks_table` | Tasks |
| `user_management_content_fragment.html.j2` | `user_management_table` | User management |
| `workers_content_fragment.html.j2` | `workers_table` | Workers |

Regression tests in `test_http_boundary.py` enforce `scope="col"`, `visually-hidden` caption, and `sticky-top` on `<thead>` for all seven templates.

### Filter and cookie consistency

The seven sortable tables split into two architectural patterns that determine their filter form and state-persistence behavior:

| Pattern | Pages | Filters | Cookie persistence | Polling |
| --- | --- | --- | --- | --- |
| **A** (standalone) | contributors, actions, workers, user\_management, nns | `form-control` (full-size) | 0-1 client-side cookies | none |
| **B** (embedded panel) | machines, tasks | `form-control-sm` | 4-6 server-side cookies | 60 s |

Design rules:

- **No custom width overrides** on filter inputs. Let `col-md-auto` size fields naturally across all standalone pages.
- This rule also applies to `/tests/finished?mode=search`: its username and free-text controls should size through the shared grid layout, not through a page-specific CSS minimum width.
- **`autocomplete="off"`** on search/filter text inputs prevents browser autofill from interfering with htmx-driven filtering. The `/actions` username and text inputs and the `/tests/finished` search inputs do not carry it.
- **Cookie max-age** must use `UI_STATE_COOKIE_MAX_AGE_SECONDS` from `settings.py`, never hardcoded values. Client-side cookies must flow through the shared `writeUiCookie()` helper in `application.js` or through template `data-*` attributes consumed by that helper. This is the single max-age owner for non-auth UI cookies; templates should expose `cookies.ui_state_max_age` or a page-specific `cookie_max_age` context instead of introducing literals. `cookies.contributors_findme_max_age` is the one other alias.

Known limitations:

- Workers and user\_management have zero cookie persistence for filter/sort state (user resets to defaults on page reload).
- Contributors has one client-side cookie (`contributors_findme`, the rank-jump mode) but no filter-state cookies.

## Template catalog

The live template inventory contains **54** Jinja templates:

- **1** base layout template
- **19** full-page templates that extend `base.html.j2`
- **34** non-base templates used as htmx fragments or shared partials

### Base layout template

| Template | Purpose |
| --- | --- |
| `base.html.j2` | Base layout (navbar, footer, page-head metadata, asset loading, htmx CDN, pending-user nav poll target) |

### Full-page templates (extend `base.html.j2`)

| Template | Purpose |
| --- | --- |
| `actions.html.j2` | Paginated action log |
| `contributors.html.j2` | Contributor leaderboard (all-time and monthly) |
| `login.html.j2` | Login form |
| `nn_upload.html.j2` | Neural network upload form |
| `nns.html.j2` | Neural network listing with pagination |
| `notfound.html.j2` | 404 error page |
| `rate_limits.html.j2` | API rate limit information page |
| `signup.html.j2` | User registration form |
| `sprt_calc.html.j2` | SPRT calculator page |
| `tests.html.j2` | Main tests dashboard |
| `tests_finished.html.j2` | Finished tests listing with filters |
| `tests_live_elo.html.j2` | Live ELO chart page |
| `tests_run.html.j2` | New test / rerun submission form |
| `tests_stats.html.j2` | Raw statistics page shell with visibility-aware poller |
| `tests_user.html.j2` | Per-user test listing |
| `tests_view.html.j2` | Single test detail page |
| `user.html.j2` | User profile page |
| `user_management.html.j2` | User administration page |
| `workers.html.j2` | Worker blocking administration page |

### Non-base templates

These templates do not extend the base layout. Some are direct htmx response fragments; others are shared partials included by page or fragment templates.

| Template | Kind | Primary use |
| --- | --- | --- |
| `actions_content_fragment.html.j2` | htmx content fragment | Swaps `#actions-content` |
| `active_run_filters_fragment.html.j2` | shared controls partial | Included by `run_table.html.j2` through `filters_template` for the Active panel |
| `contributors_content_fragment.html.j2` | htmx content fragment | Swaps `#contributors-content`; includes hidden-input OOB sync |
| `contributors_rows_fragment.html.j2` | shared row partial | Included by `contributors_content_fragment.html.j2` |
| `tests_run_tables_fragment.html.j2` | OOB poll fragment | Shared run-table OOB payload for `/tests?live=run_tables` and `/tests/user/{username}?live=run_tables` |
| `elo_results.html.j2` | shared display partial | Included where ELO summary markup is rendered |
| `elo_results_fragment.html.j2` | shared OOB partial | Included by `tests_view_detail_fragment.html.j2` for live ELO summary, status, and task-total updates |
| `homepage_stats_fragment.html.j2` | OOB summary fragment | Updates homepage stat targets |
| `live_elo_fragment.html.j2` | OOB poll fragment | Poll response for live ELO page updates |
| `machines_fragment.html.j2` | htmx content fragment | Swaps `#machines` and updates `#workers-count` OOB |
| `nns_content_fragment.html.j2` | htmx content fragment | Swaps `#nns-content` |
| `pagination.html.j2` | shared pagination partial | Included by paginated pages and fragments |
| `pending_users_nav_fragment.html.j2` | shared nav anchor fragment | Included server-side in `base.html.j2` and returned by `/user_management/pending_count` to refresh `#pending-users-nav` |
| `poll_stop_fragment.html.j2` | shared OOB partial | Renders `<div id="{poller_id}" hx-swap-oob="delete">`; included by a terminal poll response to remove its driver element and stop the poll |
| `run_table.html.j2` | shared table partial | Renders one run table shell and includes row markup |
| `run_table_row_fragment.html.j2` | row fragment | Renders `<tr>` rows; included by `run_table.html.j2`, and by `tests_run_tables_fragment.html.j2` inside the `<template id="<panel>-tbody" hx-swap-oob="innerHTML">` wrappers |
| `run_tables.html.j2` | shared table-group partial | Composes pending, active, and finished run tables |
| `tasks_content_fragment.html.j2` | htmx content fragment | Swaps `#tasks-content` and updates controls/pagination OOB |
| `tasks_controls_fragment.html.j2` | shared controls partial | Included by `tests_view.html.j2` and reused in OOB updates |
| `tasks_rows_fragment.html.j2` | shared row partial | Included by `tasks_content_fragment.html.j2` |
| `tests_filter_tabs_fragment.html.j2` | shared navigation partial | Renders test-mode tabs for caller-defined targets |
| `tests_finished_content_fragment.html.j2` | shared full-page partial | Included by `tests_finished.html.j2` |
| `tests_finished_results_fragment.html.j2` | htmx results fragment | Swaps `#tests-finished-content`; can update tab wrapper OOB |
| `tests_stats_content_fragment.html.j2` | polled content fragment | Swaps `#tests-stats-content` |
| `tests_view_detail_fragment.html.j2` | OOB poll fragment | Poll response for live detail-page summary + detail updates |
| `tests_view_details_section.html.j2` | shared detail partial | Included by `tests_view.html.j2` and `tests_view_detail_fragment.html.j2` |
| `tests_view_spsa_section.html.j2` | shared SPSA partial | Included by `tests_view.html.j2` |
| `tests_view_stats_section.html.j2` | shared stats partial | Included by `tests_view.html.j2` and `tests_view_detail_fragment.html.j2` |
| `tests_view_time_section.html.j2` | shared time partial | Included by `tests_view.html.j2` and `tests_view_detail_fragment.html.j2` |
| `tests_user_content_fragment.html.j2` | htmx content fragment | Swaps `#tests-user-content` |
| `user_management_content_fragment.html.j2` | htmx content fragment | Swaps `#user-management-content`; includes hidden-input OOB sync |
| `user_management_rows_fragment.html.j2` | shared row partial | Included by `user_management_content_fragment.html.j2` |
| `workers_content_fragment.html.j2` | htmx content fragment | Swaps `#workers-content`; includes hidden-input OOB sync |
| `workers_rows_fragment.html.j2` | shared row partial | Included by `workers_content_fragment.html.j2` |

Column notes: - **htmx content fragment**: returned as the main swap payload for a target. - **OOB / poll fragment**: returned for out-of-band updates, often on timers. - **shared partial**: included by another template rather than rendered as the top-level response body.

## Context contracts

Each template has an explicit context contract documenting the required keys. Templates do not fetch data; views provide fully shaped context.

### `base.html.j2`

Uses shared base context only. All other templates extend this.

### `actions.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `actions` | list of dicts | Action rows (see below) |
| `visible_actions` | int | Action rows rendered on the current page |
| `num_actions` | int | Total matching action count |
| `page_size` | int | Page size used for the current result set |
| `current_page` | int | 1-based page index rendered in the summary |
| `run_id_filter` | string | Active run filter, if the page is scoped to one run |
| `max_count` | int or None | Effective server-side action cap carried through GET forms |
| `sort` | string | Active sort field (`time`, `event`, `source`, `target`, `comment`) |
| `order` | string | Active sort direction (`asc` or `desc`) |
| `sort_summary` | string | Optional summary line describing capped full-result sorting scope |
| `filters` | dict | `{action, username, text, run_id}` |
| `pages` | list | Pagination items |

Each action row:

| Key | Type |
| --- | --- |
| `time_label` | string (preformatted) |
| `time_url` | string |
| `event` | string |
| `agent_name` | string |
| `agent_url` | string or None |
| `target_name` | string |
| `target_url` | string or None |
| `message` | string |

Full-page `/actions` responses may override the shared `open_graph` metadata with a summary of the first visible action row. htmx fragment responses do not render page-head tags.

### `contributors.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `is_monthly` | bool | Monthly vs all-time view |
| `is_approver` | bool | Current user is approver |
| `summary` | dict | `{testers, developers, active_testers, cpu_hours, games, tests}`; the template derives the CPU years |
| `users` | list of dicts | Contributor rows (current page) |
| `pages` | list | Pagination items |
| `search` | string | Active search query |
| `sort` | string | Active server sort field |
| `order` | string | Active sort direction |
| `view` | string | `paged` or `all` |
| `highlight` | string | Highlighted username, usually set by `findme` redirects |
| `is_truncated` | bool | Whether `view=all` was capped |
| `num_users` | int | Total filtered row count before paging/cap |
| `max_all` | int | Hard cap used for `view=all` |

Each contributor row: `username`, `user_url`, `rank`, `last_updated_label`, `last_updated_sort`, `games_per_hour`, `cpu_hours`, `games`, `tests`, `tests_repo_url`, `tests_user_url`.

### `elo_results.html.j2`

Included as a partial within run display templates.

| Key | Type | Description |
| --- | --- | --- |
| `run` | dict | The run whose results are shown |
| `show_gauge` | bool, optional | Render the live ELO gauge; default `false` |
| `results_info` | dict, optional | Formatted results; default `format_results(run)` |

### `login.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `remember_me_checked` | bool | Initial checked state for the Remember me checkbox |
| `remember_me_cookie_name` | string | UI-state cookie name mirrored by shared application JS |

Behavior notes:

- The form submits `stay_logged_in=0` through a hidden input and `stay_logged_in=1` through the checkbox so the server can distinguish an explicit opt-out from the checked/default case.
- When no `login_remember_me` UI cookie exists, the checkbox renders checked. An explicit opt-out stored in that cookie renders the checkbox unchecked on later visits.
- `login.html.j2` defaults `remember_me_cookie_name` to `login_remember_me` because the same template is also used for generic UI 403 rendering.
- `application.js` mirrors checkbox changes into the same UI-state cookie via `writeUiCookie()`, and `POST /login` refreshes it server-side for progressive enhancement.

### `machines_fragment.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `machines` | list of dicts | Machine rows |

Behavior notes:

- The fragment refreshes the `#workers-count` label out of band with the same short format used by the homepage shell: `Workers - <total>` or `Workers - <total> (<filtered>)`.
- Homepage polling keeps including the current filter form state, so fragment refreshes continue while the `q` search filter is active.
- The hidden homepage Workers header is refreshed from the current machine snapshot during `/tests` and page-1 same-route live run-table fragment rendering, so a collapsed panel still shows a live filtered count.

Each machine row: `username`, `country_code`, `concurrency`, `worker_url`, `worker_short`, `nps_m` (preformatted string), `max_memory`, `system`, `worker_arch`, `compiler_label`, `python_label`, `version_label`, `run_url`, `run_label`, `last_active_label`, `last_active_sort`.

### `nn_upload.html.j2`

| Key | Type |
| --- | --- |
| `upload_url` | string |
| `testing_guidelines_url` | string |
| `cc0_url` | string |
| `nn_stats_url` | string |

### `nns.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `filters` | dict | `{network_name, user, master_only}` |
| `pages` | list | Pagination items |
| `nns` | list of dicts | Neural network rows |

Each nn row: `time_label`, `name`, `name_url`, `user`, `first_test_label`, `first_test_url`, `last_test_label`, `last_test_url`, `downloads`, `is_master`.

Behavior notes:

- Search is URL-driven and rendered by the same `/nns` endpoint.
- htmx search updates only `#nns-content`; full-page rendering still works.
- Typing in `network_name` / `user` and toggling `master_only` triggers htmx requests, while submit remains available as a non-JS fallback.
- `network_name` and `user` are literal case-insensitive substring filters; regex metacharacters are escaped before reaching Mongo.
- `nns_content_fragment.html.j2` pairs a hidden `master_only=0` field with the checkbox `value="1"` and `data-ui-cookie-*` attributes, so unchecked htmx requests and cookie persistence stay synchronized without inline JS.
- The page shell owns the heading; the summary cards, explanatory copy, filter form (`#search_nn`), view toggle, pagination, and table live in the content fragment.

### `notfound.html.j2`

Shared base context only.

### `pagination.html.j2`

Reusable partial included by other templates.

| Key | Type | Description |
| --- | --- | --- |
| `pages` | list of dicts | Each: `{idx, url, state}` |

`state` values: `"active"`, `"disabled"`, or empty string.

### `rate_limits.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `server_rate_limit` | int | The server's remaining GitHub API calls, first paint |
| `server_reset` | string | When the GitHub rate limit resets, first paint |

### `run_table.html.j2`

| Key | Type |
| --- | --- |
| `header` | string or None |
| `count` | int or None |
| `count_text` | string or None |
| `runs` | list of run row dicts |
| `pages` | list of pagination items |
| `toggle` | string or None |
| `toggle_state` | `"Hide"` or `"Show"` |
| `show_delete` | bool |
| `active_run_filters` | dict or None |

### `run_tables.html.j2`

| Key | Type |
| --- | --- |
| `page_idx` | int |
| `prefix` | string |
| `pending_approval_runs` | list of run rows |
| `paused_runs` | list of run rows |
| `failed_runs` | list of run rows |
| `active_runs` | list of run rows |
| `active_count_text` | string |
| `active_run_filters` | dict or None |
| `finished_runs` | list of run rows |
| `num_finished_runs` | int |
| `finished_runs_pages` | list |

### `signup.html.j2`

| Key | Type |
| --- | --- |
| `recaptcha_site_key` | string |

### `sprt_calc.html.j2`

Shared base context only.

### `tasks_content_fragment.html.j2`

| Key | Type |
| --- | --- |
| `tasks` | list of task row dicts |
| `show_pentanomial` | bool |
| `show_residual` | bool |
| `run_id` | string |
| `show_task` | int |
| `sort` | string (default `"idx"`) |
| `order` | string (`"asc"` or `"desc"`, default `"desc"`) |
| `q` | string (combined worker/info filter) |
| `view` | string (`"paged"` or `"all"`) |
| `pages` | list of pagination dicts |
| `num_tasks` | int |
| `max_all` | int |
| `is_truncated` | bool |

Primary swap target: `#tasks-content`.

The fragment swaps the scrolling task table body into `#tasks-content` and refreshes the fixed controls/pagination wrappers out of band. OOB updates target `#tasks-view-controls`, `#tasks-pagination`, and the hidden form inputs `#tasks_sort`, `#tasks_order`, and `#tasks_view`.

Each task row: `task_id`, `row_class`, `worker_label`, `worker_url`, `info_label`, `last_updated_label`, `played_label`, `results_cells`, `crashes`, `time_losses`, `residual_label`, `residual_bg`.

### `tasks_controls_fragment.html.j2`

Included by `tests_view.html.j2` for the page-owned tasks table shell and returned OOB by `tasks_content_fragment.html.j2` after sort, filter, or view changes.

| Key | Type |
| --- | --- |
| `run_id` | string |
| `show_task` | int |
| `sort` | string |
| `order` | string |
| `q` | string |
| `view` | string |
| `num_tasks` | int |
| `max_all` | int |
| `is_truncated` | bool |

### `tasks_rows_fragment.html.j2`

Included by `tasks_content_fragment.html.j2`. Renders `<tr>` rows from the `tasks` list. No standalone context requirements beyond `tasks`, `show_pentanomial`, and `show_residual`.

### `tests.html.j2`

| Key | Type |
| --- | --- |
| `page_idx` | int |
| `cores` | int |
| `nps_m` | string |
| `games_per_minute` | int |
| `pending_hours` | string |
| `machines_shown` | bool |
| `machines_count` | int |
| `run_tables_ctx` | dict (for `run_tables.html.j2`) |

Behavior notes:

- `tests.html.j2` forwards the homepage Active-panel first-paint filter state through `run_tables_ctx`, including restored checkbox selections, filtered count text, and initial hide selectors.
- `run_tables_ctx` is intentionally lightweight. Unfinished-run rows omit `tasks`, `bad_tasks`, and `args.spsa.param_history`; task detail stays on the detail and tasks routes.
- Notification button state is not part of `run_tables_ctx` because it is derived from browser-local follow state at page load time.

### `tests_finished.html.j2`

| Key | Type |
| --- | --- |
| `filters` | dict (`ltc_only`, `success_only`, `yellow_only`, `username_query`, `text`, `max_count`, `mode`, the `*_query_string` keys, `filtered_query_suffix`) |
| `title` | string (the title suffix, e.g. `- Greens`) |
| `title_text` | string |
| `search_mode` | bool |
| `page_idx` | int |
| `finished_page_size` | int |
| `show_gauge` | bool |
| `finished_runs` | list of run rows |
| `visible_finished_runs` | int |
| `num_finished_runs` | int |
| `finished_runs_pages` | list |

### `tests_live_elo.html.j2`

| Key | Type |
| --- | --- |
| `run` | dict |
| `page_title` | string |
| `run_status_label` | string |
| `sprt_state` | string |
| `elo_raw`, `ci_lower_raw`, `ci_upper_raw` | float |
| `LLR_raw`, `LOS_raw`, `a_raw`, `b_raw` | float |
| `elo_value`, `ci_lower`, `ci_upper`, `LLR`, `LOS` | number |
| `W`, `L`, `D`, `games` | int |
| `w_pct`, `l_pct`, `d_pct` | float |
| `pentanomial` | list |
| `elo_model`, `elo0`, `elo1`, `alpha`, `beta` | mixed |

The page script renders LOS, LLR, and Elo gauges from `gauge-data` attributes. The Elo gauge supports a fixed default range (`[-4, +4]`) and a dynamic range. The gauge reports the uncapped server Elo value while the needle remains visually bounded by the active range.

### `tests_run.html.j2`

| Key | Type |
| --- | --- |
| `args` | dict (run arguments, empty for new test) |
| `is_rerun` | bool |
| `rescheduled_from` | string or None |
| `form_action` | string |
| `tests_repo_value` | string |
| `master_info` | dict or None |
| `valid_books` | iterable |
| `pt_info` | dict |
| `setup` | dict |
| `supported_arches` | list |
| `supported_compilers` | list |

Rendered structure notes:

- the preset chooser caption for `Test type` is a neutral heading row, not a `label` wrapper;
- the ellipsis control is a sibling button that toggles the extra preset blocks through Bootstrap collapse;
- the collapsed preset blocks expose stable ids so the toggle button can name them through `aria-controls`.

Tests repository contract:

- `tests_repo_value` is rendered from the current GitHub repo value for the form
- trailing-slash input is accepted on submit, but persisted run data is canonicalized and validated as the slash-free form

### `tests_stats.html.j2`

Page shell for `/tests/stats/{id}`. Includes the shared stats content fragment and, for unfinished non-SPSA runs, a visibility-aware htmx poller targeting `#tests-stats-content` using the dedicated raw-statistics poll cadence `poll.tests_stats`.

| Key | Type |
| --- | --- |
| `run` | dict |
| `page_title` | string |
| `stats` | dict |

### `tests_stats_content_fragment.html.j2`

Shared stats-body fragment used by both the full-page shell and `HX-Request` responses for `/tests/stats/{id}`.

| Key | Type |
| --- | --- |
| `run` | dict |
| `page_title` | string |
| `stats` | dict |

Rendered structure:

- `#tests-stats-content` root element
- original heading-and-table statistics layout shared by full-page and htmx renders
- SPRT bounds rendered as a table
- SPSA message rendered in place of the statistics tables when applicable

### `tests_user.html.j2`

| Key | Type |
| --- | --- |
| `username` | string |
| `is_approver` | bool |
| `run_tables_ctx` | dict |

### `tests_view.html.j2`

| Key | Type |
| --- | --- |
| `run` | dict |
| `run_args` | list of tuples `(name, value, url)` |
| `page_title` | string |
| `approver` | bool |
| `chi2` | value |
| `run_status_label` | string |
| `tasks_totals` | string (active workers summary) |
| `tasks_shown` | bool |
| `show_task` | int |
| `follow` | int |
| `has_pgns` | bool (run has stored task PGNs) |
| `can_modify_run` | bool |
| `same_user` | bool |
| `pt_info` | dict |
| `document_size` | int |
| `spsa_data` | dict or None |
| `notes` | list of strings |
| `warnings` | list of strings |
| `use_3dot_diff` | bool |
| `allow_github_api_calls` | bool |

Detail-page merged live polling contract:

- Unfinished runs render one visibility-aware htmx poller targeting `/tests/view/{id}/detail`.
- The poller includes `#tests-view-detail-expected`, a hidden server-owned input carrying the current canonical `expected` status.
- The poller uses `hx-swap="none"` and the dedicated cadence `poll.tests_view_detail`.
- The submitted `expected` value must match the page's canonical `run_status_label`: `active`, `paused`, or `pending`.
- The shared OOB targets are `#elo-<run_id>`, `#run-status-<run_id>`, `#tasks-totals`, `#tests-view-details`, `#tests-view-time`, and either `#tests-view-stats` or the embedded `#spsa-data-<run_id>` payload depending on the run type.
- `tests_view_detail_fragment.html.j2` OOB-refreshes the hidden expected-state input alongside the visible status label so paused/pending transitions can settle back to `204` on the next poll.
- SPSA runs keep the chart shell stable in `tests_view.html.j2`; live detail refreshes update only the embedded `application/json` payload contents so `static/js/spsa.js` can keep the payload node mounted, skip unchanged redraws, and redraw changed payloads without replacing the mounted chart shell. The full page renders the `% c` checkbox from a browser-readable cookie on first paint.

Detail-page tasks loader contract:

- When `tasks_shown` is true, `#tasks-content` starts an htmx load request from `tests_view.html.j2`.
- The template registers its swap observer and its `htmx:response:error` and `htmx:error` listeners before `await DOMContentLoaded()` so the initial `load` request cannot outrun the promise-resolution path.
- The swap observer matches on the **swap target**, through `onHtmxSwap((target) => target.id === "tasks-content", ...)`. htmx dispatches swap events on the element that issued the request, and `#tasks-content` is filled by two: its own `load`/poll triggers, and `#tasks-filters` when the reader types. An element-level `htmx:after:swap` listener on the panel sees only the first kind, so it misses every form-driven fill -- including the replacement for a load request the form aborted, which `hx-sync` makes a reachable case. `onHtmxSwap` also drops responses htmx did not swap, so an error is never recorded as a completed load.
- The same script also resolves immediately if `#tasks-content` is already marked loaded or already contains rows.

Run-table row contract:

- Run tables use the normal `.table-striped` contract.
- Active-run filtering emits a first-paint style block that hides excluded rows, and the Active filter script keeps the visible rows contiguous in tbody order so the normal `.table-striped` pattern stays correct while filters are active. The first-paint style block is hide-only and does not encode row parity.
- The Active row markup carries filter dimensions plus a source-order index for restoring the current server order after checkbox changes and OOB swaps; it does not use a row-parity contract.

### `tests_view_detail_fragment.html.j2`

Merged OOB poll fragment returned by `/tests/view/{id}/detail`. Composes the shared detail-page summary + detail sections with `hx-swap-oob` enabled.

| Key | Type |
| --- | --- |
| `run` | dict |
| `run_status_label` | string |
| `tasks_totals` | string |
| `run_args` | list of tuples `(name, value, url)` |
| `approver` | bool |
| `chi2` | dict |
| `document_size` | int |
| `spsa_data` | dict or None |

Rendered behavior:

- always includes `elo_results_fragment.html.j2`
- always includes `tests_view_details_section.html.j2`
- always includes `tests_view_time_section.html.j2`
- includes `tests_view_stats_section.html.j2` for non-SPSA runs
- emits an OOB `#spsa-data-<run_id>` `application/json` payload update for SPSA runs using `innerHTML` so the existing script node stays mounted

### `tests_view_details_section.html.j2`

Shared detail-table section used by `tests_view.html.j2` and the live detail fragment.

| Key | Type |
| --- | --- |
| `run` | dict |
| `run_args` | list of tuples `(name, value, url)` |
| `approver` | bool |
| `document_size` | int |

Rendered structure:

- `#tests-view-details` root element
- run argument rows, document size, actions link, raw statistics link when applicable, and approver link when present

### `tests_view_stats_section.html.j2`

Shared compact chi-square block for the test detail page.

| Key | Type |
| --- | --- |
| `chi2` | dict |

Rendered structure:

- `#tests-view-stats` root element
- compact `chi^2`, `dof`, and `p-value` table

### `tests_view_time_section.html.j2`

Shared time block for the test detail page.

| Key | Type |
| --- | --- |
| `run` | dict |

Rendered structure:

- `#tests-view-time` root element
- start time and last updated rows

### `tests_view_spsa_section.html.j2`

Shared SPSA chart section for the test detail page.

| Key | Type |
| --- | --- |
| `run` | dict |
| `spsa_data` | dict or None |
| `spsa_percentage_checked` | bool |

Rendered structure:

- `#tests-view-spsa` root element
- DOM-embedded `application/json` payload for the current SPSA state, exposing `param_names` plus server-shaped `chart_rows`; those chart rows fold together the start row, compact sampled history rows, and the current live point, with `c_values` present when `% c` mode can be rendered
- cookie-backed `% c` checkbox rendered from `spsa_percentage_checked`
- `#spsa_history_scroll` retained scroll container
- CSS-owned `#spsa_history_plot` shell for the fixed-size chart (Google Charts uses 1000x500; layout and scrolling handled by CSS and the scroll container)
- chart toolbar and chart container used by `static/js/spsa.js`

### `user.html.j2`

| Key | Type |
| --- | --- |
| `profile` | bool |
| `user` | dict (`username`, `email`, `tests_repo`, etc.) |
| `limit` | value |
| `hours` | int |
| `extract_repo_from_link` | string |
| `form_action` | URL |
| `registration_time_label` | string |
| `blocked` | bool |

Tests repository contract:

- `user["tests_repo"]` is persisted as the canonical slash-free GitHub repo URL
- profile form submissions may include a trailing slash, but stored user data is normalized and validated before save

### `user_management.html.j2`

| Key | Type |
| --- | --- |
| `all_count` | int |
| `pending_count` | int |
| `blocked_count` | int |
| `idle_count` | int |
| `approvers_count` | int |
| `group` | string |
| `sort` | string |
| `order` | string |
| `q` | string |
| `view` | string |
| `pages` | list |
| `selected_users` | list of user row dicts |
| `num_selected_users` | int |
| `max_all` | int |
| `is_truncated` | bool |

Each user row: `username`, `user_url`, `registration_label`, `groups`, `groups_label`, `email`.

### `workers.html.j2`

| Key | Type |
| --- | --- |
| `show_admin` | bool |
| `worker_name` | string |
| `last_updated_label` | string |
| `message` | string |
| `blocked` | bool |
| `show_email` | bool |
| `filter_value` | string |
| `sort` | string |
| `order` | string |
| `q` | string |
| `view` | string |
| `pages` | list |
| `blocked_workers` | list of dicts |
| `num_workers` | int |
| `max_all` | int |
| `is_truncated` | bool |

Each blocked worker row: `worker_name`, `last_updated_label`, `actions_url`, `owner_email`, `mailto_url`.

## Fragment context contracts

Fragment templates receive the shared base context (via `build_template_context()`) plus handler-specific keys.

### `actions_content_fragment.html.j2`

Same context as `actions.html.j2` (`actions`, `visible_actions`, `num_actions`, `page_size`, `current_page`, `run_id_filter`, `max_count`, `sort`, `order`, `sort_summary`, `filters`, `pages`).

`actions.html.j2` renders the page shell and filter form, then includes `actions_content_fragment.html.j2` inside `#actions-content`.

### `active_run_filters_fragment.html.j2`

| Key | Type |
| --- | --- |
| `active_run_filters` | dict or `None` (parsed cookie state) |
| `cookies.ui_state_max_age` | int (cookie max-age seconds) |

Renders the faceted filter panel (SPRT/SPSA/NumGames, STC/LTC, ST/SMP) above the Active runs table, wherever `run_table.html.j2` includes it: the Active panel on page 1 of a run list without a username. Server-side rendering of initial checkbox state eliminates the flash where all rows are briefly visible before JS reapplies the persisted filter.

### `contributors_content_fragment.html.j2`

Same context as the content area of `contributors.html.j2`: `users`, `pages`, `sort`, `order`, `view`, `num_users`, `max_all`, `is_truncated`, and, in the htmx response only, `is_hx`.

Sortable headers are dual-mode links (`href` + `hx-get`) targeting `#contributors-content` with `hx-push-url="true"`.

The outer contributors search form keeps `view`, `sort`, and `order` in hidden inputs. htmx fragment responses refresh those inputs out of band so later form submissions preserve the live table state.

### `contributors_rows_fragment.html.j2`

| Key | Type |
| --- | --- |
| `users` | list of contributor row dicts |

### `tests_run_tables_fragment.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `panels` | list of dicts | Each: `tbody_id`, `rows`, `show_delete`, `empty_text` |
| `count_updates` | list of dicts | Each: `id`, `text` (OOB count spans) |
| `machines_count` | int or absent | Worker count (OOB `#workers-count`) |
| `workers_count_text` | string or absent | The workers-counter text for `#workers-count` |
| `stats` | dict or absent | Homepage stats (OOB, omitted when filtered by user) |
| `stop_polling` | bool or absent | Remove `#run-tables-poller` and stop the poll |

Behavior notes:

- The unfinished-run portion of this fragment uses the same lightweight row shape as `run_tables_ctx`.
- When homepage workers filters are active, the fragment recomputes the `#workers-count` filtered value from the current machine snapshot instead of trusting the last cookie-backed filtered count.
- `workers_count_text` remains the single shared workers-counter string used by the homepage shell and OOB fragment updates.

### `elo_results_fragment.html.j2`

Shared OOB partial included by `tests_view_detail_fragment.html.j2`.

| Key | Type |
| --- | --- |
| `run` | dict |
| `run_status_label` | string, optional (OOB `#run-status-{id}`; computed from the run when absent) |
| `tasks_totals` | string, optional (OOB `#tasks-totals`) |
| `show_gauge`, `results_info` | optional, as for `elo_results.html.j2` |

### `homepage_stats_fragment.html.j2`

| Key | Type |
| --- | --- |
| `cores` | int |
| `nps_m` | string |
| `games_per_minute` | int |
| `pending_hours` | string |

### `live_elo_fragment.html.j2`

| Key | Type |
| --- | --- |
| `run` | dict |
| `run_status_label` | string |
| `sprt_state` | string |
| `elo_raw`, `ci_lower_raw`, `ci_upper_raw` | float |
| `LLR_raw`, `LOS_raw`, `a_raw`, `b_raw` | float |
| `elo_value`, `ci_lower`, `ci_upper`, `LLR`, `LOS` | number |
| `W`, `L`, `D`, `games` | int |
| `w_pct`, `l_pct`, `d_pct` | float |
| `pentanomial` | list |
| `elo_model`, `elo0`, `elo1`, `alpha`, `beta` | mixed |

This fragment is returned by `/tests/live_elo_update/{id}` and swaps `#live-elo-data` out-of-band while preserving the current client-side gauge mode.

### `machines_fragment.html.j2`

| Key | Type |
| --- | --- |
| `machines` | list of machine row dicts |
| `sort` | string |
| `order` | string |
| `q` | string |
| `current_page` | int |
| `my_workers` | bool |
| `pages` | list |
| `machines_count` | int |
| `workers_count_text` | string (OOB `#workers-count`) |

This fragment also refreshes the hidden sort/page inputs and the `#workers-count` label out of band so homepage polling, sorting, paging, and filters stay in sync without replacing the filter controls themselves.

The machine rows come from the current server machine snapshot. The same snapshot is reused by `/tests` and its page-1 same-route live run-table fragment when they need to render a live filtered workers count while the table itself is collapsed.

### `nns_content_fragment.html.j2`

Same context as `nns.html.j2` (`filters`, `pages`, `nns`, `sort`, `order`, `view`, `num_nns`, `max_all`, `is_truncated`) plus `nns_summary` with `nets`, `master_nets`, `contributors`, and `downloads`.

The fragment owns the NNS summary cards and the explanatory copy as well as the filter form, view toggle, pagination, and table so filtered htmx responses keep the full vertical page order synchronized with the current server state.

The `network_name` and `user` filters remain literal case-insensitive substring searches. Regex syntax is not part of the public contract.

Sortable headers are dual-mode links (`href` + `hx-get`) targeting `#nns-content` with `hx-push-url="true"`.

The fragment-owned GET form keeps `view`, `sort`, and `order` in hidden inputs. Because the full filter form is inside the swapped fragment, htmx responses do not need out-of-band hidden-input refresh for this page.

The `/rate_limits` page client row and the sidebar `GitHub Rate Limits` link both project their cadence from `poll.rate_limits_github`.

The `/rate_limits` server row projects its separate cadence from `poll.rate_limits_server`.

The sidebar link keeps a fixed label and uses the same red status styling as the pending-users sidebar item whenever the client GitHub budget is below the threshold.

The sidebar link is rendered directly in `base.html.j2` with the same `data-poll-seconds` cadence that `application.js` uses for client-side GitHub budget checks.

`application.js` owns the full browser-side lifecycle for that client poll:

- it initializes on every page that renders `#rate-limits-nav-link`;
- it pauses its timer while the document is hidden;
- it refreshes immediately when the page becomes visible again and on window `focus`;
- it refreshes again on persisted `pageshow` so bfcache restores do not leave stale sidebar or `/rate_limits` client-row state.

The `/rate_limits/server` endpoint returns a tiny inline HTML fragment: the remaining server budget plus an out-of-band `#server_reset` update. This route does not need a dedicated Jinja template.

The pending-users sidebar wrapper, the `/rate_limits/server` poller, and the client-side GitHub rate-limit refresh all use `visibilitychange` activation, so an active page refreshes after a hidden tab becomes visible again. The pending-users wrapper and the `/rate_limits/server` poller also fire on `load`, so each fetches once on page load beside its server-rendered value.

The GitHub sidebar link also restores the last known client warning state from local storage before first paint so page navigation does not briefly flash the link back to its normal color.

### `run_table_row_fragment.html.j2`

| Key | Type |
| --- | --- |
| `row` | run row dict |
| `show_delete` | bool |
| `show_gauge` | bool |

The `row` payload is shaped by `build_run_table_rows()` in `server/fishtest/http/template_helpers.py`. Its `diff_url` field is derived from the canonical run diff URL builder.

### `tasks_content_fragment.html.j2`

Content fragment for the tasks table. Swaps the scrolling table markup into `#tasks-content` and refreshes the fixed controls/pagination wrappers OOB. Includes `tasks_rows_fragment.html.j2` for the `<tr>` rows. Context keys: `tasks`, `show_pentanomial`, `show_residual`, `run_id`, `show_task`, `sort`, `order`, `q`, `view`, `pages`, `num_tasks`, `max_all`, `is_truncated`.

### `tasks_rows_fragment.html.j2`

Row renderer included by `tasks_content_fragment.html.j2`. Context keys: `tasks`, `show_pentanomial`, `show_residual`.

### `tests_filter_tabs_fragment.html.j2`

Reusable partial included by `tests_finished_content_fragment.html.j2`, `tests_finished_results_fragment.html.j2` and `tests_user_content_fragment.html.j2` for filter tab buttons (All / Green / Yellow / LTC).

| Key | Type | Description |
| --- | --- | --- |
| `filters` | dict | `{success_only, yellow_only, ltc_only}` |
| `hx_target` | string | Target element ID for `hx-target` |
| `base_url` | string | Base URL for filter links (default `/tests/finished`) |

When present, `filters.all_query_string`, `filters.green_query_string`, `filters.yellow_query_string`, `filters.ltc_query_string` and `filters.filtered_query_suffix` preserve additional GET state such as username search fields while the user switches between the tab filters.

### `tests_finished_content_fragment.html.j2`

| Key | Type |
| --- | --- |
| `filters` | dict (`ltc_only`, `success_only`, `yellow_only`, `username_query`, `text`, `max_count`) |
| `finished_runs` | list of run rows |
| `num_finished_runs` | int |
| `finished_page_size` | int |
| `finished_runs_pages` | list |

This full-page include owns the tab strip and the search-first `/tests/finished` GET form. Username substring uses the cached list of usernames in the users collection (`userdb.get_usernames()`, cached 30 s) and the exact-username finished-run path, while the `text` field searches the run info of finished rows (see the finished tests parameters in [UI routes](<http://official-stockfish.github.io/fishtest/ui-reference/index.md#finished-tests-testsfinished-query-parameters>)). The form preserves the effective `max_count` cap so tab switches and pagination stay on the same server-authoritative finished-tests query contract.

### `tests_finished_results_fragment.html.j2`

| Key | Type |
| --- | --- |
| `filters` | dict |
| `finished_runs` | list of run rows |
| `num_finished_runs` | int |
| `visible_finished_runs` | int |
| `finished_page_size` | int |
| `finished_runs_pages` | list |
| `page_idx` | int |
| `search_mode` | bool |
| `is_hx` | bool |

This is the htmx fragment returned by `/tests/finished` for fragment requests. It updates `#tests-finished-content` and, in navigation mode, piggy-backs an out-of-band replacement of the tab wrapper so the active tab stays aligned with the pushed URL after htmx tab clicks.

### `tests_user_content_fragment.html.j2`

| Key | Type |
| --- | --- |
| `username` | string |
| `is_approver` | bool |
| `filters` | dict |
| `run_tables_ctx` | dict |

### `user_management_content_fragment.html.j2`

Same context as the content area of `user_management.html.j2`: `group`, `sort`, `order`, `q`, `view`, `pages`, `selected_users`, `num_selected_users`, `max_all`, `is_truncated`, and, in the htmx response only, `is_hx`.

The outer GET form keeps `sort`, `order`, and `view` in hidden inputs. htmx fragment responses refresh those inputs out of band so later form-triggered requests preserve the current table state.

### `user_management_rows_fragment.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `selected_users` | list of user row dicts | Filtered user rows |

### `workers_content_fragment.html.j2`

Same context as the content area of `workers.html.j2`: `filter_value`, `sort`, `order`, `q`, `view`, `pages`, `blocked_workers`, `show_email`, `num_workers`, `max_all`, `is_truncated`, and, in the htmx response only, `is_hx`.

Sortable headers are dual-mode links (`href` + `hx-get`) targeting `#workers-content` with `hx-push-url="true"`.

The outer GET form keeps `sort`, `order`, and `view` in hidden inputs. htmx fragment responses refresh those inputs out of band so later form-triggered requests preserve the current table state.

## htmx shell-state rule

When a GET form stays outside the swapped fragment but the fragment owns sort/view/pagination links, the fragment must refresh any stateful hidden form inputs out of band. This repo uses the same pattern as `/tests/machines`:

- keep stable ids on the outer hidden inputs;
- return matching hidden inputs from the fragment with `hx-swap-oob="true"`;
- render those out-of-band inputs only in the htmx response, behind `{% if is_hx | default(false) %}`, with the view passing `is_hx` through `extra_context`. A content fragment that the full page includes would otherwise render every id twice.

That keeps later form submissions aligned with the current server-authoritative table state without introducing page-specific synchronization JavaScript.

### `workers_rows_fragment.html.j2`

| Key | Type | Description |
| --- | --- | --- |
| `blocked_workers` | list of dicts | Filtered worker rows |
| `show_email` | bool | Show owner email column |

## Authoring rules

1. **Templates are declarative**: all data shaping stays in view handlers. Templates receive pre-computed values and render them.
2. **JavaScript data**: pass via the `|tojson` filter. Never interpolate Python values directly into `<script>` blocks.
3. **Display strings**: prefer preformatted values (e.g., `*_label` keys) over formatting in templates.
4. **URLs**: prefer explicit URL keys (e.g., `*_url`) over building URLs in templates.
5. **Macros**: use Jinja2 macros for repeated patterns (pagination, run tables).
6. **Request object**: templates read `request.url` and `request.url.path` for the current address (form actions, query links); nothing else on the request.
7. **Escaping boundary**: view handlers pass raw strings. Jinja2 autoescape handles HTML-escaping at render time. Do not pre-escape values into plain strings with `html.escape()` in Python -- that causes double-escaping. Escaping in Python is appropriate when constructing a `Markup` value (escape untrusted input first, then wrap).
8. **`|safe` filter**: use only on values already typed as `Markup`, or after an explicit `|e` escape followed by controlled transformations (e.g., `{{ value | e | replace("\n", "<br>") | safe }}`).
9. **External links**: every `target="_blank"` anchor must include `rel="noopener noreferrer"` to prevent reverse-tabnabbing.
10. **No inline event handlers**: use unobtrusive JavaScript (`addEventListener` or delegated listeners) instead of `onclick`, `onsubmit`, or similar HTML attributes.
11. **Fragment templates do not extend `base.html.j2`**: htmx fragment templates are standalone files. They receive the shared base context via `build_template_context()` but must not use `{% extends %}` or `{% block %}` directives.
12. **OOB swap elements carry their own `hx-swap-oob` attribute**: the view handler does not need to set response headers for OOB updates. Each OOB element in the fragment template declares its own ID and swap strategy (e.g., `<span id="count" hx-swap-oob="innerHTML">`).
13. **Row OOB requires a `<template>` wrapper, and the wrapper carries the attributes**: a `<tr>` cannot stand on its own in parsed HTML. Put `id` and `hx-swap-oob` on the `<template>` itself:

    ```
    <template id="my-table" hx-swap-oob="innerHTML">
      {% for row in rows %}...{% endfor %}
    </template>
    ```

    htmx locates out-of-band elements with `querySelectorAll`, which does not descend into template content, so an `hx-swap-oob` nested inside the wrapper is silently ignored. htmx strips the wrapper before the swap. Targets that can stand alone, such as a `<div>`, need no wrapper.
14. **DOM API over `innerHTML` in error handlers**: JavaScript retry-button construction must use `createElement` / `textContent` / `setAttribute` instead of string concatenation with `innerHTML`. This prevents XSS from error messages and avoids escaping issues.
15. **Unicode over HTML entities**: use literal Unicode characters (e.g., `<=` or the actual symbol) instead of HTML entities (`&#8804;`, `&gt;`) in templates. This eliminates the need for `|safe` on values that contain the entity.
16. **Explicit CSRF hidden fields**: every server-rendered `<form>` that posts to a `require_csrf` route must include an explicit hidden input:

    ```
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
    ```

    The meta-tag CSRF token is a fallback for htmx headers; forms must not rely on it as the sole CSRF transport.
17. **Polling trigger policy**: every periodic htmx poller must include three trigger components: (a) a periodic trigger gated on `document.visibilityState === 'visible'`, written `every[<condition>] Ns`, the one spelling the templates use, (b) an immediate `visibilitychange[...] from:document` refresh, and (c) for section-scoped pollers, a gate on the section's expanded state. See [Architecture](<http://official-stockfish.github.io/fishtest/architecture/index.md>) for the full policy.

## Adding a new template

### Page template

1. Create `templates/mypage.html.j2` extending `base.html.j2`:

   ```
   {% extends "base.html.j2" %}
   {% block body %}
   ...
   {% endblock %}
   ```
2. Define the context contract (required keys and types) in this document.
3. Build the context in the view handler using the shared base context plus page-specific keys.
4. Return the context dict from the handler; `_dispatch_view` renders the template specified in the route config's `renderer` key.
5. Add a test that verifies the template renders without errors.

### Fragment template

1. Create `templates/mypage_fragment.html.j2` as a standalone file (no `{% extends %}`).
2. Define the context contract in this document.
3. In the view handler, use `_render_hx_fragment()` to return the fragment when `HX-Request` is present:

   ```
   return (
       _render_hx_fragment(request, "mypage_fragment.html.j2", context)
       or context
   )
   ```
4. For OOB elements, add `hx-swap-oob` attributes directly on elements inside the fragment template. Rows travel inside a `<template>` that carries the `id` and `hx-swap-oob` attributes.
5. Add a test that verifies both the full-page and fragment responses.
