{% import "macros.html" as m %}
<p class="lead pw-lede">This is the lease registry made visible — the warden’s single source of truth for
  what’s running. The console reads it; it never decides run-state. A second launch of a running test
  is <b>coalesced</b> (one execution, recorded); a different test <b>queues</b>.</p>

<div class="pw-ctrlbar">
  {% if broker == 'down' %}
  <span class="service-status service-status--down">warden down · fail-closed</span>
  {% else %}
  <span class="service-status service-status--up">warden up</span>
  {% endif %}
  <span class="authority-badge">Authority</span>
  <span class="pw-spacer"></span>
  <span class="telemetry-marker"><span class="live-dot"></span>live · ephemeral, not provenance</span>
</div>

{% if not data.cards %}
  {{ m.empty('▮', 'Nothing running',
     'No lease is held. Start a test from the Catalog, or via the CLI / agent / CI — they all funnel
      through this one warden.') }}
{% else %}
  {% for card in data.cards %}
  <article class="pw-runcard">
    <header class="pw-runcard__h">
      <span class="authority-badge">Lease</span>
      <span class="pw-runcard__op mono">{{ card.test_id }}</span>
      <span class="pw-holder mono">key {{ card.key }}</span>
      <span class="origin-badge origin-badge--{{ card.origin }}">{{ card.origin }}{% if card.who %}:{{ card.who }}{% endif %}</span>
      {% if card.origin == 'ci' %}<span class="pw-cijoined mono" title="A CI pipeline joined this lease — single-flight holds across CI + local">CI · joined lease</span>{% endif %}
      <span class="pw-spacer"></span>
      <span class="status-chip status-chip--{{ 'error' if card.stalled else 'running' }}"><span class="status-chip__dot"></span>{{ 'STALLED' if card.stalled else 'running' }}</span>
      <form class="pw-cancel" method="post" action="/p/{{ project.slug }}/cancel"
            title="Signal this run and release its lease — the queue then advances">
        <input type="hidden" name="run_id" value="{{ card.run_id }}">
        <button class="button button-sm pw-cancel__btn" type="submit">Cancel</button>
      </form>
    </header>
    <div class="pw-runcard__b">
      <div class="pw-rmeta mono">
        <span>{{ card.progress_pct }}%</span>
        <span>run {{ card.run_id }}</span>
        <span>{% if card.stalled %}no heartbeat · {{ card.heartbeat_age }}s (TTL {{ card.ttl_s }}s){% else %}heartbeat {{ card.heartbeat_age }}s ago{% endif %}</span>
        <span>watchers {{ card.watchers }}</span>
      </div>
      <div class="progress"><div class="progress__fill{% if card.progress_pct >= 100 %} progress__fill--done{% endif %}" style="width:{{ card.progress_pct }}%"></div></div>
    </div>
  </article>
  {% endfor %}

  <div id="pw-live" data-slug="{{ project.slug }}" data-run="{{ data.selected_run }}" data-seq="{{ data.last_seq }}">
  <div class="pw-console-h mono">⬗ LIVE LOG · <span class="telemetry-marker">telemetry</span> ·
    <span id="pw-ev-count">{{ data.console|length }}</span> events · run
    {% if data.cards|length > 1 %}<select id="pw-run-select" class="pw-run-select" aria-label="Select a running run" hidden>{% for c in data.cards %}<option value="{{ c.run_id }}"{% if c.run_id == data.selected_run %} selected{% endif %}>{{ c.test_id }} · {{ c.run_id }}</option>{% endfor %}</select>{% else %}{{ data.selected_run }}{% endif %}
    <span class="pw-spacer"></span>
    <button type="button" id="pw-pause" class="button button-sm pw-pause" aria-pressed="false" hidden>Pause</button>
  </div>
  <div class="console">
    {% for line in data.console %}
    <div class="console__line"><span class="console__seq">#{{ line.seq }}</span><span class="console__ts">{{ line.ts }}</span><span class="console__kind kind--{{ line.kind_class }}">{{ line.kind }}</span><span class="console__text">{{ line.text }}</span></div>
    {% else %}
    <div class="console__line"><span class="console__text">stream idle — no telemetry recorded for this run yet</span></div>
    {% endfor %}
  </div>
  </div>
{% endif %}

{% if data.queue %}
<section class="card pw-panel">
  <div class="pw-panel__head"><span class="pw-panel__title">Queue · serialized (one run at a time)</span></div>
  <div class="pw-panel__body">
  {% for q in data.queue %}
    <div class="pw-qrow"><span class="pw-qpos mono">{{ q.position }}</span><span class="pw-trow__id mono">{{ q.test_id }}</span>{{ m.origin_badge(q.origin) }}<span class="pw-spacer"></span><span class="mono muted">{{ q.run_id }}</span></div>
  {% endfor %}
  </div>
</section>
{% endif %}

<section class="card pw-panel">
  <div class="pw-panel__head"><span class="pw-panel__title">Authority audit</span><span class="pw-spacer"></span><span class="authority-badge">visible proof single-flight held</span></div>
  {% if data.audit %}
  <div class="pw-audit">
    {% for a in data.audit %}
    <div class="pw-arow mono"><span class="pw-ats">{{ a.ts }}</span><span class="pw-ak pw-ak--{{ a.event }}">{{ a.event }}</span><span class="pw-atx">{{ a.note or a.test_id }}{% if a.attached_to %} → {{ a.attached_to }}{% endif %}{% if a.origin %} · {{ a.origin }}{% endif %}</span></div>
    {% endfor %}
  </div>
  {% else %}
  <div class="pw-panel__body muted">No coalesce / queue / reclaim events yet — every refusal will be recorded
    here as visible proof the lock held.</div>
  {% endif %}
</section>
