Files

126 lines
4.5 KiB
Go

package server
import (
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Chat list activity: every listed chat shows whether its turn is running and
// whether background tasks (shells, background commands, subagents) are still
// running after the turn. Room frames reach only the connections that opened
// that chat, so the hub keeps a small per-session summary and pushes each
// change to every connection as chat_activity, and chat_sessions carries the
// current summary on each row.
//
// The summary is derived from the hub's own state only (no broker call, no
// I/O), under the hub mutex, at the points where that state changes: a turn
// admitted or finished, a step announcing or ending a turn, a shell starting
// or exiting, a shell readback reconciling the set, and a room released.
// Anything else, including every text or tool chunk, never touches it.
//
// A session's background tasks are counted only while it has a room, i.e.
// while the hub observes its stream (a viewer, or unattended observation, see
// chat_unattended.go). Once the room is released its shell exits are no
// longer seen, so the count drops to zero rather than going stale.
// chatActivityLocked computes the session's current summary. Callers hold h.mu.
func (h *chatHub) chatActivityLocked(sessionID string) protocol.ChatActivity {
activity := protocol.ChatActivity{SessionID: sessionID}
room := h.rooms[sessionID]
if room == nil {
return activity
}
activity.TurnRunning = room.running || room.observedTurn
activity.BackgroundTasks = h.backgroundTasksLocked(sessionID)
return activity
}
// backgroundTasksLocked counts the session's retained running shells. Callers
// hold h.mu.
func (h *chatHub) backgroundTasksLocked(sessionID string) int {
if turn := h.turns[sessionID]; turn != nil {
return len(turn.shells)
}
return 0
}
// noteActivityLocked recomputes the session's summary and, only when the turn
// state or the background-task count changed, records it under a new sequence
// number and pushes it to every connection. Callers hold h.mu; the push itself
// never blocks (chatSessionChanges writes on its own goroutines).
func (h *chatHub) noteActivityLocked(sessionID string) {
if sessionID == "" {
return
}
h.rememberLiveLocked(sessionID)
next := h.chatActivityLocked(sessionID)
previous := h.activity[sessionID]
if previous.TurnRunning == next.TurnRunning && previous.BackgroundTasks == next.BackgroundTasks {
return
}
if h.activity == nil {
h.activity = map[string]protocol.ChatActivity{}
}
next.Seq = h.nextActivitySeqLocked()
// The record keeps the last sequence of an idle session too, so a list
// row's Seq never moves backwards behind a push a client already applied.
// It holds one small entry per session that was ever active.
h.activity[sessionID] = next
h.sessionChanges.broadcastActivity(next)
}
// nextActivitySeqLocked returns a strictly increasing sequence. It starts from
// the wall clock in microseconds (exact in a JavaScript number) so snapshots
// after a server restart still order after the ones a client held before.
func (h *chatHub) nextActivitySeqLocked() int64 {
if h.activitySeq == 0 {
h.activitySeq = time.Now().UnixMicro()
}
h.activitySeq++
return h.activitySeq
}
// activitySnapshot returns the current summary of each session in ids, each
// with the sequence of its last recorded change. A session that has not
// changed since this server started carries the current sequence instead, so
// it still orders after anything a client kept from a previous server process
// and before any later push.
func (h *chatHub) activitySnapshot(ids []string) map[string]protocol.ChatActivity {
out := make(map[string]protocol.ChatActivity, len(ids))
if h == nil {
return out
}
h.mu.Lock()
defer h.mu.Unlock()
if h.activitySeq == 0 {
h.activitySeq = time.Now().UnixMicro()
}
for _, id := range ids {
activity := h.chatActivityLocked(id)
activity.Seq = h.activitySeq
if recorded, ok := h.activity[id]; ok {
activity.Seq = recorded.Seq
}
out[id] = activity
}
return out
}
// broadcastActivity pushes one session's activity to every connection,
// including the one whose action caused it (it has no reply carrying it).
func (c *chatSessionChanges) broadcastActivity(activity protocol.ChatActivity) {
c.mu.Lock()
targets := make([]func(protocol.Message) error, 0, len(c.clients))
for _, write := range c.clients {
targets = append(targets, write)
}
c.mu.Unlock()
for _, write := range targets {
body := activity
go func() {
_ = write(protocol.Message{Type: protocol.ChatActivityChanged, ChatActivity: &body})
}()
}
}