Chat reliability: durable ntfy outbox, queue recovery retries, unattended cap

- Chat phone pushes (attention items, auto-switch notices) go through a
  durable chat_notifications outbox with leases, bounded attempts and
  backoff, retried by a worker; pushes cleared in the app first are never
  published, and a publication racing a clear is cleared again.
- Queued/scheduled chat prompts are deleted on recovery only when they can
  never be rebuilt; transient failures keep the rows and retry with backoff.
- Unattended chats with live work always keep notifying; the idle cap rises
  from 16 to 64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:26:46 +00:00
parent accb9970b6
commit 9f13c93be6
13 changed files with 609 additions and 58 deletions
@@ -0,0 +1,74 @@
package server
import (
"context"
"encoding/json"
"net/url"
"path/filepath"
"slices"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
)
func fastQueueRecovery(t *testing.T) {
t.Helper()
settle, retry, retryMax := chatQueueRecoverySettle, chatQueueRecoveryRetry, chatQueueRecoveryRetryMax
chatQueueRecoverySettle, chatQueueRecoveryRetry, chatQueueRecoveryRetryMax = 10*time.Millisecond, 50*time.Millisecond, 100*time.Millisecond
t.Cleanup(func() {
chatQueueRecoverySettle, chatQueueRecoveryRetry, chatQueueRecoveryRetryMax = settle, retry, retryMax
})
}
func queuedPromptWithFile(t *testing.T, store *pipeline.Store, op, text, path string) {
t.Helper()
files, err := json.Marshal([]chatStoredFile{{URI: (&url.URL{Scheme: "file", Path: path}).String(), Name: filepath.Base(path)}})
if err != nil {
t.Fatal(err)
}
if _, _, err := store.EnqueueChatPrompt(context.Background(), pipeline.ChatQueuedPrompt{SessionID: "ses_1", OperationID: op, MessageID: "msg_" + op, Text: text, FilesJSON: string(files)}, 0); err != nil {
t.Fatal(err)
}
}
// A queued prompt whose attachment directory cannot be resolved at restart is
// kept and restored on a later retry, not deleted.
func TestChatQueuedPromptKeptOnTransientAttachmentFailure(t *testing.T) {
fastQueueRecovery(t)
dir := t.TempDir()
path := filepath.Join(t.TempDir(), "queue.db")
store := openQueueStore(t, path)
queuedPromptWithFile(t, store, "op-1", "with file", filepath.Join(dir, "a.txt"))
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", "relative/dir") // unresolvable for now
broker := newChannelChat()
startQueueServer(t, broker, store)
time.Sleep(200 * time.Millisecond)
if ops := queuedOps(t, store); !slices.Equal(ops, []string{"op-1"}) {
t.Fatalf("queued prompt dropped on a transient failure: %v", ops)
}
if texts := broker.promptTexts(); len(texts) != 0 {
t.Fatalf("prompt delivered while unrestorable: %v", texts)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
waitFor(t, "prompt restored on retry", func() bool { return slices.Equal(broker.promptTexts(), []string{"with file"}) })
waitFor(t, "delivered row deleted", func() bool { return len(queuedOps(t, store)) == 0 })
}
// A queued prompt that can never be rebuilt is still dropped, and the rest of
// the session's queue is delivered.
func TestChatQueuedPromptDroppedOnPermanentFailure(t *testing.T) {
fastQueueRecovery(t)
dir := t.TempDir()
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
path := filepath.Join(t.TempDir(), "queue.db")
store := openQueueStore(t, path)
queuedPromptWithFile(t, store, "op-1", "outside", filepath.Join(t.TempDir(), "elsewhere.txt"))
if _, _, err := store.EnqueueChatPrompt(context.Background(), pipeline.ChatQueuedPrompt{SessionID: "ses_1", OperationID: "op-2", MessageID: "msg_op-2", Text: "plain"}, 0); err != nil {
t.Fatal(err)
}
broker := newChannelChat()
startQueueServer(t, broker, store)
waitFor(t, "valid prompt delivered", func() bool { return slices.Equal(broker.promptTexts(), []string{"plain"}) })
waitFor(t, "rows cleared", func() bool { return len(queuedOps(t, store)) == 0 })
}