feat(workflow): implement workflow system with steps terminology (#102)
This commit is contained in:
@@ -4,6 +4,8 @@ package sqlite
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
commonsqlite "github.com/kandev/kandev/internal/common/sqlite"
|
||||
)
|
||||
|
||||
// Repository provides SQLite-based task storage operations.
|
||||
@@ -45,47 +47,7 @@ func (r *Repository) DB() *sql.DB {
|
||||
|
||||
// ensureColumn adds a column to a table if it doesn't exist
|
||||
func (r *Repository) ensureColumn(table, column, definition string) error {
|
||||
exists, err := r.columnExists(table, column)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
_, err = r.db.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", table, column, definition))
|
||||
return err
|
||||
}
|
||||
|
||||
// columnExists checks if a column exists in a table
|
||||
func (r *Repository) columnExists(table, column string) (bool, error) {
|
||||
rows, err := r.db.Query(fmt.Sprintf("PRAGMA table_info(%s)", table))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
for rows.Next() {
|
||||
var cid int
|
||||
var name, colType string
|
||||
var notNull int
|
||||
var defaultValue *string
|
||||
var pk int
|
||||
if err := rows.Scan(&cid, &name, &colType, ¬Null, &defaultValue, &pk); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if name == column {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, rows.Err()
|
||||
}
|
||||
|
||||
// boolToInt converts a boolean to an integer (for SQLite)
|
||||
func boolToInt(value bool) int {
|
||||
if value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
return commonsqlite.EnsureColumn(r.db, table, column, definition)
|
||||
}
|
||||
|
||||
// ensureWorkspaceIndexes creates workspace-related indexes
|
||||
@@ -173,23 +135,11 @@ func (r *Repository) initSchema() error {
|
||||
updated_at DATETIME NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS columns (
|
||||
id TEXT PRIMARY KEY,
|
||||
board_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
position INTEGER DEFAULT 0,
|
||||
state TEXT DEFAULT 'TODO',
|
||||
color TEXT NOT NULL DEFAULT '',
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL,
|
||||
FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tasks (
|
||||
id TEXT PRIMARY KEY,
|
||||
workspace_id TEXT NOT NULL DEFAULT '',
|
||||
board_id TEXT NOT NULL,
|
||||
column_id TEXT NOT NULL,
|
||||
workflow_step_id TEXT NOT NULL DEFAULT '',
|
||||
title TEXT NOT NULL,
|
||||
description TEXT DEFAULT '',
|
||||
state TEXT DEFAULT 'TODO',
|
||||
@@ -198,8 +148,7 @@ func (r *Repository) initSchema() error {
|
||||
metadata TEXT DEFAULT '{}',
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL,
|
||||
FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (column_id) REFERENCES columns(id) ON DELETE CASCADE
|
||||
FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS task_repositories (
|
||||
@@ -250,10 +199,9 @@ func (r *Repository) initSchema() error {
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_board_id ON tasks(board_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_column_id ON tasks(column_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_tasks_workflow_step_id ON tasks(workflow_step_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_task_repositories_task_id ON task_repositories(task_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_task_repositories_repository_id ON task_repositories(repository_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_columns_board_id ON columns(board_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_repositories_workspace_id ON repositories(workspace_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_repository_scripts_repo_id ON repository_scripts(repository_id);
|
||||
`
|
||||
@@ -320,6 +268,9 @@ func (r *Repository) initSchema() error {
|
||||
started_at DATETIME NOT NULL,
|
||||
completed_at DATETIME,
|
||||
updated_at DATETIME NOT NULL,
|
||||
is_primary INTEGER DEFAULT 0,
|
||||
workflow_step_id TEXT DEFAULT '',
|
||||
review_status TEXT DEFAULT '',
|
||||
FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
@@ -406,6 +357,9 @@ func (r *Repository) initSchema() error {
|
||||
if err := r.ensureColumn("boards", "workspace_id", "TEXT NOT NULL DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("boards", "workflow_template_id", "TEXT DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("tasks", "workspace_id", "TEXT NOT NULL DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -418,9 +372,7 @@ func (r *Repository) initSchema() error {
|
||||
if err := r.ensureColumn("workspaces", "default_agent_profile_id", "TEXT DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("columns", "color", "TEXT NOT NULL DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := r.ensureColumn("environments", "is_system", "INTEGER NOT NULL DEFAULT 0"); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -470,6 +422,15 @@ func (r *Repository) initSchema() error {
|
||||
if err := r.ensureColumn("task_sessions", "state", "TEXT NOT NULL DEFAULT 'CREATED'"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("task_sessions", "is_primary", "INTEGER DEFAULT 0"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("task_sessions", "workflow_step_id", "TEXT DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.ensureColumn("task_sessions", "review_status", "TEXT DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := r.ensureDefaultWorkspace(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -9,21 +9,20 @@ import (
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
v1 "github.com/kandev/kandev/pkg/api/v1"
|
||||
)
|
||||
|
||||
// AddTaskToBoard adds a task to a board with placement
|
||||
func (r *Repository) AddTaskToBoard(ctx context.Context, taskID, boardID, columnID string, position int) error {
|
||||
func (r *Repository) AddTaskToBoard(ctx context.Context, taskID, boardID, workflowStepID string, position int) error {
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
UPDATE tasks SET board_id = ?, column_id = ?, position = ?, updated_at = ? WHERE id = ?
|
||||
`, boardID, columnID, position, time.Now().UTC(), taskID)
|
||||
UPDATE tasks SET board_id = ?, workflow_step_id = ?, position = ?, updated_at = ? WHERE id = ?
|
||||
`, boardID, workflowStepID, position, time.Now().UTC(), taskID)
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveTaskFromBoard removes a task from a board
|
||||
func (r *Repository) RemoveTaskFromBoard(ctx context.Context, taskID, boardID string) error {
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
UPDATE tasks SET board_id = '', column_id = '', position = 0, updated_at = ? WHERE id = ? AND board_id = ?
|
||||
UPDATE tasks SET board_id = '', workflow_step_id = '', position = 0, updated_at = ? WHERE id = ? AND board_id = ?
|
||||
`, time.Now().UTC(), taskID, boardID)
|
||||
return err
|
||||
}
|
||||
@@ -40,9 +39,9 @@ func (r *Repository) CreateBoard(ctx context.Context, board *models.Board) error
|
||||
board.UpdatedAt = now
|
||||
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO boards (id, workspace_id, name, description, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`, board.ID, board.WorkspaceID, board.Name, board.Description, board.CreatedAt, board.UpdatedAt)
|
||||
INSERT INTO boards (id, workspace_id, name, description, workflow_template_id, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`, board.ID, board.WorkspaceID, board.Name, board.Description, board.WorkflowTemplateID, board.CreatedAt, board.UpdatedAt)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -50,16 +49,24 @@ func (r *Repository) CreateBoard(ctx context.Context, board *models.Board) error
|
||||
// GetBoard retrieves a board by ID
|
||||
func (r *Repository) GetBoard(ctx context.Context, id string) (*models.Board, error) {
|
||||
board := &models.Board{}
|
||||
var workflowTemplateID sql.NullString
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, workspace_id, name, description, created_at, updated_at
|
||||
SELECT id, workspace_id, name, description, workflow_template_id, created_at, updated_at
|
||||
FROM boards WHERE id = ?
|
||||
`, id).Scan(&board.ID, &board.WorkspaceID, &board.Name, &board.Description, &board.CreatedAt, &board.UpdatedAt)
|
||||
`, id).Scan(&board.ID, &board.WorkspaceID, &board.Name, &board.Description, &workflowTemplateID, &board.CreatedAt, &board.UpdatedAt)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("board not found: %s", id)
|
||||
}
|
||||
return board, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if workflowTemplateID.Valid {
|
||||
board.WorkflowTemplateID = &workflowTemplateID.String
|
||||
}
|
||||
return board, nil
|
||||
}
|
||||
|
||||
// UpdateBoard updates an existing board
|
||||
@@ -67,8 +74,8 @@ func (r *Repository) UpdateBoard(ctx context.Context, board *models.Board) error
|
||||
board.UpdatedAt = time.Now().UTC()
|
||||
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE boards SET name = ?, description = ?, updated_at = ? WHERE id = ?
|
||||
`, board.Name, board.Description, board.UpdatedAt, board.ID)
|
||||
UPDATE boards SET name = ?, description = ?, workflow_template_id = ?, updated_at = ? WHERE id = ?
|
||||
`, board.Name, board.Description, board.WorkflowTemplateID, board.UpdatedAt, board.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,7 +104,7 @@ func (r *Repository) DeleteBoard(ctx context.Context, id string) error {
|
||||
// ListBoards returns all boards
|
||||
func (r *Repository) ListBoards(ctx context.Context, workspaceID string) ([]*models.Board, error) {
|
||||
query := `
|
||||
SELECT id, workspace_id, name, description, created_at, updated_at FROM boards
|
||||
SELECT id, workspace_id, name, description, workflow_template_id, created_at, updated_at FROM boards
|
||||
`
|
||||
var args []interface{}
|
||||
if workspaceID != "" {
|
||||
@@ -115,116 +122,15 @@ func (r *Repository) ListBoards(ctx context.Context, workspaceID string) ([]*mod
|
||||
var result []*models.Board
|
||||
for rows.Next() {
|
||||
board := &models.Board{}
|
||||
err := rows.Scan(&board.ID, &board.WorkspaceID, &board.Name, &board.Description, &board.CreatedAt, &board.UpdatedAt)
|
||||
var workflowTemplateID sql.NullString
|
||||
err := rows.Scan(&board.ID, &board.WorkspaceID, &board.Name, &board.Description, &workflowTemplateID, &board.CreatedAt, &board.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if workflowTemplateID.Valid {
|
||||
board.WorkflowTemplateID = &workflowTemplateID.String
|
||||
}
|
||||
result = append(result, board)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// Column operations
|
||||
|
||||
// CreateColumn creates a new column
|
||||
func (r *Repository) CreateColumn(ctx context.Context, column *models.Column) error {
|
||||
if column.ID == "" {
|
||||
column.ID = uuid.New().String()
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
column.CreatedAt = now
|
||||
column.UpdatedAt = now
|
||||
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO columns (id, board_id, name, position, state, color, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, column.ID, column.BoardID, column.Name, column.Position, column.State, column.Color, column.CreatedAt, column.UpdatedAt)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetColumn retrieves a column by ID
|
||||
func (r *Repository) GetColumn(ctx context.Context, id string) (*models.Column, error) {
|
||||
column := &models.Column{}
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, board_id, name, position, state, color, created_at, updated_at
|
||||
FROM columns WHERE id = ?
|
||||
`, id).Scan(&column.ID, &column.BoardID, &column.Name, &column.Position, &column.State, &column.Color, &column.CreatedAt, &column.UpdatedAt)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("column not found: %s", id)
|
||||
}
|
||||
return column, err
|
||||
}
|
||||
|
||||
// GetColumnByState retrieves a column by board ID and state
|
||||
func (r *Repository) GetColumnByState(ctx context.Context, boardID string, state v1.TaskState) (*models.Column, error) {
|
||||
column := &models.Column{}
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, board_id, name, position, state, color, created_at, updated_at
|
||||
FROM columns WHERE board_id = ? AND state = ?
|
||||
`, boardID, state).Scan(&column.ID, &column.BoardID, &column.Name, &column.Position, &column.State, &column.Color, &column.CreatedAt, &column.UpdatedAt)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("column not found for board %s with state %s", boardID, state)
|
||||
}
|
||||
return column, err
|
||||
}
|
||||
|
||||
// UpdateColumn updates an existing column
|
||||
func (r *Repository) UpdateColumn(ctx context.Context, column *models.Column) error {
|
||||
column.UpdatedAt = time.Now().UTC()
|
||||
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE columns SET name = ?, position = ?, state = ?, color = ?, updated_at = ? WHERE id = ?
|
||||
`, column.Name, column.Position, column.State, column.Color, column.UpdatedAt, column.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("column not found: %s", column.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteColumn deletes a column by ID
|
||||
func (r *Repository) DeleteColumn(ctx context.Context, id string) error {
|
||||
result, err := r.db.ExecContext(ctx, `DELETE FROM columns WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("column not found: %s", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListColumns returns all columns for a board
|
||||
func (r *Repository) ListColumns(ctx context.Context, boardID string) ([]*models.Column, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
SELECT id, board_id, name, position, state, color, created_at, updated_at
|
||||
FROM columns WHERE board_id = ? ORDER BY position
|
||||
`, boardID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var result []*models.Column
|
||||
for rows.Next() {
|
||||
column := &models.Column{}
|
||||
err := rows.Scan(&column.ID, &column.BoardID, &column.Name, &column.Position, &column.State, &column.Color, &column.CreatedAt, &column.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, column)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
commonsqlite "github.com/kandev/kandev/internal/common/sqlite"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
v1 "github.com/kandev/kandev/pkg/api/v1"
|
||||
)
|
||||
|
||||
// ensureDefaultWorkspace creates a default workspace if none exists
|
||||
@@ -63,27 +63,7 @@ func (r *Repository) ensureDefaultWorkspace() error {
|
||||
`, boardID, defaultID, "Dev", "Default development board", now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type columnSeed struct {
|
||||
name string
|
||||
position int
|
||||
state string
|
||||
color string
|
||||
}
|
||||
columns := []columnSeed{
|
||||
{name: "Todo", position: 0, state: string(v1.TaskStateTODO), color: "bg-neutral-400"},
|
||||
{name: "In Progress", position: 1, state: string(v1.TaskStateInProgress), color: "bg-blue-500"},
|
||||
{name: "Review", position: 2, state: string(v1.TaskStateReview), color: "bg-yellow-500"},
|
||||
{name: "Done", position: 3, state: string(v1.TaskStateCompleted), color: "bg-green-500"},
|
||||
}
|
||||
for _, column := range columns {
|
||||
if _, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO columns (id, board_id, name, position, state, color, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, uuid.New().String(), boardID, column.name, column.position, column.state, column.color, now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Note: Workflow steps will be created by the workflow repository after it initializes
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +141,7 @@ func (r *Repository) ensureDefaultExecutorsAndEnvironments() error {
|
||||
if _, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO executors (id, name, type, status, is_system, resumable, config, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, executor.id, executor.name, executor.execType, executor.status, boolToInt(executor.isSystem), boolToInt(executor.resumable), string(configJSON), now, now); err != nil {
|
||||
`, executor.id, executor.name, executor.execType, executor.status, commonsqlite.BoolToInt(executor.isSystem), commonsqlite.BoolToInt(executor.resumable), string(configJSON), now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -182,7 +162,7 @@ func (r *Repository) ensureDefaultExecutorsAndEnvironments() error {
|
||||
if _, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO environments (id, name, kind, is_system, worktree_root, image_tag, dockerfile, build_config, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, models.EnvironmentIDLocal, "Local", models.EnvironmentKindLocalPC, boolToInt(true), "~/kandev", "", "", "{}", now, now); err != nil {
|
||||
`, models.EnvironmentIDLocal, "Local", models.EnvironmentKindLocalPC, commonsqlite.BoolToInt(true), "~/kandev", "", "", "{}", now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
@@ -195,7 +175,7 @@ func (r *Repository) ensureDefaultExecutorsAndEnvironments() error {
|
||||
if _, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO environments (id, name, kind, is_system, worktree_root, image_tag, dockerfile, build_config, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, models.EnvironmentIDLocal, "Local", models.EnvironmentKindLocalPC, boolToInt(true), "~/kandev", "", "", "{}", now, now); err != nil {
|
||||
`, models.EnvironmentIDLocal, "Local", models.EnvironmentKindLocalPC, commonsqlite.BoolToInt(true), "~/kandev", "", "", "{}", now, now); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
commonsqlite "github.com/kandev/kandev/internal/common/sqlite"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
@@ -29,7 +30,7 @@ func (r *Repository) CreateEnvironment(ctx context.Context, environment *models.
|
||||
_, err = r.db.ExecContext(ctx, `
|
||||
INSERT INTO environments (id, name, kind, is_system, worktree_root, image_tag, dockerfile, build_config, created_at, updated_at, deleted_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, environment.ID, environment.Name, environment.Kind, boolToInt(environment.IsSystem), environment.WorktreeRoot, environment.ImageTag, environment.Dockerfile, string(buildConfigJSON), environment.CreatedAt, environment.UpdatedAt, environment.DeletedAt)
|
||||
`, environment.ID, environment.Name, environment.Kind, commonsqlite.BoolToInt(environment.IsSystem), environment.WorktreeRoot, environment.ImageTag, environment.Dockerfile, string(buildConfigJSON), environment.CreatedAt, environment.UpdatedAt, environment.DeletedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -74,7 +75,7 @@ func (r *Repository) UpdateEnvironment(ctx context.Context, environment *models.
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE environments SET name = ?, kind = ?, is_system = ?, worktree_root = ?, image_tag = ?, dockerfile = ?, build_config = ?, updated_at = ?
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
`, environment.Name, environment.Kind, boolToInt(environment.IsSystem), environment.WorktreeRoot, environment.ImageTag, environment.Dockerfile, string(buildConfigJSON), environment.UpdatedAt, environment.ID)
|
||||
`, environment.Name, environment.Kind, commonsqlite.BoolToInt(environment.IsSystem), environment.WorktreeRoot, environment.ImageTag, environment.Dockerfile, string(buildConfigJSON), environment.UpdatedAt, environment.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
commonsqlite "github.com/kandev/kandev/internal/common/sqlite"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
@@ -30,7 +31,7 @@ func (r *Repository) CreateExecutor(ctx context.Context, executor *models.Execut
|
||||
_, err = r.db.ExecContext(ctx, `
|
||||
INSERT INTO executors (id, name, type, status, is_system, resumable, config, created_at, updated_at, deleted_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, executor.ID, executor.Name, executor.Type, executor.Status, boolToInt(executor.IsSystem), boolToInt(executor.Resumable), string(configJSON), executor.CreatedAt, executor.UpdatedAt, executor.DeletedAt)
|
||||
`, executor.ID, executor.Name, executor.Type, executor.Status, commonsqlite.BoolToInt(executor.IsSystem), commonsqlite.BoolToInt(executor.Resumable), string(configJSON), executor.CreatedAt, executor.UpdatedAt, executor.DeletedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -75,7 +76,7 @@ func (r *Repository) UpdateExecutor(ctx context.Context, executor *models.Execut
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE executors SET name = ?, type = ?, status = ?, is_system = ?, resumable = ?, config = ?, updated_at = ?
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
`, executor.Name, executor.Type, executor.Status, boolToInt(executor.IsSystem), boolToInt(executor.Resumable), string(configJSON), executor.UpdatedAt, executor.ID)
|
||||
`, executor.Name, executor.Type, executor.Status, commonsqlite.BoolToInt(executor.IsSystem), commonsqlite.BoolToInt(executor.Resumable), string(configJSON), executor.UpdatedAt, executor.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -184,7 +185,7 @@ func (r *Repository) UpsertExecutorRunning(ctx context.Context, running *models.
|
||||
running.ExecutorID,
|
||||
running.Runtime,
|
||||
running.Status,
|
||||
boolToInt(running.Resumable),
|
||||
commonsqlite.BoolToInt(running.Resumable),
|
||||
running.ResumeToken,
|
||||
running.AgentExecutionID,
|
||||
running.ContainerID,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
commonsqlite "github.com/kandev/kandev/internal/common/sqlite"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
@@ -27,7 +28,7 @@ func (r *Repository) CreateRepository(ctx context.Context, repository *models.Re
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, repository.ID, repository.WorkspaceID, repository.Name, repository.SourceType, repository.LocalPath, repository.Provider,
|
||||
repository.ProviderRepoID, repository.ProviderOwner, repository.ProviderName, repository.DefaultBranch, repository.WorktreeBranchPrefix,
|
||||
boolToInt(repository.PullBeforeWorktree), repository.SetupScript, repository.CleanupScript, repository.DevScript, repository.CreatedAt, repository.UpdatedAt, repository.DeletedAt)
|
||||
commonsqlite.BoolToInt(repository.PullBeforeWorktree), repository.SetupScript, repository.CleanupScript, repository.DevScript, repository.CreatedAt, repository.UpdatedAt, repository.DeletedAt)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -62,7 +63,7 @@ func (r *Repository) UpdateRepository(ctx context.Context, repository *models.Re
|
||||
provider_name = ?, default_branch = ?, worktree_branch_prefix = ?, pull_before_worktree = ?, setup_script = ?, cleanup_script = ?, dev_script = ?, updated_at = ?
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
`, repository.Name, repository.SourceType, repository.LocalPath, repository.Provider, repository.ProviderRepoID,
|
||||
repository.ProviderOwner, repository.ProviderName, repository.DefaultBranch, repository.WorktreeBranchPrefix, boolToInt(repository.PullBeforeWorktree),
|
||||
repository.ProviderOwner, repository.ProviderName, repository.DefaultBranch, repository.WorktreeBranchPrefix, commonsqlite.BoolToInt(repository.PullBeforeWorktree),
|
||||
repository.SetupScript, repository.CleanupScript, repository.DevScript, repository.UpdatedAt, repository.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -6,10 +6,12 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
commonsqlite "github.com/kandev/kandev/internal/common/sqlite"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
@@ -197,19 +199,20 @@ func (r *Repository) CreateTaskSession(ctx context.Context, session *models.Task
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to serialize repository snapshot: %w", err)
|
||||
}
|
||||
|
||||
_, err = r.db.ExecContext(ctx, `
|
||||
INSERT INTO task_sessions (
|
||||
id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, session.ID, session.TaskID, session.AgentExecutionID, session.ContainerID, session.AgentProfileID,
|
||||
session.ExecutorID, session.EnvironmentID, session.RepositoryID, session.BaseBranch,
|
||||
string(agentProfileSnapshotJSON), string(executorSnapshotJSON), string(environmentSnapshotJSON), string(repositorySnapshotJSON),
|
||||
string(session.State), session.ErrorMessage, string(metadataJSON),
|
||||
session.StartedAt, session.CompletedAt, session.UpdatedAt)
|
||||
session.StartedAt, session.CompletedAt, session.UpdatedAt,
|
||||
commonsqlite.BoolToInt(session.IsPrimary), session.WorkflowStepID, session.ReviewStatus)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -224,12 +227,16 @@ func (r *Repository) GetTaskSession(ctx context.Context, id string) (*models.Tas
|
||||
var environmentSnapshotJSON string
|
||||
var repositorySnapshotJSON string
|
||||
var completedAt sql.NullTime
|
||||
var isPrimary int
|
||||
var workflowStepID sql.NullString
|
||||
var reviewStatus sql.NullString
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
FROM task_sessions WHERE id = ?
|
||||
`, id).Scan(
|
||||
&session.ID, &session.TaskID, &session.AgentExecutionID, &session.ContainerID, &session.AgentProfileID,
|
||||
@@ -237,6 +244,7 @@ func (r *Repository) GetTaskSession(ctx context.Context, id string) (*models.Tas
|
||||
&session.RepositoryID, &session.BaseBranch,
|
||||
&agentProfileSnapshotJSON, &executorSnapshotJSON, &environmentSnapshotJSON, &repositorySnapshotJSON,
|
||||
&state, &session.ErrorMessage, &metadataJSON, &session.StartedAt, &completedAt, &session.UpdatedAt,
|
||||
&isPrimary, &workflowStepID, &reviewStatus,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -247,6 +255,13 @@ func (r *Repository) GetTaskSession(ctx context.Context, id string) (*models.Tas
|
||||
}
|
||||
|
||||
session.State = models.TaskSessionState(state)
|
||||
session.IsPrimary = isPrimary == 1
|
||||
if workflowStepID.Valid {
|
||||
session.WorkflowStepID = &workflowStepID.String
|
||||
}
|
||||
if reviewStatus.Valid {
|
||||
session.ReviewStatus = &reviewStatus.String
|
||||
}
|
||||
if completedAt.Valid {
|
||||
session.CompletedAt = &completedAt.Time
|
||||
}
|
||||
@@ -295,12 +310,16 @@ func (r *Repository) GetTaskSessionByTaskID(ctx context.Context, taskID string)
|
||||
var environmentSnapshotJSON string
|
||||
var repositorySnapshotJSON string
|
||||
var completedAt sql.NullTime
|
||||
var isPrimary int
|
||||
var workflowStepID sql.NullString
|
||||
var reviewStatus sql.NullString
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
FROM task_sessions WHERE task_id = ? ORDER BY started_at DESC LIMIT 1
|
||||
`, taskID).Scan(
|
||||
&session.ID, &session.TaskID, &session.AgentExecutionID, &session.ContainerID, &session.AgentProfileID,
|
||||
@@ -308,6 +327,7 @@ func (r *Repository) GetTaskSessionByTaskID(ctx context.Context, taskID string)
|
||||
&session.RepositoryID, &session.BaseBranch,
|
||||
&agentProfileSnapshotJSON, &executorSnapshotJSON, &environmentSnapshotJSON, &repositorySnapshotJSON,
|
||||
&state, &session.ErrorMessage, &metadataJSON, &session.StartedAt, &completedAt, &session.UpdatedAt,
|
||||
&isPrimary, &workflowStepID, &reviewStatus,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -318,6 +338,13 @@ func (r *Repository) GetTaskSessionByTaskID(ctx context.Context, taskID string)
|
||||
}
|
||||
|
||||
session.State = models.TaskSessionState(state)
|
||||
session.IsPrimary = isPrimary == 1
|
||||
if workflowStepID.Valid {
|
||||
session.WorkflowStepID = &workflowStepID.String
|
||||
}
|
||||
if reviewStatus.Valid {
|
||||
session.ReviewStatus = &reviewStatus.String
|
||||
}
|
||||
if completedAt.Valid {
|
||||
session.CompletedAt = &completedAt.Time
|
||||
}
|
||||
@@ -366,12 +393,16 @@ func (r *Repository) GetActiveTaskSessionByTaskID(ctx context.Context, taskID st
|
||||
var environmentSnapshotJSON string
|
||||
var repositorySnapshotJSON string
|
||||
var completedAt sql.NullTime
|
||||
var isPrimary int
|
||||
var workflowStepID sql.NullString
|
||||
var reviewStatus sql.NullString
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
FROM task_sessions
|
||||
WHERE task_id = ? AND state IN ('CREATED', 'STARTING', 'RUNNING', 'WAITING_FOR_INPUT')
|
||||
ORDER BY started_at DESC LIMIT 1
|
||||
@@ -381,6 +412,7 @@ func (r *Repository) GetActiveTaskSessionByTaskID(ctx context.Context, taskID st
|
||||
&session.RepositoryID, &session.BaseBranch,
|
||||
&agentProfileSnapshotJSON, &executorSnapshotJSON, &environmentSnapshotJSON, &repositorySnapshotJSON,
|
||||
&state, &session.ErrorMessage, &metadataJSON, &session.StartedAt, &completedAt, &session.UpdatedAt,
|
||||
&isPrimary, &workflowStepID, &reviewStatus,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -391,6 +423,13 @@ func (r *Repository) GetActiveTaskSessionByTaskID(ctx context.Context, taskID st
|
||||
}
|
||||
|
||||
session.State = models.TaskSessionState(state)
|
||||
session.IsPrimary = isPrimary == 1
|
||||
if workflowStepID.Valid {
|
||||
session.WorkflowStepID = &workflowStepID.String
|
||||
}
|
||||
if reviewStatus.Valid {
|
||||
session.ReviewStatus = &reviewStatus.String
|
||||
}
|
||||
if completedAt.Valid {
|
||||
session.CompletedAt = &completedAt.Time
|
||||
}
|
||||
@@ -459,13 +498,15 @@ func (r *Repository) UpdateTaskSession(ctx context.Context, session *models.Task
|
||||
agent_execution_id = ?, container_id = ?, agent_profile_id = ?, executor_id = ?, environment_id = ?,
|
||||
repository_id = ?, base_branch = ?,
|
||||
agent_profile_snapshot = ?, executor_snapshot = ?, environment_snapshot = ?, repository_snapshot = ?,
|
||||
state = ?, error_message = ?, metadata = ?, completed_at = ?, updated_at = ?
|
||||
state = ?, error_message = ?, metadata = ?, completed_at = ?, updated_at = ?,
|
||||
is_primary = ?, workflow_step_id = ?, review_status = ?
|
||||
WHERE id = ?
|
||||
`, session.AgentExecutionID, session.ContainerID, session.AgentProfileID, session.ExecutorID, session.EnvironmentID,
|
||||
session.RepositoryID, session.BaseBranch,
|
||||
string(agentProfileSnapshotJSON), string(executorSnapshotJSON), string(environmentSnapshotJSON), string(repositorySnapshotJSON),
|
||||
string(session.State), session.ErrorMessage, string(metadataJSON), session.CompletedAt,
|
||||
session.UpdatedAt, session.ID)
|
||||
string(session.State), session.ErrorMessage, string(metadataJSON), session.CompletedAt, session.UpdatedAt,
|
||||
commonsqlite.BoolToInt(session.IsPrimary), session.WorkflowStepID, session.ReviewStatus,
|
||||
session.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -506,7 +547,8 @@ func (r *Repository) ListTaskSessions(ctx context.Context, taskID string) ([]*mo
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
FROM task_sessions WHERE task_id = ? ORDER BY started_at DESC
|
||||
`, taskID)
|
||||
if err != nil {
|
||||
@@ -534,7 +576,8 @@ func (r *Repository) ListActiveTaskSessions(ctx context.Context) ([]*models.Task
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
FROM task_sessions WHERE state IN ('CREATED', 'STARTING', 'RUNNING', 'WAITING_FOR_INPUT') ORDER BY started_at DESC
|
||||
`)
|
||||
if err != nil {
|
||||
@@ -562,7 +605,8 @@ func (r *Repository) ListActiveTaskSessionsByTaskID(ctx context.Context, taskID
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
FROM task_sessions WHERE task_id = ? AND state IN ('CREATED', 'STARTING', 'RUNNING', 'WAITING_FOR_INPUT') ORDER BY started_at DESC
|
||||
`, taskID)
|
||||
if err != nil {
|
||||
@@ -638,6 +682,9 @@ func (r *Repository) scanTaskSessions(ctx context.Context, rows *sql.Rows) ([]*m
|
||||
var environmentSnapshotJSON string
|
||||
var repositorySnapshotJSON string
|
||||
var completedAt sql.NullTime
|
||||
var isPrimary int
|
||||
var workflowStepID sql.NullString
|
||||
var reviewStatus sql.NullString
|
||||
|
||||
err := rows.Scan(
|
||||
&session.ID, &session.TaskID, &session.AgentExecutionID, &session.ContainerID, &session.AgentProfileID,
|
||||
@@ -645,12 +692,20 @@ func (r *Repository) scanTaskSessions(ctx context.Context, rows *sql.Rows) ([]*m
|
||||
&session.RepositoryID, &session.BaseBranch,
|
||||
&agentProfileSnapshotJSON, &executorSnapshotJSON, &environmentSnapshotJSON, &repositorySnapshotJSON,
|
||||
&state, &session.ErrorMessage, &metadataJSON, &session.StartedAt, &completedAt, &session.UpdatedAt,
|
||||
&isPrimary, &workflowStepID, &reviewStatus,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session.State = models.TaskSessionState(state)
|
||||
session.IsPrimary = isPrimary == 1
|
||||
if workflowStepID.Valid {
|
||||
session.WorkflowStepID = &workflowStepID.String
|
||||
}
|
||||
if reviewStatus.Valid {
|
||||
session.ReviewStatus = &reviewStatus.String
|
||||
}
|
||||
if completedAt.Valid {
|
||||
session.CompletedAt = &completedAt.Time
|
||||
}
|
||||
@@ -787,3 +842,195 @@ func (r *Repository) DeleteTaskSessionWorktreesBySession(ctx context.Context, se
|
||||
return err
|
||||
}
|
||||
|
||||
// GetPrimarySessionByTaskID retrieves the primary session for a task
|
||||
func (r *Repository) GetPrimarySessionByTaskID(ctx context.Context, taskID string) (*models.TaskSession, error) {
|
||||
session := &models.TaskSession{}
|
||||
var state string
|
||||
var metadataJSON string
|
||||
var agentProfileSnapshotJSON string
|
||||
var executorSnapshotJSON string
|
||||
var environmentSnapshotJSON string
|
||||
var repositorySnapshotJSON string
|
||||
var completedAt sql.NullTime
|
||||
var isPrimary int
|
||||
var workflowStepID sql.NullString
|
||||
var reviewStatus sql.NullString
|
||||
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, task_id, agent_execution_id, container_id, agent_profile_id, executor_id, environment_id,
|
||||
repository_id, base_branch,
|
||||
agent_profile_snapshot, executor_snapshot, environment_snapshot, repository_snapshot,
|
||||
state, error_message, metadata, started_at, completed_at, updated_at,
|
||||
is_primary, workflow_step_id, review_status
|
||||
FROM task_sessions WHERE task_id = ? AND is_primary = 1 LIMIT 1
|
||||
`, taskID).Scan(
|
||||
&session.ID, &session.TaskID, &session.AgentExecutionID, &session.ContainerID, &session.AgentProfileID,
|
||||
&session.ExecutorID, &session.EnvironmentID,
|
||||
&session.RepositoryID, &session.BaseBranch,
|
||||
&agentProfileSnapshotJSON, &executorSnapshotJSON, &environmentSnapshotJSON, &repositorySnapshotJSON,
|
||||
&state, &session.ErrorMessage, &metadataJSON, &session.StartedAt, &completedAt, &session.UpdatedAt,
|
||||
&isPrimary, &workflowStepID, &reviewStatus,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("no primary session found for task: %s", taskID)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session.State = models.TaskSessionState(state)
|
||||
session.IsPrimary = isPrimary == 1
|
||||
if workflowStepID.Valid {
|
||||
session.WorkflowStepID = &workflowStepID.String
|
||||
}
|
||||
if reviewStatus.Valid {
|
||||
session.ReviewStatus = &reviewStatus.String
|
||||
}
|
||||
if completedAt.Valid {
|
||||
session.CompletedAt = &completedAt.Time
|
||||
}
|
||||
if metadataJSON != "" && metadataJSON != "{}" {
|
||||
if err := json.Unmarshal([]byte(metadataJSON), &session.Metadata); err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize agent session metadata: %w", err)
|
||||
}
|
||||
}
|
||||
if agentProfileSnapshotJSON != "" && agentProfileSnapshotJSON != "{}" {
|
||||
if err := json.Unmarshal([]byte(agentProfileSnapshotJSON), &session.AgentProfileSnapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize agent profile snapshot: %w", err)
|
||||
}
|
||||
}
|
||||
if executorSnapshotJSON != "" && executorSnapshotJSON != "{}" {
|
||||
if err := json.Unmarshal([]byte(executorSnapshotJSON), &session.ExecutorSnapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize executor snapshot: %w", err)
|
||||
}
|
||||
}
|
||||
if environmentSnapshotJSON != "" && environmentSnapshotJSON != "{}" {
|
||||
if err := json.Unmarshal([]byte(environmentSnapshotJSON), &session.EnvironmentSnapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize environment snapshot: %w", err)
|
||||
}
|
||||
}
|
||||
if repositorySnapshotJSON != "" && repositorySnapshotJSON != "{}" {
|
||||
if err := json.Unmarshal([]byte(repositorySnapshotJSON), &session.RepositorySnapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize repository snapshot: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
worktrees, err := r.ListTaskSessionWorktrees(ctx, session.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load session worktrees: %w", err)
|
||||
}
|
||||
session.Worktrees = worktrees
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// GetPrimarySessionIDsByTaskIDs returns a map of task ID to primary session ID for the given task IDs.
|
||||
// Tasks without a primary session are not included in the result.
|
||||
func (r *Repository) GetPrimarySessionIDsByTaskIDs(ctx context.Context, taskIDs []string) (map[string]string, error) {
|
||||
if len(taskIDs) == 0 {
|
||||
return make(map[string]string), nil
|
||||
}
|
||||
|
||||
// Build placeholders for IN clause
|
||||
placeholders := make([]string, len(taskIDs))
|
||||
args := make([]interface{}, len(taskIDs))
|
||||
for i, id := range taskIDs {
|
||||
placeholders[i] = "?"
|
||||
args[i] = id
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, task_id FROM task_sessions
|
||||
WHERE task_id IN (%s) AND is_primary = 1
|
||||
`, strings.Join(placeholders, ","))
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
result := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var sessionID, taskID string
|
||||
if err := rows.Scan(&sessionID, &taskID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[taskID] = sessionID
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// SetSessionPrimary marks a session as primary and clears primary flag on other sessions for the same task
|
||||
func (r *Repository) SetSessionPrimary(ctx context.Context, sessionID string) error {
|
||||
now := time.Now().UTC()
|
||||
|
||||
// First, get the task_id for this session
|
||||
var taskID string
|
||||
err := r.db.QueryRowContext(ctx, `SELECT task_id FROM task_sessions WHERE id = ?`, sessionID).Scan(&taskID)
|
||||
if err == sql.ErrNoRows {
|
||||
return fmt.Errorf("session not found: %s", sessionID)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Clear primary flag on all sessions for this task
|
||||
_, err = r.db.ExecContext(ctx, `
|
||||
UPDATE task_sessions SET is_primary = 0, updated_at = ? WHERE task_id = ?
|
||||
`, now, taskID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set primary flag on the specified session
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE task_sessions SET is_primary = 1, updated_at = ? WHERE id = ?
|
||||
`, now, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("session not found: %s", sessionID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSessionWorkflowStep updates the workflow step for a session
|
||||
func (r *Repository) UpdateSessionWorkflowStep(ctx context.Context, sessionID string, stepID string) error {
|
||||
now := time.Now().UTC()
|
||||
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE task_sessions SET workflow_step_id = ?, updated_at = ? WHERE id = ?
|
||||
`, stepID, now, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("session not found: %s", sessionID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSessionReviewStatus updates the review status of a session
|
||||
func (r *Repository) UpdateSessionReviewStatus(ctx context.Context, sessionID string, status string) error {
|
||||
now := time.Now().UTC()
|
||||
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE task_sessions SET review_status = ?, updated_at = ? WHERE id = ?
|
||||
`, status, now, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return fmt.Errorf("session not found: %s", sessionID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -33,9 +33,9 @@ func (r *Repository) CreateTask(ctx context.Context, task *models.Task) error {
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO tasks (id, workspace_id, board_id, column_id, title, description, state, priority, position, metadata, created_at, updated_at)
|
||||
INSERT INTO tasks (id, workspace_id, board_id, workflow_step_id, title, description, state, priority, position, metadata, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, task.ID, task.WorkspaceID, task.BoardID, task.ColumnID, task.Title, task.Description, task.State, task.Priority, task.Position, string(metadata), task.CreatedAt, task.UpdatedAt)
|
||||
`, task.ID, task.WorkspaceID, task.BoardID, task.WorkflowStepID, task.Title, task.Description, task.State, task.Priority, task.Position, string(metadata), task.CreatedAt, task.UpdatedAt)
|
||||
if err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
return fmt.Errorf("failed to rollback task insert: %w", rollbackErr)
|
||||
@@ -51,9 +51,9 @@ func (r *Repository) GetTask(ctx context.Context, id string) (*models.Task, erro
|
||||
task := &models.Task{}
|
||||
var metadata string
|
||||
err := r.db.QueryRowContext(ctx, `
|
||||
SELECT id, workspace_id, board_id, column_id, title, description, state, priority, position, metadata, created_at, updated_at
|
||||
SELECT id, workspace_id, board_id, workflow_step_id, title, description, state, priority, position, metadata, created_at, updated_at
|
||||
FROM tasks WHERE id = ?
|
||||
`, id).Scan(&task.ID, &task.WorkspaceID, &task.BoardID, &task.ColumnID, &task.Title, &task.Description, &task.State, &task.Priority, &task.Position, &metadata, &task.CreatedAt, &task.UpdatedAt)
|
||||
`, id).Scan(&task.ID, &task.WorkspaceID, &task.BoardID, &task.WorkflowStepID, &task.Title, &task.Description, &task.State, &task.Priority, &task.Position, &metadata, &task.CreatedAt, &task.UpdatedAt)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("task not found: %s", id)
|
||||
@@ -76,9 +76,9 @@ func (r *Repository) UpdateTask(ctx context.Context, task *models.Task) error {
|
||||
}
|
||||
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE tasks SET workspace_id = ?, board_id = ?, column_id = ?, title = ?, description = ?, state = ?, priority = ?, position = ?, metadata = ?, updated_at = ?
|
||||
UPDATE tasks SET workspace_id = ?, board_id = ?, workflow_step_id = ?, title = ?, description = ?, state = ?, priority = ?, position = ?, metadata = ?, updated_at = ?
|
||||
WHERE id = ?
|
||||
`, task.WorkspaceID, task.BoardID, task.ColumnID, task.Title, task.Description, task.State, task.Priority, task.Position, string(metadata), task.UpdatedAt, task.ID)
|
||||
`, task.WorkspaceID, task.BoardID, task.WorkflowStepID, task.Title, task.Description, task.State, task.Priority, task.Position, string(metadata), task.UpdatedAt, task.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -107,7 +107,7 @@ func (r *Repository) DeleteTask(ctx context.Context, id string) error {
|
||||
// ListTasks returns all tasks for a board
|
||||
func (r *Repository) ListTasks(ctx context.Context, boardID string) ([]*models.Task, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
SELECT id, workspace_id, board_id, column_id, title, description, state, priority, position, metadata, created_at, updated_at
|
||||
SELECT id, workspace_id, board_id, workflow_step_id, title, description, state, priority, position, metadata, created_at, updated_at
|
||||
FROM tasks
|
||||
WHERE board_id = ?
|
||||
ORDER BY position
|
||||
@@ -120,13 +120,13 @@ func (r *Repository) ListTasks(ctx context.Context, boardID string) ([]*models.T
|
||||
return r.scanTasks(rows)
|
||||
}
|
||||
|
||||
// ListTasksByColumn returns all tasks in a column
|
||||
func (r *Repository) ListTasksByColumn(ctx context.Context, columnID string) ([]*models.Task, error) {
|
||||
// ListTasksByWorkflowStep returns all tasks in a workflow step
|
||||
func (r *Repository) ListTasksByWorkflowStep(ctx context.Context, workflowStepID string) ([]*models.Task, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
SELECT id, workspace_id, board_id, column_id, title, description, state, priority, position, metadata, created_at, updated_at
|
||||
SELECT id, workspace_id, board_id, workflow_step_id, title, description, state, priority, position, metadata, created_at, updated_at
|
||||
FROM tasks
|
||||
WHERE column_id = ? ORDER BY position
|
||||
`, columnID)
|
||||
WHERE workflow_step_id = ? ORDER BY position
|
||||
`, workflowStepID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -145,7 +145,7 @@ func (r *Repository) scanTasks(rows *sql.Rows) ([]*models.Task, error) {
|
||||
&task.ID,
|
||||
&task.WorkspaceID,
|
||||
&task.BoardID,
|
||||
&task.ColumnID,
|
||||
&task.WorkflowStepID,
|
||||
&task.Title,
|
||||
&task.Description,
|
||||
&task.State,
|
||||
|
||||
Reference in New Issue
Block a user