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:
@@ -221,9 +221,24 @@ func chatQueueRow(sessionID string, p *chatQueuedPrompt) pipeline.ChatQueuedProm
|
||||
}
|
||||
}
|
||||
|
||||
// errChatQueuedPromptInvalid marks a stored prompt that can never be rebuilt
|
||||
// (unreadable attachment list, an attachment outside the store). Only such a
|
||||
// row is dropped; any other promptFromRow failure (the attachment directory
|
||||
// cannot be resolved right now) keeps the row for a later retry.
|
||||
var errChatQueuedPromptInvalid = errors.New("queued prompt is invalid")
|
||||
|
||||
// chatQueueRecoveryRetry is the first delay before a session whose queued
|
||||
// prompts could not be restored is tried again; it doubles up to
|
||||
// chatQueueRecoveryRetryMax.
|
||||
var (
|
||||
chatQueueRecoveryRetry = 30 * time.Second
|
||||
chatQueueRecoveryRetryMax = 10 * time.Minute
|
||||
)
|
||||
|
||||
// promptFromRow rebuilds a queued prompt read back from the store. Its files
|
||||
// must still name the server's own attachment directory; they are leased again
|
||||
// so retention cannot collect them before delivery.
|
||||
// so retention cannot collect them before delivery. A permanent failure wraps
|
||||
// errChatQueuedPromptInvalid.
|
||||
func promptFromRow(row pipeline.ChatQueuedPrompt) (*chatQueuedPrompt, error) {
|
||||
p := &chatQueuedPrompt{
|
||||
text: row.Text,
|
||||
@@ -239,7 +254,7 @@ func promptFromRow(row pipeline.ChatQueuedPrompt) (*chatQueuedPrompt, error) {
|
||||
_ = json.Unmarshal([]byte(row.PartsJSON), &p.parts)
|
||||
var stored []chatStoredFile
|
||||
if err := json.Unmarshal([]byte(row.FilesJSON), &stored); err != nil {
|
||||
return p, fmt.Errorf("queued prompt attachments are unreadable")
|
||||
return p, fmt.Errorf("%w: queued prompt attachments are unreadable", errChatQueuedPromptInvalid)
|
||||
}
|
||||
if len(stored) == 0 {
|
||||
return p, nil
|
||||
@@ -253,7 +268,7 @@ func promptFromRow(row pipeline.ChatQueuedPrompt) (*chatQueuedPrompt, error) {
|
||||
for _, file := range stored {
|
||||
u, err := url.Parse(file.URI)
|
||||
if err != nil || u.Scheme != "file" || filepath.Dir(u.Path) != dir {
|
||||
return p, fmt.Errorf("queued prompt attachment %q is outside the attachment store", file.Name)
|
||||
return p, fmt.Errorf("%w: queued prompt attachment %q is outside the attachment store", errChatQueuedPromptInvalid, file.Name)
|
||||
}
|
||||
chatAttachmentStore.leased[u.Path] = true
|
||||
p.files = append(p.files, opencode.NewPreparedPromptFile(file.URI, file.Name))
|
||||
@@ -411,49 +426,80 @@ func (h *chatHub) resumeQueued(ctx context.Context, broker chatBroker) {
|
||||
return
|
||||
}
|
||||
for _, sessionID := range sessions {
|
||||
rows, err := h.queueStore.ChatQueuedPrompts(ctx, sessionID)
|
||||
if err != nil {
|
||||
log.Printf("chat queue recovery session=%s: %v", sessionID, err)
|
||||
h.resumeQueuedSession(ctx, broker, sessionID, chatQueueRecoveryRetry)
|
||||
}
|
||||
}
|
||||
|
||||
// resumeQueuedSession restores one session's durable queue. A prompt that can
|
||||
// never be rebuilt is dropped; a transient failure (the store or the
|
||||
// attachment directory unavailable) keeps every row, restores nothing for the
|
||||
// session (so FIFO order is kept) and tries again after retry, backing off.
|
||||
// A row whose operation the live hub already knows (a retry racing a client
|
||||
// that queued or delivered it meanwhile) is left to the hub.
|
||||
func (h *chatHub) resumeQueuedSession(ctx context.Context, broker chatBroker, sessionID string, retry time.Duration) {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
later := func(err error) {
|
||||
log.Printf("chat queue recovery session=%s: %v; retrying in %s", sessionID, err, retry)
|
||||
next := min(2*retry, chatQueueRecoveryRetryMax)
|
||||
time.AfterFunc(retry, func() { h.resumeQueuedSession(ctx, broker, sessionID, next) })
|
||||
}
|
||||
rows, err := h.queueStore.ChatQueuedPrompts(ctx, sessionID)
|
||||
if err != nil {
|
||||
later(err)
|
||||
return
|
||||
}
|
||||
var prompts []*chatQueuedPrompt
|
||||
for _, row := range rows {
|
||||
h.mu.Lock()
|
||||
_, known := h.recentOps[sessionID+"\x00"+row.OperationID]
|
||||
h.mu.Unlock()
|
||||
if known && row.OperationID != "" {
|
||||
continue
|
||||
}
|
||||
var prompts []*chatQueuedPrompt
|
||||
for _, row := range rows {
|
||||
p, err := promptFromRow(row)
|
||||
if err != nil {
|
||||
releaseChatAttachments(p.files)
|
||||
log.Printf("chat queue recovery session=%s: %v", sessionID, err)
|
||||
_, _ = h.queueStore.DeleteChatQueuedPrompt(ctx, sessionID, row.OperationID)
|
||||
continue
|
||||
}
|
||||
p, err := promptFromRow(row)
|
||||
if err == nil {
|
||||
prompts = append(prompts, p)
|
||||
}
|
||||
if len(prompts) == 0 {
|
||||
continue
|
||||
}
|
||||
releaseChatAttachments(p.files)
|
||||
if !errors.Is(err, errChatQueuedPromptInvalid) {
|
||||
for _, kept := range prompts {
|
||||
releaseChatAttachments(kept.files)
|
||||
}
|
||||
later(err)
|
||||
return
|
||||
}
|
||||
log.Printf("chat queue recovery session=%s: dropping %s: %v", sessionID, row.OperationID, err)
|
||||
_, _ = h.queueStore.DeleteChatQueuedPrompt(ctx, sessionID, row.OperationID)
|
||||
}
|
||||
if len(prompts) == 0 {
|
||||
return
|
||||
}
|
||||
h.mu.Lock()
|
||||
room := h.roomLocked(sessionID)
|
||||
if room.broker == nil {
|
||||
room.broker = broker
|
||||
}
|
||||
room.notBefore = time.Now().Add(chatQueueRecoverySettle)
|
||||
for _, p := range prompts {
|
||||
if !room.holdsOp(p.operationID) {
|
||||
room.queue = append(room.queue, p)
|
||||
h.rememberOpLocked(sessionID, p.operationID)
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
go func(room *chatRoom) {
|
||||
// Attach the stream first so a turn the harness is still running is
|
||||
// observed before the settle window lets the runner deliver.
|
||||
if err := h.ensureObserving(room, broker); err != nil && room.ctx.Err() == nil {
|
||||
log.Printf("chat queue recovery observe session=%s: %v", room.sessionID, err)
|
||||
}
|
||||
h.mu.Lock()
|
||||
room := h.roomLocked(sessionID)
|
||||
if room.broker == nil {
|
||||
room.broker = broker
|
||||
}
|
||||
room.notBefore = time.Now().Add(chatQueueRecoverySettle)
|
||||
for _, p := range prompts {
|
||||
if !room.holdsOp(p.operationID) {
|
||||
room.queue = append(room.queue, p)
|
||||
h.rememberOpLocked(sessionID, p.operationID)
|
||||
}
|
||||
}
|
||||
signalWake(room.wake)
|
||||
h.mu.Unlock()
|
||||
go func(room *chatRoom) {
|
||||
// Attach the stream first so a turn the harness is still running is
|
||||
// observed before the settle window lets the runner deliver.
|
||||
if err := h.ensureObserving(room, broker); err != nil && room.ctx.Err() == nil {
|
||||
log.Printf("chat queue recovery observe session=%s: %v", room.sessionID, err)
|
||||
}
|
||||
h.mu.Lock()
|
||||
signalWake(room.wake)
|
||||
h.mu.Unlock()
|
||||
}(room)
|
||||
}
|
||||
}(room)
|
||||
}
|
||||
|
||||
func (r *chatRoom) holdsOp(operationID string) bool {
|
||||
|
||||
Reference in New Issue
Block a user