From 8d7166e67ebe7c93b4ecca5ab9a2f02fa92d87a0 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sun, 31 May 2026 21:51:33 -0400 Subject: [PATCH] refactor(server): share prompt validation before background dispatch Use the same prompt and session checks before accepting background work that synchronous runs already use. This keeps rejected prompts consistent as the server begins accepting requests asynchronously. Co-Authored-By: Charm Crush --- internal/agent/agent.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index ef37f164d711c608916e98dc15ddedcaf1694033..bc3df59e7626943ca31cbc293c0b76a814c05fae 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -183,12 +183,25 @@ func NewSessionAgent( } } -func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (result *fantasy.AgentResult, retErr error) { +// ValidateCall performs the cheap structural validation that +// sessionAgent.Run requires before a call can be dispatched: a call must +// carry either a non-empty prompt or a text attachment, and it must name a +// session. It is exported so callers that accept a run before dispatching it +// (e.g. backend.SendMessage) can apply the same checks and keep the error +// contract consistent. +func ValidateCall(call SessionAgentCall) error { if call.Prompt == "" && !message.ContainsTextAttachment(call.Attachments) { - return nil, ErrEmptyPrompt + return ErrEmptyPrompt } if call.SessionID == "" { - return nil, ErrSessionMissing + return ErrSessionMissing + } + return nil +} + +func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (result *fantasy.AgentResult, retErr error) { + if err := ValidateCall(call); err != nil { + return nil, err } // Queue the message if busy. Strip OnComplete: the caller that