bc218dfbbe
* feat: add workspace sources from files panel * fix: preserve worktree identity during recovery * feat: unify workspace source actions * fix: validate recovered workspace repositories * fix: harden workspace source filesystem boundaries * test: split workspace repository chip coverage * fix: restrict repository rescan recovery paths * fix: stabilize workspace source CI checks * test: canonicalize workspace source paths * test: align workspace source container paths * fix: populate direct workspace repository names * test: preserve git fixture rewrites across restarts * test: isolate backend git fixture config * fix: resume remote workspace executions before creation * fix: preserve ssh agent identity during resume * fix: preserve SSH workspace across backend restart * fix: simplify workspace actions trigger * fix: refine add workspace repositories UX * docs: narrow workspace source UX updates * fix: avoid phantom turn on session resume * test(web): normalize workspace source request headers * feat(web): streamline workspace source picker * docs: remove trailing spaces from workspace source plan * fix: prevent resume status from creating phantom turns * fix(web): stabilize workspace source attachment * refactor(web): extract workspace source store contract * fix: avoid phantom turns on idle agent resume * fix: address backend static analysis findings * fix: preserve agentctl readiness status value * fix: preserve resume messages without active turns * fix: resume agents from promoted workspace root * chore: merge origin/main into feature/add-new-repo-folder-d97 * feat(web): explain workspace source consequences * test(websocket): count session activity subscription
233 lines
10 KiB
Go
233 lines
10 KiB
Go
package websocket
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/kandev/kandev/internal/common/logger"
|
|
"github.com/kandev/kandev/internal/events"
|
|
"github.com/kandev/kandev/internal/events/bus"
|
|
ws "github.com/kandev/kandev/pkg/websocket"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type TaskEventBroadcaster struct {
|
|
hub *Hub
|
|
subscriptions []bus.Subscription
|
|
logger *logger.Logger
|
|
}
|
|
|
|
func RegisterTaskNotifications(ctx context.Context, eventBus bus.EventBus, hub *Hub, log *logger.Logger) *TaskEventBroadcaster {
|
|
b := &TaskEventBroadcaster{
|
|
hub: hub,
|
|
logger: log.WithFields(zap.String("component", "ws-task-broadcaster")),
|
|
}
|
|
if eventBus == nil {
|
|
return b
|
|
}
|
|
|
|
b.subscribe(eventBus, events.WorkspaceCreated, ws.ActionWorkspaceCreated)
|
|
b.subscribe(eventBus, events.WorkspaceUpdated, ws.ActionWorkspaceUpdated)
|
|
b.subscribe(eventBus, events.WorkspaceDeleted, ws.ActionWorkspaceDeleted)
|
|
b.subscribe(eventBus, events.WorkflowCreated, ws.ActionWorkflowCreated)
|
|
b.subscribe(eventBus, events.WorkflowUpdated, ws.ActionWorkflowUpdated)
|
|
b.subscribe(eventBus, events.WorkflowDeleted, ws.ActionWorkflowDeleted)
|
|
b.subscribe(eventBus, events.WorkflowStepCreated, ws.ActionWorkflowStepCreated)
|
|
b.subscribe(eventBus, events.WorkflowStepUpdated, ws.ActionWorkflowStepUpdated)
|
|
b.subscribe(eventBus, events.WorkflowStepDeleted, ws.ActionWorkflowStepDeleted)
|
|
b.subscribe(eventBus, events.AgentProfileCreated, ws.ActionAgentProfileCreated)
|
|
b.subscribe(eventBus, events.AgentProfileUpdated, ws.ActionAgentProfileUpdated)
|
|
b.subscribe(eventBus, events.AgentProfileDeleted, ws.ActionAgentProfileDeleted)
|
|
b.subscribe(eventBus, events.TaskCreated, ws.ActionTaskCreated)
|
|
b.subscribe(eventBus, events.TaskUpdated, ws.ActionTaskUpdated)
|
|
b.subscribe(eventBus, events.SessionWorkspaceSourcesUpdated, ws.ActionSessionWorkspaceSourcesUpdated)
|
|
b.subscribe(eventBus, events.TaskDeleted, ws.ActionTaskDeleted)
|
|
b.subscribe(eventBus, events.TaskStateChanged, ws.ActionTaskStateChanged)
|
|
b.subscribe(eventBus, events.TaskPlanCreated, ws.ActionTaskPlanCreated)
|
|
b.subscribe(eventBus, events.TaskPlanUpdated, ws.ActionTaskPlanUpdated)
|
|
b.subscribe(eventBus, events.TaskPlanDeleted, ws.ActionTaskPlanDeleted)
|
|
b.subscribe(eventBus, events.TaskPlanRevisionCreated, ws.ActionTaskPlanRevisionCreated)
|
|
b.subscribe(eventBus, events.TaskPlanReverted, ws.ActionTaskPlanReverted)
|
|
b.subscribe(eventBus, events.TaskWalkthroughCreated, ws.ActionTaskWalkthroughCreated)
|
|
b.subscribe(eventBus, events.TaskWalkthroughUpdated, ws.ActionTaskWalkthroughUpdated)
|
|
b.subscribe(eventBus, events.TaskWalkthroughDeleted, ws.ActionTaskWalkthroughDeleted)
|
|
b.subscribe(eventBus, events.RepositoryCreated, ws.ActionRepositoryCreated)
|
|
b.subscribe(eventBus, events.RepositoryUpdated, ws.ActionRepositoryUpdated)
|
|
b.subscribe(eventBus, events.RepositoryDeleted, ws.ActionRepositoryDeleted)
|
|
b.subscribe(eventBus, events.RepositoryScriptCreated, ws.ActionRepositoryScriptCreated)
|
|
b.subscribe(eventBus, events.RepositoryScriptUpdated, ws.ActionRepositoryScriptUpdated)
|
|
b.subscribe(eventBus, events.RepositoryScriptDeleted, ws.ActionRepositoryScriptDeleted)
|
|
b.subscribe(eventBus, events.ExecutorCreated, ws.ActionExecutorCreated)
|
|
b.subscribe(eventBus, events.ExecutorUpdated, ws.ActionExecutorUpdated)
|
|
b.subscribe(eventBus, events.ExecutorDeleted, ws.ActionExecutorDeleted)
|
|
b.subscribe(eventBus, events.ExecutorProfileCreated, ws.ActionExecutorProfileCreated)
|
|
b.subscribe(eventBus, events.ExecutorProfileUpdated, ws.ActionExecutorProfileUpdated)
|
|
b.subscribe(eventBus, events.ExecutorProfileDeleted, ws.ActionExecutorProfileDeleted)
|
|
b.subscribe(eventBus, events.ExecutorPrepareProgress, ws.ActionExecutorPrepareProgress)
|
|
b.subscribe(eventBus, events.ExecutorPrepareCompleted, ws.ActionExecutorPrepareCompleted)
|
|
b.subscribe(eventBus, events.EnvironmentCreated, ws.ActionEnvironmentCreated)
|
|
b.subscribe(eventBus, events.EnvironmentUpdated, ws.ActionEnvironmentUpdated)
|
|
b.subscribe(eventBus, events.EnvironmentDeleted, ws.ActionEnvironmentDeleted)
|
|
b.subscribe(eventBus, events.TaskSessionStateChanged, ws.ActionSessionStateChanged)
|
|
b.subscribe(eventBus, events.TaskSessionActivityChanged, ws.ActionSessionActivityChanged)
|
|
b.subscribe(eventBus, events.MessageAdded, ws.ActionSessionMessageAdded)
|
|
b.subscribe(eventBus, events.MessageUpdated, ws.ActionSessionMessageUpdated)
|
|
b.subscribe(eventBus, events.MessageDeleted, ws.ActionSessionMessageDeleted)
|
|
b.subscribe(eventBus, events.AgentctlStarting, ws.ActionSessionAgentctlStarting)
|
|
b.subscribe(eventBus, events.AgentctlReady, ws.ActionSessionAgentctlReady)
|
|
b.subscribe(eventBus, events.AgentctlError, ws.ActionSessionAgentctlError)
|
|
b.subscribe(eventBus, events.TurnStarted, ws.ActionSessionTurnStarted)
|
|
b.subscribe(eventBus, events.TurnCompleted, ws.ActionSessionTurnCompleted)
|
|
b.subscribe(eventBus, events.MessageQueueStatusChanged, ws.ActionMessageQueueStatusChanged)
|
|
b.subscribe(eventBus, events.GitHubTaskPRUpdated, ws.ActionGitHubTaskPRUpdated)
|
|
b.subscribe(eventBus, events.GitHubTaskCIOptionsUpdated, ws.ActionGitHubTaskCIOptionsUpdated)
|
|
b.subscribe(eventBus, events.GitHubRateLimitUpdated, ws.ActionGitHubRateLimitUpdated)
|
|
b.subscribe(eventBus, events.GitLabTaskMRUpdated, ws.ActionGitLabTaskMRUpdated)
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
b.Close()
|
|
}()
|
|
|
|
return b
|
|
}
|
|
|
|
func (b *TaskEventBroadcaster) Close() {
|
|
for _, sub := range b.subscriptions {
|
|
if sub != nil && sub.IsValid() {
|
|
_ = sub.Unsubscribe()
|
|
}
|
|
}
|
|
b.subscriptions = nil
|
|
}
|
|
|
|
func (b *TaskEventBroadcaster) subscribe(eventBus bus.EventBus, subject, action string) {
|
|
sub, err := eventBus.Subscribe(subject, func(ctx context.Context, event *bus.Event) error {
|
|
msg, err := ws.NewNotification(action, event.Data)
|
|
if err != nil {
|
|
b.logger.Error("failed to build websocket notification", zap.String("action", action), zap.Error(err))
|
|
return nil
|
|
}
|
|
// Try to extract session_id from event data (works for both map and struct types)
|
|
var sessionID string
|
|
if data, ok := event.Data.(map[string]interface{}); ok {
|
|
sessionID, _ = data["session_id"].(string)
|
|
} else if data, ok := event.Data.(interface{ GetSessionID() string }); ok {
|
|
sessionID = data.GetSessionID()
|
|
}
|
|
|
|
// Debug logging for session state changes with metadata
|
|
if action == ws.ActionSessionStateChanged {
|
|
if data, ok := event.Data.(map[string]interface{}); ok {
|
|
if metadata, ok := data["metadata"]; ok {
|
|
b.logger.Debug("received session.state_changed with metadata",
|
|
zap.String("action", action),
|
|
zap.String("session_id", sessionID),
|
|
zap.Any("metadata", metadata))
|
|
}
|
|
}
|
|
}
|
|
if data, ok := event.Data.(map[string]interface{}); ok {
|
|
b.logLifecycleBroadcast(action, data, sessionID)
|
|
}
|
|
|
|
workspaceID := extractWorkspaceID(event.Data)
|
|
switch action {
|
|
case ws.ActionWorkspaceCreated, ws.ActionWorkspaceUpdated, ws.ActionWorkspaceDeleted:
|
|
// Workspace event payloads are the workspace DTO itself: the
|
|
// workspace ID lives under "id", not "workspace_id". Without this
|
|
// the extract comes back empty and the event would broadcast to
|
|
// every user (caught by the auth E2E segregation spec).
|
|
if workspaceID == "" {
|
|
workspaceID = extractStringField(event.Data, "id")
|
|
}
|
|
b.hub.BroadcastToWorkspace(workspaceID, msg)
|
|
return nil
|
|
case ws.ActionSessionAgentctlStarting, ws.ActionSessionAgentctlReady, ws.ActionSessionAgentctlError:
|
|
if sessionID != "" {
|
|
b.hub.BroadcastToSession(sessionID, msg)
|
|
return nil
|
|
}
|
|
case ws.ActionSessionStateChanged:
|
|
// Broadcast beyond the session subscribers so the sidebar task
|
|
// switcher can track state changes for all tasks — but scoped to
|
|
// the owning workspace's user when auth is enabled.
|
|
b.hub.BroadcastToWorkspace(workspaceID, msg)
|
|
return nil
|
|
case ws.ActionSessionMessageAdded, ws.ActionSessionMessageUpdated, ws.ActionSessionMessageDeleted:
|
|
if sessionID != "" {
|
|
b.hub.BroadcastToSession(sessionID, msg)
|
|
return nil
|
|
}
|
|
case ws.ActionSessionWorkspaceSourcesUpdated:
|
|
if sessionID != "" {
|
|
b.hub.BroadcastToSession(sessionID, msg)
|
|
return nil
|
|
}
|
|
case ws.ActionMessageQueueStatusChanged:
|
|
if sessionID != "" {
|
|
b.hub.BroadcastToSession(sessionID, msg)
|
|
return nil
|
|
}
|
|
case ws.ActionExecutorPrepareProgress, ws.ActionExecutorPrepareCompleted:
|
|
// Broadcast to the owning workspace's clients so prepare
|
|
// progress/warnings are available when the user navigates to the
|
|
// session page after task creation.
|
|
b.hub.BroadcastToWorkspace(workspaceID, msg)
|
|
return nil
|
|
}
|
|
// Workspace-carrying events (task/workflow/repository/…) route to the
|
|
// owner's clients; events without workspace context (executors,
|
|
// environments, agent profiles — instance-wide resources) stay global.
|
|
b.hub.BroadcastToWorkspace(workspaceID, msg)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
b.logger.Error("failed to subscribe to events", zap.String("subject", subject), zap.Error(err))
|
|
return
|
|
}
|
|
b.subscriptions = append(b.subscriptions, sub)
|
|
}
|
|
|
|
// extractWorkspaceID pulls a workspace ID from event payloads (map- or
|
|
// struct-shaped). Empty means "no workspace context" — the event is treated
|
|
// as instance-wide and broadcast to everyone.
|
|
func extractWorkspaceID(data interface{}) string {
|
|
if id := extractStringField(data, "workspace_id"); id != "" {
|
|
return id
|
|
}
|
|
if provider, ok := data.(interface{ GetWorkspaceID() string }); ok {
|
|
return provider.GetWorkspaceID()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func extractStringField(data interface{}, key string) string {
|
|
if m, ok := data.(map[string]interface{}); ok {
|
|
if value, ok := m[key].(string); ok {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (b *TaskEventBroadcaster) logLifecycleBroadcast(action string, data map[string]interface{}, sessionID string) {
|
|
switch action {
|
|
case ws.ActionTaskCreated, ws.ActionTaskUpdated, ws.ActionTaskStateChanged,
|
|
ws.ActionTaskDeleted, ws.ActionSessionStateChanged:
|
|
default:
|
|
return
|
|
}
|
|
b.logger.Debug("ws lifecycle broadcast",
|
|
zap.String("action", action),
|
|
zap.Any("task_id", data["task_id"]),
|
|
zap.String("session_id", sessionID),
|
|
zap.Any("state", data["state"]),
|
|
zap.Any("old_state", data["old_state"]),
|
|
zap.Any("new_state", data["new_state"]),
|
|
zap.Any("primary_session_id", data["primary_session_id"]),
|
|
zap.Any("primary_session_state", data["primary_session_state"]),
|
|
zap.Any("updated_at", data["updated_at"]),
|
|
zap.Int("connected_clients", b.hub.GetClientCount()),
|
|
)
|
|
}
|