package server import "git.a-wan.net/alan/pipelines/internal/protocol" // chatPermissionRetainedLimit bounds the dedup set of already-pushed permission // request ids one session retains, so a long turn cannot grow it without limit. const chatPermissionRetainedLimit = 256 // seenPermission records requestID as pushed for the session and reports whether // it is newly seen (true) or a duplicate (false). The retained ids are bounded: // the oldest is dropped once the session exceeds chatPermissionRetainedLimit, so // the same pending request is pushed at most once while the state stays bounded. func (h *chatHub) seenPermission(sessionID, requestID string) bool { if sessionID == "" || requestID == "" { return false } h.mu.Lock() defer h.mu.Unlock() turn := h.turnLocked(sessionID) if turn.permissions == nil { turn.permissions = map[string]struct{}{} } if _, ok := turn.permissions[requestID]; ok { return false } turn.permissions[requestID] = struct{}{} turn.permissionOrder = append(turn.permissionOrder, requestID) if len(turn.permissionOrder) > chatPermissionRetainedLimit { oldest := turn.permissionOrder[0] turn.permissionOrder = turn.permissionOrder[1:] delete(turn.permissions, oldest) } return true } // reservePermission claims the shared right to answer requestID for client. It // returns true when this caller is the first to answer (the only caller allowed // to reach the agent), and false with the recorded resolution when the request is // already answered or is already being answered, so a later answer is refused // rather than delivered twice. The reservation is bounded like the dedup set. func (h *chatHub) reservePermission(sessionID, requestID, client string) (*chatResolution, bool) { if sessionID == "" || requestID == "" { return nil, false } h.mu.Lock() defer h.mu.Unlock() turn := h.turnLocked(sessionID) if turn.resolutions == nil { turn.resolutions = map[string]*chatResolution{} } if prior, ok := turn.resolutions[requestID]; ok { return prior, false } turn.resolutions[requestID] = &chatResolution{client: client} turn.resolutionOrder = append(turn.resolutionOrder, requestID) if len(turn.resolutionOrder) > chatPermissionRetainedLimit { oldest := turn.resolutionOrder[0] turn.resolutionOrder = turn.resolutionOrder[1:] delete(turn.resolutions, oldest) } return nil, true } // releasePermission drops a reservation whose answer never reached the agent, so // a later attempt can win instead of the request staying permanently refused. func (h *chatHub) releasePermission(sessionID, requestID string) { h.mu.Lock() defer h.mu.Unlock() turn := h.turns[sessionID] if turn == nil { return } delete(turn.resolutions, requestID) for i, id := range turn.resolutionOrder { if id == requestID { turn.resolutionOrder = append(turn.resolutionOrder[:i], turn.resolutionOrder[i+1:]...) break } } } // confirmPermission records the delivered answer and pushes the resolution to // every watching client, so each one's approval card clears and shows the // outcome and which client gave it. func (h *chatHub) confirmPermission(sessionID, requestID, client, decision string) { h.mu.Lock() if turn := h.turns[sessionID]; turn != nil { if resolution, ok := turn.resolutions[requestID]; ok { resolution.decision = decision resolution.answered = true } } frame := protocol.ChatPermissionFrame{SessionID: sessionID, RequestID: requestID} var targets []*chatSub if room := h.rooms[sessionID]; room != nil { for i := range room.pending { if room.pending[i].RequestID == requestID { frame = room.pending[i] room.pending = append(room.pending[:i], room.pending[i+1:]...) break } } for _, sub := range room.subs { targets = append(targets, sub) } } h.mu.Unlock() h.clearChatAttention(sessionID, chatNotificationID(sessionID, "permission", requestID)) frame.Resolved = true frame.Decision = decision frame.ResolvedBy = client msg := protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame} for _, sub := range targets { sub.send(msg) } }