feedback: thumb up/down on logs + reviewer self-score 0-10

Two unrelated features bundled because they ship together and share
migration 0028.

Strategic-log feedback (thumb up/down):
- New strategic_log_feedback table with UNIQUE(log_id, user_id) so each
  user has one vote per log, flippable in place (up -> down -> clear).
  UI shows aggregate counts only.
- app/services/log_feedback.py: set_vote, get_counts, sign/verify
  feedback tokens (same itsdangerous pattern as auth.sign_pending,
  30-day TTL for email links).
- POST /api/log/{id}/feedback: web vote, auth required, returns counts
  + the requesting user's own vote.
- GET /feedback?token=...&vote=...: email-link target, no auth, signed
  token encodes (user, log, vote), renders feedback_thanks.html.
- partials/log.html: thumbs row below content, JS-driven swap via the
  POST endpoint. Dashboard latest-log card and /log page both render
  this partial via htmx, so the buttons appear in all three surfaces.
- digest emails: a "How was today's read?" row above the unsub footer,
  with signed-token URLs against the latest StrategicLog at send time.
  Plain-text fallback included.

Reviewer self-score (0-10):
- _SYSTEM_PROMPT asks for an integer score with anchors (10 exemplary,
  5 borderline, 0 unfit). Verdict gains score: int | None.
- Deterministic-layer hits get score=0 (hard rule, no nuance);
  error rows get None; LLM rows get the model's score clamped 0..10.
- ReviewerVerdict.score, StrategicLog.reviewer_score, and
  IndicatorSummary.reviewer_score all new SMALLINT NULL columns.
- ai_log_job + indicator_summary_job persist verdict.score onto their
  content rows when committing the row alongside content.

Tests:
- tests/test_strategic_log_feedback.py: vote, flip, clear, aggregate
  across users, invalid vote, token round-trip + tamper + garbage +
  'clear' not signable for email path.
- tests/test_output_review.py: score parsing, clamping (>10, <0),
  missing/non-numeric -> None, deterministic-layer score=0.

Full suite: 427 passed (was 412), 5 skipped, no regressions.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-05-29 21:28:03 +02:00
parent f3ac65f8f7
commit 8946dee2e0
14 changed files with 962 additions and 14 deletions

View file

@ -0,0 +1,30 @@
{% extends "public_base.html" %}
{% block title %}{{ BRAND_NAME }} &middot; Feedback{% endblock %}
{% block main %}
<section class="public-section" style="max-width:520px; margin:60px auto; text-align:center;">
{% if ok %}
<h1 class="public-section__head" style="margin-bottom:14px;">
{% if vote == 'up' %}Thanks for the thumbs up.{% else %}Thanks for the thumbs down.{% endif %}
</h1>
<p style="color:var(--muted); font-size:14px; line-height:1.55;">
Your vote on this strategic log is recorded.
{% if counts %}
Current tally: <strong>{{ counts.up }}</strong> &#x1F44D; ·
<strong>{{ counts.down }}</strong> &#x1F44E;.
{% endif %}
</p>
<p style="margin-top:24px;">
<a href="/" class="btn-primary">Open the dashboard</a>
</p>
{% else %}
<h1 class="public-section__head" style="margin-bottom:14px;">Couldn&rsquo;t record that vote</h1>
<p style="color:var(--muted); font-size:14px; line-height:1.55;">
{{ message or "Something went wrong; please try again from the dashboard." }}
</p>
<p style="margin-top:24px;">
<a href="/" class="btn-primary">Open the dashboard</a>
</p>
{% endif %}
</section>
{% endblock %}

View file

@ -9,4 +9,78 @@
title="Last generated {{ log.generated_at.strftime('%Y-%m-%d %H:%M UTC') }}">
{{ log.content_html | safe | glossary(tone) }}
</div>
{% if log.feedback %}
{# Anonymous-in-UI thumb up/down. Server stores (user, log, vote) for dedup
so a vote can be flipped; UI shows aggregate counts only.
POST clicks JSON-fetch and swap this partial back in place — no full
reload, the log content stays put. #}
<div class="log-feedback" id="log-feedback-{{ log.id }}"
data-log-id="{{ log.id }}"
style="display:flex; align-items:center; gap:14px; margin-top:18px;
padding-top:14px; border-top:1px solid var(--border);
font-size:13px; color:var(--muted);">
<span>Was this useful?</span>
<button type="button" class="log-feedback__btn log-feedback__btn--up"
data-vote="up"
aria-pressed="{{ 'true' if log.feedback.user_vote == 'up' else 'false' }}"
title="Helpful"
style="background:none; border:1px solid var(--border);
padding:4px 10px; border-radius:4px; cursor:pointer;
{% if log.feedback.user_vote == 'up' %}background:var(--accent-bg, #eef);
border-color:var(--accent);{% endif %}">
👍 <span class="log-feedback__count">{{ log.feedback.up }}</span>
</button>
<button type="button" class="log-feedback__btn log-feedback__btn--down"
data-vote="down"
aria-pressed="{{ 'true' if log.feedback.user_vote == 'down' else 'false' }}"
title="Not useful"
style="background:none; border:1px solid var(--border);
padding:4px 10px; border-radius:4px; cursor:pointer;
{% if log.feedback.user_vote == 'down' %}background:var(--accent-bg, #fee);
border-color:var(--accent);{% endif %}">
👎 <span class="log-feedback__count">{{ log.feedback.down }}</span>
</button>
<span class="log-feedback__status" aria-live="polite"
style="font-size:12px; opacity:0.7;"></span>
</div>
<script>
(function () {
var root = document.getElementById('log-feedback-{{ log.id }}');
if (!root || root.dataset.wired) return;
root.dataset.wired = '1';
var logId = root.dataset.logId;
var statusEl = root.querySelector('.log-feedback__status');
root.querySelectorAll('.log-feedback__btn').forEach(function (btn) {
btn.addEventListener('click', async function () {
var currentlyPressed = btn.getAttribute('aria-pressed') === 'true';
// Clicking the already-pressed vote clears it (toggle off).
var vote = currentlyPressed ? 'clear' : btn.dataset.vote;
statusEl.textContent = '…';
try {
var r = await fetch('/api/log/' + logId + '/feedback', {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({vote: vote}),
credentials: 'same-origin',
});
if (!r.ok) throw new Error('Vote failed: ' + r.status);
var data = await r.json();
// Update counts + pressed state in place. Server is source of truth.
var upBtn = root.querySelector('.log-feedback__btn--up');
var downBtn = root.querySelector('.log-feedback__btn--down');
upBtn.querySelector('.log-feedback__count').textContent = data.up;
downBtn.querySelector('.log-feedback__count').textContent = data.down;
upBtn.setAttribute('aria-pressed', data.user_vote === 'up' ? 'true' : 'false');
downBtn.setAttribute('aria-pressed', data.user_vote === 'down' ? 'true' : 'false');
statusEl.textContent = 'thanks';
setTimeout(function () { statusEl.textContent = ''; }, 1800);
} catch (e) {
statusEl.textContent = 'could not save';
}
});
});
})();
</script>
{% endif %}
{% endif %}