UPDATE app_settings s
SET value_json = to_jsonb(wc.enabled),
    updated_at = NOW()
FROM worker_controls wc
WHERE (wc.name, s.key) IN (
  ('ai-qualifier', 'ai_qualifier_enabled'),
  ('ai-writer', 'ai_writer_enabled')
);

UPDATE raw_posts
SET rewrite_status='failed',
    rewrite_notes=COALESCE(rewrite_notes, 'recovered stale writer processing state'),
    updated_at=NOW()
WHERE rewrite_status='processing'
  AND updated_at < NOW() - INTERVAL '30 minutes';
