Merge branch 'ws-delivery'
This commit is contained in:
@@ -29,6 +29,7 @@ func chatSamples() []struct {
|
||||
{"query", ChatQuery{SessionID: "ses_1", Limit: 50}},
|
||||
{"prompt", ChatPromptRequest{SessionID: "ses_1", Text: "hello", Model: "m", Agent: "build", Files: []string{"file:///tmp/a.png"}, Client: "Pixel 9 Pro XL"}},
|
||||
{"prompt_queued_ack", Message{Type: ChatPrompt, ID: "a1", ChatQueued: true}},
|
||||
{"prompt_steered_ack", Message{Type: ChatPrompt, ID: "a2", ChatSteered: true}},
|
||||
{"subscribe", ChatQuery{SessionID: "ses_1"}},
|
||||
{"event_user", ChatEventFrame{
|
||||
SessionID: "ses_1", MessageID: "usr_1", Kind: ChatEventUser, Client: "Pixel 10 Pro Fold",
|
||||
|
||||
@@ -246,6 +246,9 @@ type Message struct {
|
||||
// accepted into the session's queue instead of being sent immediately.
|
||||
ChatSubscribe *ChatQuery `json:"chat_subscribe,omitempty"`
|
||||
ChatQueued bool `json:"chat_queued,omitempty"`
|
||||
// ChatSteered means the prompt was handed to the turn already running; its
|
||||
// reply is that turn's continuing output.
|
||||
ChatSteered bool `json:"chat_steered,omitempty"`
|
||||
// ChatQueue is the session's server-held prompt queue: pushed as a
|
||||
// chat_queue frame when it changes and carried by the chat_subscribe and
|
||||
// chat_unqueue replies.
|
||||
|
||||
@@ -59,7 +59,7 @@ type chatHub struct {
|
||||
liveStore chatLiveStore
|
||||
// recentOps/recentOpOrder are the bounded record of accepted prompt
|
||||
// operation ids, so a client retry is acknowledged instead of re-sent.
|
||||
recentOps map[string]struct{}
|
||||
recentOps map[string]chatRecentOp
|
||||
recentOpOrder []string
|
||||
// unattended holds the unattended-observation timings (chat_unattended.go),
|
||||
// fixed when the hub is built.
|
||||
|
||||
@@ -63,6 +63,10 @@ const (
|
||||
chatAdmitRefused
|
||||
)
|
||||
|
||||
type chatRecentOp struct {
|
||||
steered bool
|
||||
}
|
||||
|
||||
type chatStoredFile struct {
|
||||
URI string `json:"uri"`
|
||||
Name string `json:"name"`
|
||||
@@ -405,21 +409,21 @@ func (r *chatRoom) holdsOp(operationID string) bool {
|
||||
// duplicateOp reports whether operationID is a prompt this session already
|
||||
// accepted (queued reports it is still waiting in the queue), so a client retry
|
||||
// is acknowledged again instead of delivered twice.
|
||||
func (h *chatHub) duplicateOp(sessionID, operationID string) (duplicate, queued bool) {
|
||||
func (h *chatHub) duplicateOp(sessionID, operationID string) (duplicate, queued, steered bool) {
|
||||
if operationID == "" {
|
||||
return false, false
|
||||
return false, false, false
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if room := h.rooms[sessionID]; room != nil {
|
||||
for _, p := range room.queue {
|
||||
if p.operationID == operationID {
|
||||
return true, p.acked
|
||||
return true, p.acked, false
|
||||
}
|
||||
}
|
||||
}
|
||||
_, seen := h.recentOps[sessionID+"\x00"+operationID]
|
||||
return seen, false
|
||||
recent, seen := h.recentOps[sessionID+"\x00"+operationID]
|
||||
return seen, false, recent.steered
|
||||
}
|
||||
|
||||
func (h *chatHub) rememberOp(sessionID, operationID string) {
|
||||
@@ -434,12 +438,12 @@ func (h *chatHub) rememberOpLocked(sessionID, operationID string) {
|
||||
}
|
||||
key := sessionID + "\x00" + operationID
|
||||
if h.recentOps == nil {
|
||||
h.recentOps = map[string]struct{}{}
|
||||
h.recentOps = map[string]chatRecentOp{}
|
||||
}
|
||||
if _, ok := h.recentOps[key]; ok {
|
||||
return
|
||||
}
|
||||
h.recentOps[key] = struct{}{}
|
||||
h.recentOps[key] = chatRecentOp{}
|
||||
h.recentOpOrder = append(h.recentOpOrder, key)
|
||||
for len(h.recentOpOrder) > chatRecentOpsLimit {
|
||||
delete(h.recentOps, h.recentOpOrder[0])
|
||||
@@ -447,6 +451,16 @@ func (h *chatHub) rememberOpLocked(sessionID, operationID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *chatHub) markOpSteered(sessionID, operationID string) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
key := sessionID + "\x00" + operationID
|
||||
if recent, ok := h.recentOps[key]; ok {
|
||||
recent.steered = true
|
||||
h.recentOps[key] = recent
|
||||
}
|
||||
}
|
||||
|
||||
// forgetOp lets a prompt that never reached the harness be retried.
|
||||
func (h *chatHub) forgetOp(sessionID, operationID string) {
|
||||
h.mu.Lock()
|
||||
|
||||
@@ -117,7 +117,7 @@ func TestChatPromptDuringTurnIsSteeredImmediately(t *testing.T) {
|
||||
t.Fatalf("first prompt queued: %+v", ack)
|
||||
}
|
||||
waitFor(t, "first prompt", func() bool { return broker.promptCount() == 1 })
|
||||
if ack := sendPrompt(t, server.ctx, conn, "p2", "two", "op-2"); ack.ChatQueued {
|
||||
if ack := sendPrompt(t, server.ctx, conn, "p2", "two", "op-2"); ack.ChatQueued || !ack.ChatSteered {
|
||||
t.Fatalf("mid-turn prompt was queued instead of forwarded: %+v", ack)
|
||||
}
|
||||
broker.steerMu.Lock()
|
||||
@@ -130,7 +130,7 @@ func TestChatPromptDuringTurnIsSteeredImmediately(t *testing.T) {
|
||||
t.Fatalf("steered prompt was stored: %v", ops)
|
||||
}
|
||||
// A retry of the same operation is acknowledged, never forwarded again.
|
||||
if ack := sendPrompt(t, server.ctx, conn, "p2-retry", "two", "op-2"); ack.ChatQueued {
|
||||
if ack := sendPrompt(t, server.ctx, conn, "p2-retry", "two", "op-2"); ack.ChatQueued || !ack.ChatSteered {
|
||||
t.Fatalf("retry ack = %+v", ack)
|
||||
}
|
||||
broker.steerMu.Lock()
|
||||
|
||||
@@ -64,11 +64,11 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
|
||||
// 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 {
|
||||
} else if duplicate, queued, steered := 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})
|
||||
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: queued, ChatSteered: steered})
|
||||
return
|
||||
}
|
||||
attachmentFiles, attachmentParts, err := prepareChatAttachments(prompt.SessionID, prompt.Attachments)
|
||||
@@ -130,9 +130,10 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
|
||||
// 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})
|
||||
hub.markOpSteered(sessionID, operationID)
|
||||
// The harness took the message into its running turn: it is not queued,
|
||||
// and the turn already in flight carries its reply.
|
||||
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatSteered: true})
|
||||
}
|
||||
if queued {
|
||||
// The client that sent it is told the prompt is waiting for the in-flight
|
||||
|
||||
Reference in New Issue
Block a user