1a467b969f
* fix(web): wire permission approval UI for Kandev MCP tools
KandevToolMessage adapter ignored the permissionMessage prop, so MCP tools
that block on a permission request rendered the result row but no Approve/
Deny buttons — users could not unblock the agent. Extract the shared
permission handlers into use-permission-handlers.ts and reuse them from
both ToolCallMessage and KandevToolMessage. KandevRow now also shows an
amber pending clock icon and hides the result-count summary while waiting
for approval so the row reads correctly before the result arrives.
Adds a mock-agent scenarioKandevMCPPermission emitting
mcp__kandev__list_workspaces_kandev plus a Playwright e2e and unit tests
covering the pending UI and the absence of "0 workspaces" while pending.
* fix(web): collapse tool + permission status into resolved Kandev row
After approve/reject the orchestrator stamps the decision onto the
permission_request message immediately, but the tool_call's own
meta.status stays "pending" until the next tool_call update lands.
KandevToolMessage previously passed meta.status straight to the renderer,
so the row kept the amber pending clock and the hidden-summary state for
the brief window between the two events. resolveKandevStatus now prefers
the permission decision once present, mapping approved -> complete and
rejected -> error.
Drops a redundant Playwright `.first()` after a toHaveCount(1) assertion
on the same locator. Adds unit coverage for the resolved-decision state.
* fix(web): keep Kandev tool status independent of permission decision
The previous fix collapsed an approved permission into tool status
"complete" and a rejected permission into "error". That was wrong on two
counts: an approved tool that's still running rendered as already done
(green check + visible result summary built from an empty payload), and
any later tool_call error was silently masked because the status had
already been forced to "complete".
Replace the status override with a separate display overlay. KandevRow
now reads a `KandevPermissionUIContext` ("pending" | "rejected" |
undefined) and uses it only to drive the waiting-on-user affordances —
amber clock, hidden summary — and a rejection mark that wins over the
tool's own success state. The renderer always receives the real
tool_call status, so a tool that's approved-but-pending still reads as
pending and a tool that errors after approval still renders the red X.
Adds explicit regression tests for the two race-window failure modes
plus the new rejected overlay.
* test(web): tolerate duplicate-render backend race in Kandev MCP e2e
CI shard 1 sometimes renders two approve buttons for the same Kandev MCP
permission because the orchestrator can create a second generic tool_call
message for the same tool_call_id alongside the kandev-routed one. Both
buttons resolve the same pending_id, so clicking the first unblocks the
agent either way.
The bug the test guards against is "no approve button at all" — the
strict toHaveCount(1) assertion was tying the test to that separate
backend issue. Relax to at-least-one and click the first.
* fix: persist Deny clicks as rejected, not approved
Backend RespondToPermission previously derived status purely from cancelled:
cancelled=true -> "rejected", otherwise "approved". Deny clicks sent
cancelled=false with a reject_* option_id, so they were recorded as
"approved". Add an explicit rejected flag from the WS request so the
backend can record "rejected" without taking the cancellation path (which
races against permission_cancelled -> "expired").
Also refactor permission-request-message.tsx to consume the shared
usePermissionResponseHandlers hook (the duplicated inline handler had the
same bug) and introduce a typed models.PermissionStatus enum on the
backend so status strings are not scattered literals.
* test(web): scope Kandev MCP permission e2e to the Kandev row
Previously the test clicked the first matching permission-approve button,
which could be a duplicate generic ToolCallMessage fallback rendered for
the same pending_id by a backend race -- a green test no longer proved the
custom Kandev approval UI rendered.
Add a `data-testid="kandev-tool-permission"` wrapper around the Kandev
action row and a `kandevPermissionApproveButtons` page-object locator that
scopes through it, then assert `toHaveCount(1)` so the test only passes
when the Kandev custom row actually mounts.
* chore(orchestrator): log rejected flag in RespondToPermission
Debug log was missing the new rejected param, making it impossible to
distinguish explicit Deny from dialog cancellation when triaging
permission-flow issues.
656 lines
22 KiB
Go
656 lines
22 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/kandev/kandev/internal/agentctl/types/streams"
|
|
"github.com/kandev/kandev/internal/events"
|
|
"github.com/kandev/kandev/internal/task/models"
|
|
)
|
|
|
|
// CreateMessage creates a new message on an agent session
|
|
func (s *Service) CreateMessage(ctx context.Context, req *CreateMessageRequest) (*models.Message, error) {
|
|
session, err := s.sessions.GetTaskSession(ctx, req.TaskSessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
authorType := models.MessageAuthorUser
|
|
if req.AuthorType == "agent" {
|
|
authorType = models.MessageAuthorAgent
|
|
}
|
|
|
|
messageType := models.MessageType(req.Type)
|
|
if messageType == "" {
|
|
messageType = models.MessageTypeMessage
|
|
}
|
|
|
|
taskID := req.TaskID
|
|
if taskID == "" && session != nil {
|
|
taskID = session.TaskID
|
|
}
|
|
|
|
// Ensure we have a turn ID - get active turn or start a new one
|
|
turnID := req.TurnID
|
|
if turnID == "" {
|
|
turn, err := s.getOrStartTurn(ctx, req.TaskSessionID)
|
|
if err != nil {
|
|
s.logger.Warn("failed to get or start turn for message",
|
|
zap.String("session_id", req.TaskSessionID),
|
|
zap.Error(err))
|
|
// Continue with empty turn ID - will fail on foreign key if turn is required
|
|
} else if turn != nil {
|
|
turnID = turn.ID
|
|
}
|
|
}
|
|
|
|
message := &models.Message{
|
|
ID: uuid.New().String(),
|
|
TaskSessionID: req.TaskSessionID,
|
|
TaskID: taskID,
|
|
TurnID: turnID,
|
|
AuthorType: authorType,
|
|
AuthorID: req.AuthorID,
|
|
Content: req.Content,
|
|
Type: messageType,
|
|
Metadata: req.Metadata,
|
|
RequestsInput: req.RequestsInput,
|
|
CreatedAt: time.Now().UTC(),
|
|
}
|
|
|
|
if err := s.messages.CreateMessage(ctx, message); err != nil {
|
|
s.logger.Error("failed to create message", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
// Publish message.added event
|
|
s.publishMessageEvent(ctx, events.MessageAdded, message)
|
|
|
|
s.logger.Info("message created",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("session_id", message.TaskSessionID),
|
|
zap.String("author_type", string(message.AuthorType)))
|
|
|
|
return message, nil
|
|
}
|
|
|
|
// CreateMessageWithID creates a new message with a pre-generated ID.
|
|
// This is used for streaming messages where the ID is generated by the caller.
|
|
// It includes retry logic to handle transient database errors and ensure
|
|
// message chunks are not lost during streaming.
|
|
func (s *Service) CreateMessageWithID(ctx context.Context, id string, req *CreateMessageRequest) (*models.Message, error) {
|
|
const maxRetries = 5
|
|
const retryDelay = 50 * time.Millisecond
|
|
|
|
session, err := s.getSessionWithRetry(ctx, req.TaskSessionID, id, maxRetries, retryDelay)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
message := s.buildMessage(ctx, id, req, session)
|
|
|
|
if err := s.createMessageWithRetry(ctx, message, maxRetries, retryDelay); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Publish message.added event
|
|
s.publishMessageEvent(ctx, events.MessageAdded, message)
|
|
|
|
s.logger.Info("message created with ID",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("session_id", message.TaskSessionID),
|
|
zap.String("author_type", string(message.AuthorType)))
|
|
|
|
return message, nil
|
|
}
|
|
|
|
// getSessionWithRetry fetches a session, retrying on transient errors caused by out-of-order events.
|
|
func (s *Service) getSessionWithRetry(ctx context.Context, sessionID, messageID string, maxRetries int, retryDelay time.Duration) (*models.TaskSession, error) {
|
|
var session *models.TaskSession
|
|
var err error
|
|
for attempt := 0; attempt < maxRetries; attempt++ {
|
|
session, err = s.sessions.GetTaskSession(ctx, sessionID)
|
|
if err == nil {
|
|
return session, nil
|
|
}
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
if attempt < maxRetries-1 {
|
|
s.logger.Debug("session not found for message create, retrying",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("message_id", messageID),
|
|
zap.Int("attempt", attempt+1),
|
|
zap.Int("max_retries", maxRetries))
|
|
time.Sleep(retryDelay)
|
|
}
|
|
}
|
|
s.logger.Warn("session not found for message create after retries",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("message_id", messageID),
|
|
zap.Int("retries", maxRetries),
|
|
zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
// buildMessage constructs a Message model from a CreateMessageRequest and resolved session.
|
|
func (s *Service) buildMessage(ctx context.Context, id string, req *CreateMessageRequest, session *models.TaskSession) *models.Message {
|
|
authorType := models.MessageAuthorUser
|
|
if req.AuthorType == "agent" {
|
|
authorType = models.MessageAuthorAgent
|
|
}
|
|
|
|
messageType := models.MessageType(req.Type)
|
|
if messageType == "" {
|
|
messageType = models.MessageTypeMessage
|
|
}
|
|
|
|
taskID := req.TaskID
|
|
if taskID == "" && session != nil {
|
|
taskID = session.TaskID
|
|
}
|
|
|
|
turnID := req.TurnID
|
|
if turnID == "" {
|
|
if turn, err := s.getOrStartTurn(ctx, req.TaskSessionID); err != nil {
|
|
s.logger.Warn("failed to get or start turn for streaming message",
|
|
zap.String("session_id", req.TaskSessionID),
|
|
zap.Error(err))
|
|
} else if turn != nil {
|
|
turnID = turn.ID
|
|
}
|
|
}
|
|
|
|
return &models.Message{
|
|
ID: id,
|
|
TaskSessionID: req.TaskSessionID,
|
|
TaskID: taskID,
|
|
TurnID: turnID,
|
|
AuthorType: authorType,
|
|
AuthorID: req.AuthorID,
|
|
Content: req.Content,
|
|
Type: messageType,
|
|
Metadata: req.Metadata,
|
|
RequestsInput: req.RequestsInput,
|
|
CreatedAt: time.Now().UTC(),
|
|
}
|
|
}
|
|
|
|
// createMessageWithRetry persists a message with retry logic for transient DB errors.
|
|
func (s *Service) createMessageWithRetry(ctx context.Context, message *models.Message, maxRetries int, retryDelay time.Duration) error {
|
|
var err error
|
|
for attempt := 0; attempt < maxRetries; attempt++ {
|
|
err = s.messages.CreateMessage(ctx, message)
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
if attempt < maxRetries-1 {
|
|
s.logger.Debug("failed to create message, retrying",
|
|
zap.String("message_id", message.ID),
|
|
zap.Int("attempt", attempt+1),
|
|
zap.Int("max_retries", maxRetries),
|
|
zap.Error(err))
|
|
time.Sleep(retryDelay)
|
|
}
|
|
}
|
|
s.logger.Error("failed to create message with ID after retries",
|
|
zap.String("id", message.ID),
|
|
zap.Int("retries", maxRetries),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// GetMessage retrieves a message by ID
|
|
func (s *Service) GetMessage(ctx context.Context, id string) (*models.Message, error) {
|
|
return s.messages.GetMessage(ctx, id)
|
|
}
|
|
|
|
// ListMessages returns all messages for a session.
|
|
func (s *Service) ListMessages(ctx context.Context, sessionID string) ([]*models.Message, error) {
|
|
return s.messages.ListMessages(ctx, sessionID)
|
|
}
|
|
|
|
// ListMessagesPaginated returns messages for a session with pagination options.
|
|
func (s *Service) ListMessagesPaginated(ctx context.Context, req ListMessagesRequest) ([]*models.Message, bool, error) {
|
|
limit := req.Limit
|
|
if limit <= 0 && (req.Before != "" || req.After != "") {
|
|
limit = DefaultMessagesPageSize
|
|
}
|
|
if limit > MaxMessagesPageSize {
|
|
limit = MaxMessagesPageSize
|
|
}
|
|
return s.messages.ListMessagesPaginated(ctx, req.TaskSessionID, models.ListMessagesOptions{
|
|
Limit: limit,
|
|
Before: req.Before,
|
|
After: req.After,
|
|
Sort: req.Sort,
|
|
})
|
|
}
|
|
|
|
// SearchMessages returns messages whose content matches the query in the given session.
|
|
func (s *Service) SearchMessages(ctx context.Context, sessionID, query string, limit int) ([]*models.Message, error) {
|
|
return s.messages.SearchMessages(ctx, sessionID, models.SearchMessagesOptions{
|
|
Query: query,
|
|
Limit: limit,
|
|
})
|
|
}
|
|
|
|
// DeleteMessage deletes a message
|
|
func (s *Service) DeleteMessage(ctx context.Context, id string) error {
|
|
if err := s.messages.DeleteMessage(ctx, id); err != nil {
|
|
s.logger.Error("failed to delete message", zap.String("message_id", id), zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
s.logger.Info("message deleted", zap.String("message_id", id))
|
|
return nil
|
|
}
|
|
|
|
// UpdateMessage updates an existing message and publishes an event.
|
|
func (s *Service) UpdateMessage(ctx context.Context, message *models.Message) error {
|
|
if err := s.messages.UpdateMessage(ctx, message); err != nil {
|
|
s.logger.Error("failed to update message",
|
|
zap.String("message_id", message.ID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Publish message.updated event for real-time streaming
|
|
s.publishMessageEvent(ctx, events.MessageUpdated, message)
|
|
|
|
return nil
|
|
}
|
|
|
|
// AppendMessageContent appends additional content to an existing message.
|
|
// This is used for streaming agent responses where content arrives incrementally.
|
|
func (s *Service) AppendMessageContent(ctx context.Context, messageID, additionalContent string) error {
|
|
message, err := s.messages.GetMessage(ctx, messageID)
|
|
if err != nil {
|
|
s.logger.Warn("message not found for append",
|
|
zap.String("message_id", messageID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Append the new content
|
|
message.Content += additionalContent
|
|
|
|
if err := s.messages.UpdateMessage(ctx, message); err != nil {
|
|
s.logger.Error("failed to append message content",
|
|
zap.String("message_id", messageID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Publish message.updated event for real-time streaming
|
|
s.publishMessageEvent(ctx, events.MessageUpdated, message)
|
|
|
|
s.logger.Debug("message content appended",
|
|
zap.String("message_id", messageID),
|
|
zap.Int("appended_length", len(additionalContent)),
|
|
zap.Int("total_length", len(message.Content)))
|
|
|
|
return nil
|
|
}
|
|
|
|
// AppendThinkingContent appends additional thinking content to an existing thinking message.
|
|
// This updates the metadata.thinking field for streaming agent reasoning.
|
|
func (s *Service) AppendThinkingContent(ctx context.Context, messageID, additionalContent string) error {
|
|
message, err := s.messages.GetMessage(ctx, messageID)
|
|
if err != nil {
|
|
s.logger.Warn("thinking message not found for append",
|
|
zap.String("message_id", messageID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Initialize metadata if nil
|
|
if message.Metadata == nil {
|
|
message.Metadata = make(map[string]interface{})
|
|
}
|
|
|
|
// Get existing thinking content and append
|
|
existingThinking := ""
|
|
if existing, ok := message.Metadata["thinking"].(string); ok {
|
|
existingThinking = existing
|
|
}
|
|
message.Metadata["thinking"] = existingThinking + additionalContent
|
|
|
|
if err := s.messages.UpdateMessage(ctx, message); err != nil {
|
|
s.logger.Error("failed to append thinking content",
|
|
zap.String("message_id", messageID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Publish message.updated event for real-time streaming
|
|
s.publishMessageEvent(ctx, events.MessageUpdated, message)
|
|
|
|
s.logger.Debug("thinking content appended",
|
|
zap.String("message_id", messageID),
|
|
zap.Int("appended_length", len(additionalContent)))
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateToolCallMessage updates a tool call message's status, optionally title and normalized data.
|
|
// It includes retry logic to handle race conditions where the complete event
|
|
// may arrive before the message has been created by the start event.
|
|
// If the message is not found after retries and taskID/turnID/msgType are provided, it creates the message.
|
|
// The normalized parameter contains typed tool payload data that gets added to metadata.
|
|
func (s *Service) UpdateToolCallMessage(ctx context.Context, sessionID, toolCallID, status, result, title string, normalized *streams.NormalizedPayload) error {
|
|
return s.UpdateToolCallMessageWithCreate(ctx, sessionID, toolCallID, "", status, result, title, normalized, "", "", "")
|
|
}
|
|
|
|
// UpdateToolCallMessageWithCreate is like UpdateToolCallMessage but can create the message if not found.
|
|
// If taskID, turnID, and msgType are provided, the message will be created if it doesn't exist.
|
|
// parentToolCallID is used for subagent nesting (empty for top-level).
|
|
func (s *Service) UpdateToolCallMessageWithCreate(ctx context.Context, sessionID, toolCallID, parentToolCallID, status, result, title string, normalized *streams.NormalizedPayload, taskID, turnID, msgType string) error {
|
|
const maxRetries = 5
|
|
const retryDelay = 100 * time.Millisecond
|
|
|
|
message, err := s.getToolCallMessageWithRetry(ctx, sessionID, toolCallID, maxRetries, retryDelay)
|
|
|
|
// If message not found and we have enough info to create it, do so
|
|
if err != nil && taskID != "" && msgType != "" {
|
|
return s.createToolCallMessageFallback(ctx, sessionID, toolCallID, parentToolCallID, status, title, turnID, taskID, msgType, normalized)
|
|
}
|
|
|
|
if err != nil {
|
|
s.logger.Warn("tool call message not found for update after retries",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.Int("retries", maxRetries),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
s.applyToolCallMessageUpdate(message, status, result, title, normalized)
|
|
|
|
if err := s.messages.UpdateMessage(ctx, message); err != nil {
|
|
s.logger.Error("failed to update tool call message",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Publish message.updated event
|
|
s.publishMessageEvent(ctx, events.MessageUpdated, message)
|
|
|
|
s.logger.Info("tool call message updated",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.String("status", status))
|
|
|
|
return nil
|
|
}
|
|
|
|
// getToolCallMessageWithRetry fetches a tool call message with retry logic for race conditions.
|
|
func (s *Service) getToolCallMessageWithRetry(ctx context.Context, sessionID, toolCallID string, maxRetries int, retryDelay time.Duration) (*models.Message, error) {
|
|
var message *models.Message
|
|
var err error
|
|
for attempt := 0; attempt < maxRetries; attempt++ {
|
|
message, err = s.messages.GetMessageByToolCallID(ctx, sessionID, toolCallID)
|
|
if err == nil {
|
|
return message, nil
|
|
}
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
if attempt < maxRetries-1 {
|
|
s.logger.Debug("tool call message not found, retrying",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.Int("attempt", attempt+1),
|
|
zap.Int("max_retries", maxRetries))
|
|
time.Sleep(retryDelay)
|
|
}
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
// createToolCallMessageFallback creates a tool call message when it cannot be found via GetMessageByToolCallID.
|
|
func (s *Service) createToolCallMessageFallback(ctx context.Context, sessionID, toolCallID, parentToolCallID, status, title, turnID, taskID, msgType string, normalized *streams.NormalizedPayload) error {
|
|
s.logger.Info("tool call message not found, creating it",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.String("task_id", taskID),
|
|
zap.String("msg_type", msgType))
|
|
|
|
metadata := map[string]interface{}{
|
|
"tool_call_id": toolCallID,
|
|
"title": title,
|
|
"status": status,
|
|
}
|
|
if parentToolCallID != "" {
|
|
metadata["parent_tool_call_id"] = parentToolCallID
|
|
}
|
|
if normalized != nil {
|
|
metadata["normalized"] = normalized
|
|
}
|
|
|
|
msg, createErr := s.CreateMessage(ctx, &CreateMessageRequest{
|
|
TaskSessionID: sessionID,
|
|
TaskID: taskID,
|
|
TurnID: turnID,
|
|
Content: title,
|
|
AuthorType: "agent",
|
|
Type: msgType,
|
|
Metadata: metadata,
|
|
})
|
|
if createErr != nil {
|
|
s.logger.Error("failed to create tool call message as fallback",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.Error(createErr))
|
|
return createErr
|
|
}
|
|
|
|
s.logger.Info("created tool call message as fallback",
|
|
zap.String("message_id", msg.ID),
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.String("status", status))
|
|
return nil
|
|
}
|
|
|
|
// applyToolCallMessageUpdate applies status, result, normalized data, and title to a tool call message.
|
|
//
|
|
// Defensive guard: a permission_request row also carries `tool_call_id` in metadata, so
|
|
// any future code path that hands such a row in here would otherwise silently overwrite
|
|
// the user's approve/reject decision and retype it to tool_execute. The repository's
|
|
// GetMessageByToolCallID excludes permission_request, but the guard makes the invariant
|
|
// explicit at the layer that does the writing.
|
|
func (s *Service) applyToolCallMessageUpdate(message *models.Message, status, result, title string, normalized *streams.NormalizedPayload) {
|
|
if message.Type == models.MessageTypePermissionRequest {
|
|
// Error severity: the repo-layer GetMessageByToolCallID filter is supposed
|
|
// to make this branch unreachable. Reaching it means a caller bug; surface
|
|
// it loudly so the invariant violation isn't silently swallowed.
|
|
s.logger.Error("applyToolCallMessageUpdate refusing to overwrite permission_request",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("incoming_status", status))
|
|
return
|
|
}
|
|
if message.Metadata == nil {
|
|
message.Metadata = make(map[string]interface{})
|
|
}
|
|
message.Metadata["status"] = status
|
|
if result != "" {
|
|
message.Metadata["result"] = result
|
|
}
|
|
|
|
if normalized != nil {
|
|
message.Metadata["normalized"] = normalized
|
|
// Update message type if the normalized kind changed
|
|
// This handles cases like Read on a directory converting to code_search
|
|
newMsgType := models.MessageType(normalized.Kind().ToMessageType())
|
|
if newMsgType != message.Type {
|
|
s.logger.Debug("updating message type based on normalized kind",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("old_type", string(message.Type)),
|
|
zap.String("new_type", string(newMsgType)),
|
|
zap.String("normalized_kind", string(normalized.Kind())))
|
|
message.Type = newMsgType
|
|
}
|
|
}
|
|
|
|
// Update title/content if provided and different from current
|
|
if title != "" && title != message.Content {
|
|
message.Content = title
|
|
message.Metadata["title"] = title
|
|
}
|
|
}
|
|
|
|
// UpdatePermissionMessage updates a permission request message's status.
|
|
// It includes retry logic to handle race conditions.
|
|
func (s *Service) UpdatePermissionMessage(ctx context.Context, sessionID, pendingID string, status models.PermissionStatus) error {
|
|
const maxRetries = 5
|
|
const retryDelay = 100 * time.Millisecond
|
|
|
|
var message *models.Message
|
|
var err error
|
|
|
|
// Retry loop to handle race condition
|
|
for attempt := 0; attempt < maxRetries; attempt++ {
|
|
message, err = s.messages.GetMessageByPendingID(ctx, sessionID, pendingID)
|
|
if err == nil {
|
|
break
|
|
}
|
|
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
|
|
if attempt < maxRetries-1 {
|
|
s.logger.Debug("permission message not found, retrying",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.Int("attempt", attempt+1),
|
|
zap.Int("max_retries", maxRetries))
|
|
time.Sleep(retryDelay)
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
s.logger.Warn("permission message not found for update after retries",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.Int("retries", maxRetries),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
if message.Metadata == nil {
|
|
message.Metadata = make(map[string]interface{})
|
|
}
|
|
message.Metadata["status"] = string(status)
|
|
|
|
if err := s.messages.UpdateMessage(ctx, message); err != nil {
|
|
s.logger.Error("failed to update permission message",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
// Publish message.updated event
|
|
s.publishMessageEvent(ctx, events.MessageUpdated, message)
|
|
|
|
// When a permission expires, also mark the related tool call as cancelled
|
|
// so the UI no longer shows a loading spinner on the tool call.
|
|
if status == models.PermissionStatusExpired {
|
|
if toolCallID, ok := message.Metadata["tool_call_id"].(string); ok && toolCallID != "" {
|
|
if err := s.UpdateToolCallMessage(ctx, sessionID, toolCallID, "error", "", "", nil); err != nil {
|
|
s.logger.Warn("failed to cancel related tool call message",
|
|
zap.String("tool_call_id", toolCallID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.Error(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
s.logger.Info("permission message updated",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.String("status", string(status)))
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateClarificationMessageForQuestion updates a single clarification message
|
|
// (identified by pending_id + question_id) with the new status and the answer
|
|
// payload. Used by both single- and multi-question clarification bundles since
|
|
// each question lives in its own chat message.
|
|
func (s *Service) UpdateClarificationMessageForQuestion(ctx context.Context, sessionID, pendingID, questionID, status string, answer interface{}) error {
|
|
const maxRetries = 5
|
|
const retryDelay = 100 * time.Millisecond
|
|
|
|
var message *models.Message
|
|
var err error
|
|
|
|
for attempt := 0; attempt < maxRetries; attempt++ {
|
|
message, err = s.messages.FindMessageByPendingIDAndQuestion(ctx, sessionID, pendingID, questionID)
|
|
if err == nil {
|
|
break
|
|
}
|
|
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
|
|
if attempt < maxRetries-1 {
|
|
s.logger.Debug("clarification message not found, retrying",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.String("question_id", questionID),
|
|
zap.Int("attempt", attempt+1),
|
|
zap.Int("max_retries", maxRetries))
|
|
time.Sleep(retryDelay)
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
s.logger.Warn("clarification message not found for update after retries",
|
|
zap.String("session_id", sessionID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.String("question_id", questionID),
|
|
zap.Int("retries", maxRetries),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
if message.Metadata == nil {
|
|
message.Metadata = make(map[string]interface{})
|
|
}
|
|
message.Metadata["status"] = status
|
|
if answer != nil {
|
|
message.Metadata["response"] = answer
|
|
}
|
|
|
|
if err := s.messages.UpdateMessage(ctx, message); err != nil {
|
|
s.logger.Error("failed to update clarification message",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.String("question_id", questionID),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
s.publishMessageEvent(ctx, events.MessageUpdated, message)
|
|
|
|
s.logger.Info("clarification message updated",
|
|
zap.String("message_id", message.ID),
|
|
zap.String("pending_id", pendingID),
|
|
zap.String("question_id", questionID),
|
|
zap.String("status", status))
|
|
|
|
return nil
|
|
}
|