Send mid-turn chat prompts to the harness and queue the rest durably on the server
Claude Code (stream-json stdin) and Codex (turn/steer pinned to the active turn) receive a prompt sent while a turn runs immediately. Other harnesses (OpenCode) get a durable chat_prompt_queue row owned by the session: it survives the last client detaching and a server restart, is delivered in order when the turn ends with or without a client, and is deduplicated by operation id. chat_subscribe carries the queue, chat_queue frames report delivered/removed/failed changes, and chat_unqueue removes a waiting prompt.
This commit is contained in:
@@ -59,6 +59,18 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
|
||||
return
|
||||
}
|
||||
}
|
||||
operationID := prompt.OperationID
|
||||
if operationID == "" {
|
||||
// Every server-held prompt is addressable (chat_unqueue) and unique in
|
||||
// its session's durable queue, so mint an id for a client that sent none.
|
||||
operationID = "srv-" + strings.TrimPrefix(chatClientToken(), "conn-")
|
||||
} else if duplicate, queued := hub.duplicateOp(prompt.SessionID, operationID); duplicate {
|
||||
// A retry of a prompt this session already accepted: acknowledge it the
|
||||
// same way again instead of delivering it a second time.
|
||||
hub.attach(prompt.SessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
|
||||
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: queued})
|
||||
return
|
||||
}
|
||||
attachmentFiles, attachmentParts, err := prepareChatAttachments(prompt.SessionID, prompt.Attachments)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
@@ -85,29 +97,43 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
|
||||
Attachments: attachmentParts,
|
||||
Client: sub.label,
|
||||
OperationID: prompt.OperationID,
|
||||
OperationID: operationID,
|
||||
}
|
||||
if userFrame.MessageID == "" {
|
||||
userFrame.MessageID = hub.nextUserMessageID()
|
||||
}
|
||||
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
|
||||
queuedPrompt := &chatQueuedPrompt{
|
||||
text: prompt.Text,
|
||||
files: attachmentFiles,
|
||||
operationID: prompt.OperationID,
|
||||
operationID: operationID,
|
||||
// Only the caller's real OpenCode message id is forwarded. The hub's own
|
||||
// `usr_` display id is not a valid OpenCode `msg_` id, so a client that
|
||||
// omitted native_message_id lets OpenCode generate one instead.
|
||||
nativeID: prompt.NativeMessageID,
|
||||
messageID: userFrame.MessageID,
|
||||
client: sub.label,
|
||||
parts: attachmentParts,
|
||||
requestID: request.ID,
|
||||
write: write,
|
||||
})
|
||||
if refused {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
|
||||
}
|
||||
// Claim the operation before admission so a concurrent retry of the same
|
||||
// prompt is acknowledged rather than delivered twice.
|
||||
hub.rememberOp(sessionID, operationID)
|
||||
admission, admitErr := hub.admitPrompt(ctx, sessionID, broker, queuedPrompt)
|
||||
if admission == chatAdmitRefused {
|
||||
hub.forgetOp(sessionID, operationID)
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: admitErr.Error()})
|
||||
return
|
||||
}
|
||||
accepted = true
|
||||
queued := admission == chatAdmitQueued
|
||||
// Publish the user turn to every other client; the sender already has it.
|
||||
hub.broadcastUser(userFrame, clientID)
|
||||
if admission == chatAdmitSteered {
|
||||
// The harness took the message into its running turn: it is sent, not
|
||||
// queued, and the turn already in flight carries its reply.
|
||||
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID})
|
||||
}
|
||||
if queued {
|
||||
// The client that sent it is told the prompt is waiting for the in-flight
|
||||
// turn, so it shows queued/steering instead of blocking or silently
|
||||
@@ -200,7 +226,10 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
|
||||
}
|
||||
// Acknowledge before the stream is allowed to fan out: otherwise an immediately
|
||||
// available retained shell can overtake the subscribe reply on this connection.
|
||||
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID})
|
||||
// The reply carries the prompts the server still holds for the session, so
|
||||
// a reloaded client draws them as Queued without any device storage.
|
||||
queue := hub.queueFrame(query.SessionID)
|
||||
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue})
|
||||
if err := hub.ensureObserving(sub.room, broker); err != nil {
|
||||
sub.close()
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not observe session " + query.SessionID})
|
||||
|
||||
Reference in New Issue
Block a user