Fan a chat session out to every client and share approvals exactly-once
A session was owned by one connection: the claim map in chat_claims.go let the
first client hold it, every other client saw the session Busy and read-only, and
a second client's prompt was rejected. Replace the claim with a per-session
"room" so the operator's phone, tablet and terminal all watch and write the same
conversation, and a pending approval is a shared, exactly-once object.
Server (internal/server, internal/protocol):
- New chat_room.go: one room per session holding the session's single broker
stream, its one bounded prompt queue (8) and the fan-out to every attached
client. A chat_subscribe frame attaches a connection to a session's shared
stream and replays the still-unresolved approval requests the room holds.
- chat_stream.go: handleChatPrompt accepts a prompt from any client. It attaches
the sender, either starts a turn or queues the prompt behind the in-flight turn
and acknowledges it with chat_queued:true ("queued" is the chosen behaviour,
stated on the wire and in docs/cli.md), and echoes the user turn to every other
client attributed to the sender's label. The prompt handler no longer owns the
stream; one turn runner per room drains the queue, so two turns never
interleave. Per-connection write serialisation is unchanged (each chatSub has
its own writer goroutine and bounded buffer).
- chat_permissions.go / chat_commands.go: an approval is reserved by the first
answer and confirmed once delivered; the first answer wins, is recorded with
the answering client's label, and the resolution is pushed to every watching
client as chat_permission with resolved/decision/resolved_by so their cards
clear. A later answer is refused with an error naming the client that answered,
and never reaches the agent twice; a reservation whose reply failed is
released.
- chat_hub.go / chat_sessions.go / chat.go: the hub now owns rooms and the
user-turn id sequence; the resulting user turns are retained (role and client
carried) so a reconnect re-syncs who said what. chat_sessions no longer marks a
session Busy and its clientID parameter is gone; the protocol keeps the Busy
field for wire compatibility only. websocket.go detaches a connection's chat
subscriptions when the read loop ends.
- chat_claims.go is removed, not left vestigial: the only exclusive thing left is
a turn, and the room's queue is that.
App (android):
- PipelinesRepository carries a stable per-device client label (Build.MODEL, with
a fallback) on prompts and replies, and adds chatSubscribe; AppViewModel
subscribes on opening a chat (best-effort, an older server's error frame does
not stop history loading) and records the queued ack.
- ChatStore folds a user frame from another client into the transcript once,
attributed by the sender's label, and never receives its own prompt back, so
the sender's own message is never duplicated. ChatThread.readOnly is gone:
canSend no longer depends on another client, the composer says "Queued - it
will send when the current turn finishes." instead of disabling input, and the
status label is Running/Idle only. Scroll/follow behaviour is untouched.
- Tests: ChatProtocolTest round-trips the new wire fields, ChatFormatTest asserts
the stale Busy flag cannot produce a read-only label, MultiChatStateTest asserts
a session another client watches stays writable.
Reflected where it belongs: no packaged template changes (no bundled starter
embeds the chat protocol). Checked the packaged authoring skill references: none
of them describes the chat wire beyond operations.md, and
internal/author/skill/references/operations.md is now stale - its "Brokered
OpenCode chat" section still says chat_sessions marks "busy" when another client
holds the session and that a second client's chat_prompt is rejected, and it does
not mention chat_subscribe, the per-session prompt queue/chat_queued, the
per-client label or the shared exactly-once approval (it also still describes the
per-subscription permission poller, which the room's single poller replaced). The
supported semantics are the ones in docs/cli.md, updated in this commit; the
operations.md section needs a rewrite to match. No README or other shared
document was edited here.
Checks run on the committed tree: gofmt -l internal cmd examples (nothing), go
build ./..., go vet ./internal/... ./cmd/..., go test ./internal/server
./internal/protocol ./internal/stage ./internal/pipeline.
This commit is contained in:
+73
-15
@@ -1,7 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -22,15 +24,18 @@ const chatMaxTurns = 128
|
||||
// chatHub retains the accumulated turn state per session, independently of any
|
||||
// single connection, so a reconnecting client can re-sync what it missed. State
|
||||
// is keyed by session and then by message id (a session holds many turns), and
|
||||
// is capped at chatRetainedLimit.
|
||||
// is capped at chatRetainedLimit. It also owns the per-session live rooms: the
|
||||
// one broker stream, the prompt queue and the fan-out to every attached client.
|
||||
type chatHub struct {
|
||||
mu sync.Mutex
|
||||
turns map[string]*chatTurn
|
||||
now func() time.Time
|
||||
// claims maps a session to the client currently writing it so a second
|
||||
// client is rejected; claimOrder bounds the map like chatMaxTurns.
|
||||
claims map[string]string
|
||||
claimOrder []string
|
||||
mu sync.Mutex
|
||||
turns map[string]*chatTurn
|
||||
rooms map[string]*chatRoom
|
||||
baseCtx context.Context
|
||||
registry operatorRegistry
|
||||
now func() time.Time
|
||||
// userSeq is a monotonic source of ids for the user turns the server echoes
|
||||
// to the other clients, so each retained user message has a stable id.
|
||||
userSeq int
|
||||
}
|
||||
|
||||
// chatPart is the retained form of one part. Text and reasoning deltas append to
|
||||
@@ -40,12 +45,16 @@ type chatPart struct {
|
||||
text []byte
|
||||
}
|
||||
|
||||
// chatMessage is one retained assistant turn. Its parts keep the exact ids the
|
||||
// supporting chat_event frames carried, so a resume preserves attribution.
|
||||
// chatMessage is one retained turn. Its parts keep the exact ids the supporting
|
||||
// chat_event frames carried, so a resume preserves attribution. Role and client
|
||||
// are carried for a user turn the server echoed (a prompt another client sent),
|
||||
// so a resumed transcript still says who sent it.
|
||||
type chatMessage struct {
|
||||
id string
|
||||
order []string
|
||||
parts map[string]*chatPart
|
||||
id string
|
||||
role string
|
||||
client string
|
||||
order []string
|
||||
parts map[string]*chatPart
|
||||
}
|
||||
|
||||
type chatTurn struct {
|
||||
@@ -62,10 +71,33 @@ type chatTurn struct {
|
||||
// bounds it to chatPermissionRetainedLimit.
|
||||
permissions map[string]struct{}
|
||||
permissionOrder []string
|
||||
// resolutions records the shared exactly-once answers to permission requests,
|
||||
// keyed by request id, so a second answer is refused and the recorded outcome
|
||||
// survives the clients that gave it; resolutionOrder bounds it to
|
||||
// chatPermissionRetainedLimit like the dedup set above.
|
||||
resolutions map[string]*chatResolution
|
||||
resolutionOrder []string
|
||||
}
|
||||
|
||||
// chatResolution is the shared answer to one permission request. answered is
|
||||
// false while the first answer is still being delivered to the agent, so a
|
||||
// concurrent second answer is refused without ever reaching the agent twice.
|
||||
type chatResolution struct {
|
||||
client string
|
||||
decision string
|
||||
answered bool
|
||||
}
|
||||
|
||||
func newChatHub() *chatHub {
|
||||
return &chatHub{turns: map[string]*chatTurn{}, now: time.Now}
|
||||
return &chatHub{turns: map[string]*chatTurn{}, rooms: map[string]*chatRoom{}, baseCtx: context.Background(), now: time.Now}
|
||||
}
|
||||
|
||||
// nextUserMessageID mints a stable id for one echoed user turn.
|
||||
func (h *chatHub) nextUserMessageID() string {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.userSeq++
|
||||
return "usr_" + strconv.Itoa(h.userSeq)
|
||||
}
|
||||
|
||||
func (h *chatHub) clock() time.Time {
|
||||
@@ -100,11 +132,33 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) {
|
||||
}
|
||||
message := turn.messages[frame.MessageID]
|
||||
if message == nil {
|
||||
message = &chatMessage{id: frame.MessageID, parts: map[string]*chatPart{}}
|
||||
role := protocol.ChatRoleAssistant
|
||||
client := ""
|
||||
if frame.Kind == protocol.ChatEventUser {
|
||||
role = protocol.ChatRoleUser
|
||||
client = frame.Client
|
||||
}
|
||||
message = &chatMessage{id: frame.MessageID, role: role, client: client, parts: map[string]*chatPart{}}
|
||||
turn.order = append(turn.order, frame.MessageID)
|
||||
turn.messages[frame.MessageID] = message
|
||||
} else if message.client == "" && frame.Kind == protocol.ChatEventUser {
|
||||
message.client = frame.Client
|
||||
}
|
||||
switch frame.Kind {
|
||||
case protocol.ChatEventUser:
|
||||
if frame.Part == nil || frame.Part.ID == "" || frame.Part.Text == "" || turn.overflow {
|
||||
return
|
||||
}
|
||||
if !turn.retain(len(frame.Part.Text)) {
|
||||
return
|
||||
}
|
||||
part, ok := message.parts[frame.Part.ID]
|
||||
if !ok {
|
||||
part = &chatPart{part: protocol.ChatPart{Type: protocol.ChatPartText, ID: frame.Part.ID}}
|
||||
message.order = append(message.order, frame.Part.ID)
|
||||
message.parts[frame.Part.ID] = part
|
||||
}
|
||||
part.text = append(part.text, frame.Part.Text...)
|
||||
case protocol.ChatEventText, protocol.ChatEventReasoning:
|
||||
id := ""
|
||||
if frame.Part != nil {
|
||||
@@ -255,6 +309,10 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
|
||||
continue
|
||||
}
|
||||
out := protocol.ChatMessage{ID: id, SessionID: sessionID, Role: protocol.ChatRoleAssistant}
|
||||
if message.role != "" {
|
||||
out.Role = message.role
|
||||
}
|
||||
out.Client = message.client
|
||||
for _, partID := range message.order {
|
||||
if part, ok := message.parts[partID]; ok {
|
||||
piece := part.part
|
||||
|
||||
Reference in New Issue
Block a user