fix: session recovery after backend restart (#126)

This commit is contained in:
João Salavisa
2026-01-30 21:58:00 +00:00
committed by GitHub
parent 8a08459001
commit f57fd4b184
5 changed files with 27 additions and 1 deletions
@@ -13,6 +13,7 @@ import (
agentctltypes "github.com/kandev/kandev/internal/agentctl/types"
"github.com/kandev/kandev/internal/agentctl/types/streams"
"github.com/kandev/kandev/internal/common/appctx"
"github.com/kandev/kandev/internal/common/constants"
"github.com/kandev/kandev/internal/common/logger"
v1 "github.com/kandev/kandev/pkg/api/v1"
"github.com/kandev/kandev/pkg/agent"
@@ -332,7 +333,8 @@ func (sm *SessionManager) InitializeAndPrompt(
go func() {
// Use detached context that respects stopCh for graceful shutdown
promptCtx, cancel := appctx.Detached(ctx, sm.stopCh, 10*time.Minute)
// Use PromptTimeout (60 min) to match the HTTP client/server timeouts
promptCtx, cancel := appctx.Detached(ctx, sm.stopCh, constants.PromptTimeout)
defer cancel()
_, err := sm.SendPrompt(promptCtx, execution, effectivePrompt, false, markReady)
@@ -529,6 +529,9 @@ func (s *Service) resumeExecutorsOnStartup(ctx context.Context) {
zap.String("session_id", sessionID),
zap.Error(err))
_ = s.repo.UpdateTaskSessionState(ctx, sessionID, models.TaskSessionStateFailed, err.Error())
// Clear the stale execution ID to prevent "execution not found" errors
// when the user tries to prompt after the failed resume
_ = s.repo.ClearSessionExecutionID(ctx, sessionID)
_ = s.repo.DeleteExecutorRunningBySessionID(ctx, sessionID)
continue
}
@@ -173,6 +173,9 @@ func (m *mockRepository) UpdateTaskSession(ctx context.Context, session *models.
func (m *mockRepository) UpdateTaskSessionState(ctx context.Context, id string, state models.TaskSessionState, errorMessage string) error {
return nil
}
func (m *mockRepository) ClearSessionExecutionID(ctx context.Context, id string) error {
return nil
}
func (m *mockRepository) ListTaskSessions(ctx context.Context, taskID string) ([]*models.TaskSession, error) {
return nil, nil
}
@@ -68,6 +68,7 @@ type Repository interface {
GetActiveTaskSessionByTaskID(ctx context.Context, taskID string) (*models.TaskSession, error)
UpdateTaskSession(ctx context.Context, session *models.TaskSession) error
UpdateTaskSessionState(ctx context.Context, id string, state models.TaskSessionState, errorMessage string) error
ClearSessionExecutionID(ctx context.Context, id string) error
ListTaskSessions(ctx context.Context, taskID string) ([]*models.TaskSession, error)
ListActiveTaskSessions(ctx context.Context) ([]*models.TaskSession, error)
ListActiveTaskSessionsByTaskID(ctx context.Context, taskID string) ([]*models.TaskSession, error)
@@ -541,6 +541,23 @@ func (r *Repository) UpdateTaskSessionState(ctx context.Context, id string, stat
return nil
}
// ClearSessionExecutionID clears the agent_execution_id for a session.
// This is used when a stale execution ID needs to be removed (e.g., after a failed resume on startup).
func (r *Repository) ClearSessionExecutionID(ctx context.Context, id string) error {
now := time.Now().UTC()
result, err := r.db.ExecContext(ctx, `
UPDATE task_sessions SET agent_execution_id = '', container_id = '', updated_at = ? WHERE id = ?
`, now, id)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return fmt.Errorf("agent session not found: %s", id)
}
return nil
}
// ListTaskSessions returns all agent sessions for a task
func (r *Repository) ListTaskSessions(ctx context.Context, taskID string) ([]*models.TaskSession, error) {
rows, err := r.db.QueryContext(ctx, `