Schedule chat prompts: send_at holds a queued prompt until its time

A chat_prompt with a future send_at (epoch ms) is always queued, durable in
chat_prompt_queue (new send_at column, added to older databases on Open),
listed with send_at in chat_queue frames, and delivered by the room's runner
once the time passes, waking on a timer. The queue stays FIFO: prompts queued
after a scheduled one wait behind it. A past or zero send_at behaves as before;
chat_unqueue cancels as for any queued prompt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 00:51:06 +00:00
parent f72c46d599
commit 1ea1e5b73c
7 changed files with 258 additions and 12 deletions
+15 -3
View File
@@ -21,6 +21,11 @@ import (
// forwarded at once and the harness reads it at its next step. Otherwise it is
// written to the durable chat_prompt_queue and delivered in order when the turn
// ends, whether or not any client is still attached, and after a restart.
//
// A prompt with a future send_at is scheduled: it is always queued (never run
// or steered at once) and the runner delivers it once that time has passed and
// the session is free, waking on a timer rather than polling. The queue stays
// strictly FIFO, so a prompt queued after a scheduled one waits behind it.
// chatQueueBusyPoll is how often a room holding queued prompts re-checks a
// harness that reports its own turn in flight. TurnRunning performs no I/O.
@@ -81,8 +86,10 @@ func (h *chatHub) admitPrompt(ctx context.Context, sessionID string, broker chat
if room.broker == nil {
room.broker = broker
}
busy := room.running || len(room.queue) > 0 || room.observedTurn
queuedAhead := len(room.queue) > 0
// A prompt scheduled for later is always queued: never run or steered now.
scheduled := p.sendAt.After(h.clock())
busy := scheduled || room.running || len(room.queue) > 0 || room.observedTurn
queuedAhead := scheduled || len(room.queue) > 0
h.mu.Unlock()
if !busy {
busy = brokerTurnRunning(broker, sessionID)
@@ -166,6 +173,7 @@ func chatQueueRow(sessionID string, p *chatQueuedPrompt) pipeline.ChatQueuedProm
FilesJSON: string(filesJSON),
PartsJSON: string(partsJSON),
CreatedAt: p.queuedAt,
SendAt: p.sendAt,
}
}
@@ -182,6 +190,7 @@ func promptFromRow(row pipeline.ChatQueuedPrompt) (*chatQueuedPrompt, error) {
acked: true,
durable: true,
queuedAt: row.CreatedAt,
sendAt: row.SendAt,
}
_ = json.Unmarshal([]byte(row.PartsJSON), &p.parts)
var stored []chatStoredFile
@@ -233,7 +242,7 @@ func (h *chatHub) queueFrame(sessionID string) protocol.ChatQueueFrame {
return frame
}
for _, row := range rows {
p := &chatQueuedPrompt{text: row.Text, operationID: row.OperationID, messageID: row.MessageID, nativeID: row.NativeID, client: row.Client, queuedAt: row.CreatedAt}
p := &chatQueuedPrompt{text: row.Text, operationID: row.OperationID, messageID: row.MessageID, nativeID: row.NativeID, client: row.Client, queuedAt: row.CreatedAt, sendAt: row.SendAt}
_ = json.Unmarshal([]byte(row.PartsJSON), &p.parts)
frame.Prompts = append(frame.Prompts, p.wire())
}
@@ -252,6 +261,9 @@ func (p *chatQueuedPrompt) wire() protocol.ChatQueuedPrompt {
if !p.queuedAt.IsZero() {
out.QueuedAt = p.queuedAt.UnixMilli()
}
if !p.sendAt.IsZero() {
out.SendAt = p.sendAt.UnixMilli()
}
return out
}