fix: increase prompt timeout to 60min, add error feedback, rename acp to agent API (#97)
* fix: strip origin/ prefix from base branch for rebase/merge operations The frontend was passing 'origin/main' as the base branch, but the backend expects just the branch name (e.g., 'main') and constructs 'origin/' itself. This caused git fetch to fail with: fatal: couldn't find remote ref origin/main Because the command became 'git fetch origin origin/main' instead of 'git fetch origin main'. * fix: increase prompt timeout to 60min, add error feedback, rename acp to agent API - Add PromptTimeout constant (60 minutes) in common/constants/timeouts.go - Update agentctl client and server to use the constant - Add error message creation when prompt fails (timeout, agent not running, etc) - Rename /acp/* endpoints to /agent/* (protocol-agnostic naming) - Rename acp.go files to agent.go in both client and server - Rename handler functions from handleACP* to handleAgent*
This commit is contained in:
+9
-9
@@ -6,12 +6,12 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/coder/acp-go-sdk"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/kandev/kandev/internal/agentctl/types"
|
||||
"github.com/kandev/kandev/internal/agentctl/types/streams"
|
||||
"github.com/kandev/kandev/internal/common/constants"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -52,7 +52,7 @@ func (c *Client) Initialize(ctx context.Context, clientName, clientVersion strin
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/acp/initialize", bytes.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/agent/initialize", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func (c *Client) NewSession(ctx context.Context, cwd string, mcpServers []types.
|
||||
return "", err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/acp/session/new", bytes.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/agent/session/new", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -152,7 +152,7 @@ func (c *Client) LoadSession(ctx context.Context, sessionID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/acp/session/load", bytes.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/agent/session/load", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -211,10 +211,10 @@ func (c *Client) Prompt(ctx context.Context, text string) (*PromptResponse, erro
|
||||
|
||||
// Use a longer timeout for prompt - agent may take time
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Minute,
|
||||
Timeout: constants.PromptTimeout,
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/acp/prompt", bytes.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/agent/prompt", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -255,7 +255,7 @@ func (c *Client) Prompt(ctx context.Context, text string) (*PromptResponse, erro
|
||||
// StreamUpdates opens a WebSocket connection for streaming agent events.
|
||||
// Events include message chunks, reasoning, tool calls, plan updates, and completion/error.
|
||||
func (c *Client) StreamUpdates(ctx context.Context, handler func(AgentEvent)) error {
|
||||
wsURL := "ws" + c.baseURL[4:] + "/api/v1/acp/stream"
|
||||
wsURL := "ws" + c.baseURL[4:] + "/api/v1/agent/stream"
|
||||
|
||||
conn, _, err := websocket.DefaultDialer.DialContext(ctx, wsURL, nil)
|
||||
if err != nil {
|
||||
@@ -324,7 +324,7 @@ type CancelResponse struct {
|
||||
|
||||
// Cancel interrupts the current agent turn.
|
||||
func (c *Client) Cancel(ctx context.Context) error {
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/acp/cancel", nil)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/agent/cancel", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -371,7 +371,7 @@ func (c *Client) RespondToPermission(ctx context.Context, pendingID, optionID st
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/acp/permissions/respond", bytes.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/api/v1/agent/permissions/respond", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
+12
-11
@@ -9,10 +9,11 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/kandev/kandev/internal/agentctl/types"
|
||||
"github.com/kandev/kandev/internal/common/constants"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// InitializeRequest is a request to initialize the ACP session
|
||||
// InitializeRequest is a request to initialize the agent session.
|
||||
type InitializeRequest struct {
|
||||
ClientName string `json:"client_name"`
|
||||
ClientVersion string `json:"client_version"`
|
||||
@@ -31,7 +32,7 @@ type InitializeResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleACPInitialize(c *gin.Context) {
|
||||
func (s *Server) handleAgentInitialize(c *gin.Context) {
|
||||
var req InitializeRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, InitializeResponse{
|
||||
@@ -91,7 +92,7 @@ type NewSessionResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleACPNewSession(c *gin.Context) {
|
||||
func (s *Server) handleAgentNewSession(c *gin.Context) {
|
||||
var req NewSessionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, NewSessionResponse{
|
||||
@@ -142,7 +143,7 @@ type LoadSessionResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleACPLoadSession(c *gin.Context) {
|
||||
func (s *Server) handleAgentLoadSession(c *gin.Context) {
|
||||
var req LoadSessionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, LoadSessionResponse{
|
||||
@@ -198,7 +199,7 @@ type PromptResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleACPPrompt(c *gin.Context) {
|
||||
func (s *Server) handleAgentPrompt(c *gin.Context) {
|
||||
var req PromptRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, PromptResponse{
|
||||
@@ -227,7 +228,7 @@ func (s *Server) handleACPPrompt(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Use a long timeout for prompt - agent may take time to complete
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Minute)
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), constants.PromptTimeout)
|
||||
defer cancel()
|
||||
|
||||
err := adapter.Prompt(ctx, req.Text)
|
||||
@@ -247,8 +248,8 @@ func (s *Server) handleACPPrompt(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// handleACPStreamWS streams ACP session notifications via WebSocket
|
||||
func (s *Server) handleACPStreamWS(c *gin.Context) {
|
||||
// handleAgentStreamWS streams agent session notifications via WebSocket.
|
||||
func (s *Server) handleAgentStreamWS(c *gin.Context) {
|
||||
conn, err := s.upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
s.logger.Error("WebSocket upgrade failed", zap.Error(err))
|
||||
@@ -260,7 +261,7 @@ func (s *Server) handleACPStreamWS(c *gin.Context) {
|
||||
}
|
||||
}()
|
||||
|
||||
s.logger.Info("ACP stream WebSocket connected")
|
||||
s.logger.Info("agent stream WebSocket connected")
|
||||
|
||||
// Get the session updates channel
|
||||
updatesCh := s.procMgr.GetUpdates()
|
||||
@@ -326,8 +327,8 @@ type CancelResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// handleACPCancel interrupts the current agent turn.
|
||||
func (s *Server) handleACPCancel(c *gin.Context) {
|
||||
// handleAgentCancel interrupts the current agent turn.
|
||||
func (s *Server) handleAgentCancel(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -67,16 +67,14 @@ func (s *Server) setupRoutes() {
|
||||
api.POST("/start", s.handleStart)
|
||||
api.POST("/stop", s.handleStop)
|
||||
|
||||
// ACP high-level methods (using acp-go-sdk)
|
||||
api.POST("/acp/initialize", s.handleACPInitialize)
|
||||
api.POST("/acp/session/new", s.handleACPNewSession)
|
||||
api.POST("/acp/session/load", s.handleACPLoadSession)
|
||||
api.POST("/acp/prompt", s.handleACPPrompt)
|
||||
api.POST("/acp/cancel", s.handleACPCancel)
|
||||
api.GET("/acp/stream", s.handleACPStreamWS)
|
||||
|
||||
// Permission request handling
|
||||
api.POST("/acp/permissions/respond", s.handlePermissionRespond)
|
||||
// Agent session/prompt methods (works with all adapters: ACP, Codex, ClaudeCode, OpenCode)
|
||||
api.POST("/agent/initialize", s.handleAgentInitialize)
|
||||
api.POST("/agent/session/new", s.handleAgentNewSession)
|
||||
api.POST("/agent/session/load", s.handleAgentLoadSession)
|
||||
api.POST("/agent/prompt", s.handleAgentPrompt)
|
||||
api.POST("/agent/cancel", s.handleAgentCancel)
|
||||
api.GET("/agent/stream", s.handleAgentStreamWS)
|
||||
api.POST("/agent/permissions/respond", s.handlePermissionRespond)
|
||||
|
||||
// Unified workspace stream (git status, files, shell)
|
||||
api.GET("/workspace/stream", s.handleWorkspaceStreamWS)
|
||||
|
||||
@@ -245,11 +245,14 @@ func (wt *WorkspaceTracker) checkGitChanges(ctx context.Context) {
|
||||
wt.cachedHeadSHA = currentHead
|
||||
wt.gitStateMu.Unlock()
|
||||
|
||||
// Check if HEAD moved backward (reset, rebase, etc.)
|
||||
// This happens when currentHead is an ancestor of previousHead
|
||||
// Check if history was rewritten (reset, rebase, amend, etc.)
|
||||
// There are three cases:
|
||||
// 1. HEAD moved backward: currentHead is an ancestor of previousHead (e.g., git reset HEAD~1)
|
||||
// 2. History rewritten: previousHead is NOT reachable from currentHead (e.g., git rebase -i, git commit --amend)
|
||||
// 3. HEAD moved forward: previousHead IS an ancestor of currentHead (normal commits)
|
||||
if previousHead != "" {
|
||||
if wt.isAncestor(ctx, currentHead, previousHead) {
|
||||
// HEAD moved backward - emit reset notification
|
||||
// Case 1: HEAD moved backward - emit reset notification
|
||||
wt.logger.Info("detected git reset (HEAD moved backward)",
|
||||
zap.String("previous", previousHead),
|
||||
zap.String("current", currentHead))
|
||||
@@ -258,8 +261,19 @@ func (wt *WorkspaceTracker) checkGitChanges(ctx context.Context) {
|
||||
PreviousHead: previousHead,
|
||||
CurrentHead: currentHead,
|
||||
})
|
||||
} else if !wt.isAncestor(ctx, previousHead, currentHead) {
|
||||
// Case 2: History was rewritten - previousHead is not reachable from currentHead
|
||||
// This happens with interactive rebase, commit amend, etc.
|
||||
wt.logger.Info("detected git history rewrite (previous HEAD not reachable)",
|
||||
zap.String("previous", previousHead),
|
||||
zap.String("current", currentHead))
|
||||
wt.notifyWorkspaceStreamGitReset(&types.GitResetNotification{
|
||||
Timestamp: time.Now(),
|
||||
PreviousHead: previousHead,
|
||||
CurrentHead: currentHead,
|
||||
})
|
||||
} else {
|
||||
// HEAD moved forward - get new commits
|
||||
// Case 3: HEAD moved forward normally - get new commits
|
||||
commits := wt.getCommitsSince(ctx, previousHead)
|
||||
|
||||
// Filter out commits that are already on remote branches.
|
||||
|
||||
@@ -18,4 +18,9 @@ const (
|
||||
// TaskDeleteTimeout is the maximum time to wait for task deletion,
|
||||
// including cleanup scripts and worktree removal.
|
||||
TaskDeleteTimeout = 2 * time.Minute
|
||||
|
||||
// PromptTimeout is the maximum time to wait for an agent to complete a prompt.
|
||||
// Agent tasks can take a long time (complex code generation, large refactors),
|
||||
// so this is set to a generous value.
|
||||
PromptTimeout = 60 * time.Minute
|
||||
)
|
||||
|
||||
@@ -208,9 +208,36 @@ func (h *MessageHandlers) wsAddMessage(ctx context.Context, msg *ws.Message) (*w
|
||||
}
|
||||
}
|
||||
}
|
||||
h.logger.Warn("failed to forward message as prompt to agent",
|
||||
zap.String("task_id", taskID),
|
||||
zap.Error(err))
|
||||
// If prompt still failed after retries, create an error message for the user
|
||||
if err != nil {
|
||||
h.logger.Warn("failed to forward message as prompt to agent",
|
||||
zap.String("task_id", taskID),
|
||||
zap.Error(err))
|
||||
|
||||
// Create an error message so the user sees feedback
|
||||
errorMsg := "Failed to send message to agent"
|
||||
if strings.Contains(err.Error(), "context deadline exceeded") || strings.Contains(err.Error(), "timeout") {
|
||||
errorMsg = "Request timed out. The agent may be processing a complex task. Please try again."
|
||||
} else if errors.Is(err, executor.ErrExecutionNotFound) {
|
||||
errorMsg = "Agent is not running. Please restart the session."
|
||||
}
|
||||
|
||||
if _, createErr := h.messageController.CreateMessage(promptCtx, dto.CreateMessageRequest{
|
||||
TaskSessionID: sessionID,
|
||||
TaskID: taskID,
|
||||
Content: errorMsg,
|
||||
AuthorType: "agent",
|
||||
Type: string(v1.MessageTypeError),
|
||||
Metadata: map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
},
|
||||
}); createErr != nil {
|
||||
h.logger.Error("failed to create error message for prompt failure",
|
||||
zap.String("task_id", taskID),
|
||||
zap.String("session_id", sessionID),
|
||||
zap.Error(createErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user