Files

173 lines
5.7 KiB
Go

package pipeline
import (
"context"
"database/sql"
"sort"
"strconv"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// AttentionAckRetention bounds how long an acknowledgement is kept. The
// Needs-you list shows only today's work and a chat watermark is refreshed
// whenever the chat is opened, so a month-old acknowledgement protects nothing.
const AttentionAckRetention = 30 * 24 * time.Hour
// initAttentionAcks creates the two additive acknowledgement tables. They are
// tables of their own so no run or session schema changes; run_id is kept as a
// column so pruning against pipeline_runs uses an integer comparison.
func (s *Store) initAttentionAcks() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS attention_dismissals (
key TEXT PRIMARY KEY, run_id INTEGER NOT NULL, dismissed_at INTEGER NOT NULL);
CREATE TABLE IF NOT EXISTS chat_seen_watermarks (
session_id TEXT PRIMARY KEY, seen_at INTEGER NOT NULL, acked_at INTEGER NOT NULL);`)
return err
}
type attentionQuerier interface {
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}
func readAttentionAcks(ctx context.Context, q attentionQuerier) (protocol.AttentionAcks, error) {
acks := protocol.AttentionAcks{Dismissed: []string{}, ChatSeen: map[string]int64{}}
rows, err := q.QueryContext(ctx, `SELECT key FROM attention_dismissals ORDER BY key`)
if err != nil {
return acks, err
}
for rows.Next() {
var key string
if err := rows.Scan(&key); err != nil {
rows.Close()
return acks, err
}
acks.Dismissed = append(acks.Dismissed, key)
}
rows.Close()
if err := rows.Err(); err != nil {
return acks, err
}
rows, err = q.QueryContext(ctx, `SELECT session_id, seen_at FROM chat_seen_watermarks`)
if err != nil {
return acks, err
}
defer rows.Close()
for rows.Next() {
var id string
var at int64
if err := rows.Scan(&id, &at); err != nil {
return acks, err
}
acks.ChatSeen[id] = at
}
return acks, rows.Err()
}
// AttentionAcks returns every stored acknowledgement.
func (s *Store) AttentionAcks(ctx context.Context) (protocol.AttentionAcks, error) {
return readAttentionAcks(ctx, s.db)
}
// AckAttention validates and records one acknowledgement, prunes stale rows,
// and returns the delta that actually changed the stored set together with the
// whole set afterwards.
//
// A dismissal of a run that does not exist is skipped, and so is a watermark
// for a session that is not in the operator registry, so a client cannot grow
// the tables with invented ids. A watermark only advances: the stored value is
// the maximum of the old value, the client's value and the session's own
// registry recency, so a device that opened the chat from a stale list row
// still acknowledges everything the server listed at that moment.
func (s *Store) AckAttention(ctx context.Context, ack *protocol.AttentionAcks, now time.Time) (delta, all protocol.AttentionAcks, err error) {
valid, err := protocol.ValidateAttentionAck(ack)
if err != nil {
return delta, all, err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return delta, all, err
}
defer tx.Rollback()
nowMillis := now.UnixMilli()
delta = protocol.AttentionAcks{Dismissed: []string{}, ChatSeen: map[string]int64{}}
for _, key := range valid.Dismissed {
runID, _ := strconv.ParseInt(key[:strings.IndexByte(key, ':')], 10, 64)
result, err := tx.ExecContext(ctx, `INSERT INTO attention_dismissals(key, run_id, dismissed_at)
SELECT ?, id, ? FROM pipeline_runs WHERE id=? ON CONFLICT(key) DO NOTHING`, key, nowMillis, runID)
if err != nil {
return delta, all, err
}
if n, _ := result.RowsAffected(); n > 0 {
delta.Dismissed = append(delta.Dismissed, key)
}
}
ids := make([]string, 0, len(valid.ChatSeen))
for id := range valid.ChatSeen {
ids = append(ids, id)
}
sort.Strings(ids)
for _, id := range ids {
var recency, stored int64
var hasStored bool
err := tx.QueryRowContext(ctx, `SELECT last_used_at FROM operator_sessions WHERE session_id=?`, id).Scan(&recency)
if err == sql.ErrNoRows {
continue
}
if err != nil {
return delta, all, err
}
switch err := tx.QueryRowContext(ctx, `SELECT seen_at FROM chat_seen_watermarks WHERE session_id=?`, id).Scan(&stored); err {
case nil:
hasStored = true
case sql.ErrNoRows:
default:
return delta, all, err
}
next := max(valid.ChatSeen[id], recency, stored)
if _, err := tx.ExecContext(ctx, `INSERT INTO chat_seen_watermarks(session_id, seen_at, acked_at) VALUES(?,?,?)
ON CONFLICT(session_id) DO UPDATE SET seen_at=excluded.seen_at, acked_at=excluded.acked_at`, id, next, nowMillis); err != nil {
return delta, all, err
}
if !hasStored || next > stored {
delta.ChatSeen[id] = next
}
}
if err := pruneAttentionAcks(ctx, tx, now); err != nil {
return delta, all, err
}
all, err = readAttentionAcks(ctx, tx)
if err != nil {
return delta, all, err
}
return delta, all, tx.Commit()
}
// PruneAttentionAcks drops acknowledgements for runs and sessions that no
// longer exist and any older than AttentionAckRetention.
func (s *Store) PruneAttentionAcks(ctx context.Context, now time.Time) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := pruneAttentionAcks(ctx, tx, now); err != nil {
return err
}
return tx.Commit()
}
func pruneAttentionAcks(ctx context.Context, tx *sql.Tx, now time.Time) error {
cutoff := now.Add(-AttentionAckRetention).UnixMilli()
for _, statement := range []string{
`DELETE FROM attention_dismissals WHERE dismissed_at < ? OR run_id NOT IN (SELECT id FROM pipeline_runs)`,
`DELETE FROM chat_seen_watermarks WHERE acked_at < ? OR session_id NOT IN (SELECT session_id FROM operator_sessions)`,
} {
if _, err := tx.ExecContext(ctx, statement, cutoff); err != nil {
return err
}
}
return nil
}