Merge branch 'ws-delivery'

This commit is contained in:
2026-11-07 01:42:55 +00:00
14 changed files with 97 additions and 21 deletions
+1 -1
View File
@@ -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.
+21 -7
View File
@@ -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()
+2 -2
View File
@@ -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()
+6 -5
View File
@@ -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