fd79d6170f
* feat(tasks): multi-branch tasks — N (repo, branch) pairs per task Extends multi-repo into multi-branch so a single task can hold multiple worktrees on the same repo (one per branch). Sibling worktrees layout at <task>/<repo>/ + <task>/<repo>-<slug>/, dedicated PRWatch per branch, live agentctl tracker rescan + frontend store updates so the UI splits PRs / files / commits per branch without a session restart. MCP add_branch_to_task_kandev tool is shielded to the worktree executor. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(web): unify multi-branch repo label as <repo> · <branch> across panels PR Changes and Commits panels rendered repo labels differently for multi-branch tasks — Commits showed "kandev · feature/x" while PR Changes leaked the raw "kandev-feature-x" agentctl tracker tag. Reformat tracker-tagged repo names via formatRepoLabel using the longest known-repo prefix so kandev-cli-* doesn't collapse to kandev · cli-*. * fix(tasks): address multi-branch PR review — data races, slug collisions, MCP error codes Backend - agentctl process.Manager: add repoTrackersMu RWMutex guarding repoTrackers + workspaceTracker. Reads snapshot under RLock; RescanRepositories builds trackers first, takes Lock for swap/append. Validates newWorkDir via os.Stat before publishing cfg.WorkDir. - worktree.Manager: reject invalid explicit BranchSlug up-front; cache key uses sanitized wt.BranchSlug so read/write paths are symmetric. - worktree.store: narrow isDuplicateColumnError to "duplicate column name". - task service AddBranchToTask: slug-collision guard rejects feat/a vs feat-a. - orchestrator resolvePushRepo: two-pass exact-name match before subdir prefix fallback so "backend" doesn't swallow pushes for "backend-admin". - executor buildRepoSpecs: duplicate repos always get a non-empty BranchSlug (falls back to branch-N when both branch fields sanitize to empty). - MCP handler: classifyAddBranchError maps service errors to NOT_FOUND / CONFLICT / VALIDATION_ERROR; add ErrorCodeConflict constant. - MCP server: extract registerAddBranchToTaskTool, register in task mode only (external mode contract: config tools + create_task_kandev). - workspace_rescan handler: reject malformed JSON with 400 instead of silently rescanning the current root. - branch_materializer: split MaterializeBranch into prepare + finalize helpers to satisfy the 80-line cap. Frontend - existingPrUrl returns undefined when a task has >1 PR so per-repo View PR buttons take over (no silent linking to a sibling). - formatRepoLabel checks exact known-repo match before prefix splitting so "kandev-cli" isn't mislabeled as "kandev · cli". - Repo chip key now includes base_branch so siblings differing only on base render as distinct chips. Docs - multi-branch spec uniqueness wording aligned with the canonical 4-column key (task_id, repository_id, base_branch, checkout_branch). - worktree.CreateRequest.BranchSlug doc describes the sibling layout (<repo>-<slug>) instead of the obsolete nested form. * fix(tasks): scope slug-collision check to repo only + use errors.Is for ErrTaskNotFound - service_branches: drop base_branch from the slug-collision check. Worktree path is (repo, slug); two rows on the same repo cut from different bases still collide on disk. - service_branches: prepareBranchAdd wraps taskrepo.ErrTaskNotFound via fmt.Errorf with %w so classifiers don't have to substring-match the formatted task UUID. - mcp handlers: classifyAddBranchError uses errors.Is(err, ErrTaskNotFound). * fix(agentctl): sanitize rescan work_dir before os.Stat to clear CodeQL path-injection CodeQL flagged the rescan endpoint as a path-injection sink (the HTTP-supplied work_dir flowed straight into os.Stat and downstream scanRepositorySubdirs). Add validateRescanPath which: - requires the input to be a clean absolute path with no ".." segments - rejects anything that isn't equal to or an ancestor of the current cfg.WorkDir — the only legitimate use case is the branch materializer promoting <task>/<repo>/ to <task>/ Defense in depth: agentctl binds to localhost, but any colocated process could otherwise direct it at /etc or the user's home by POSTing /api/v1/workspace/rescan. * fix(agentctl): tighten validateRescanPath, close TOCTOU on existingTrackers, add tests - Drop the strings.Contains(clean, "..") guard. filepath.Clean already resolves traversal; the substring check rejects legitimate names like /home/u/..hidden without adding defense (the ancestor check via filepath.Rel is the real boundary). - Drop the rel == "." branch in the ancestor check — early return for clean == currentClean already covers identity. - Read existingTrackers inside the write-lock section (not under RLock then re-Lock) so two concurrent rescans can't both see 0 trackers and double-bootstrap the multi-repo set. - Add table-driven TestValidateRescanPath covering rejection paths (relative, descendant, sibling, traversal-cleaned, empty input), legitimate ancestor promotion, exact match, no-anchor first-launch, and a /home/u/..hidden dotted-segment case. * fix(agentctl): break taint flow into os.Stat — derive rescan path from cfg, not HTTP body CodeQL's go/path-injection trace still flagged manager_rescan.go after the prior sanitizer because the user-supplied newWorkDir flowed (after validation) into os.Stat and then into cfg.WorkDir → tracker paths → workspace_monitor os.Stat sinks. Replace validateRescanPath with resolveRescanPath: it returns a path DERIVED from currentWorkDir (no-op = currentClean, promotion = filepath.Dir(currentClean)) and uses newPath only for comparison, never as a source for the returned string. os.Stat now receives a path whose provenance is trusted manager config. Functionally the legitimate caller (branch materializer promoting <task>/<repo>/ to <task>/) is unchanged. * test(agentctl): make TestResolveRescanPath windows-portable The Linux Backend Tests job passed but the Backend (windows) job rejected every "absolute" case because filepath.IsAbs("/task/repo") is false on Windows (no volume letter). Build inputs via filepath.Join against absRoot() (filepath.Abs of "/" / "\\") so the same table covers POSIX (`/...`) and Windows (`C:\...`) without per-OS branches. Keep the traversal-after-Clean case POSIX-only since filepath.Clean is the shared primitive and that input form needs a volume to be considered absolute on Windows anyway. * fix(agentctl): serialize RescanRepositories + re-check trackers under write lock Two concurrent RescanRepositories calls could both observe an empty tracker set (between the snapshot read and the bootstrap branch) and both enter transitionToMultiRepoMode, leaking a duplicate bare-root tracker and duplicate per-repo trackers. Same race in appendNewRepoTrackers would double-append the same new child across both callers. Fix: - Add rescanMu sync.Mutex on Manager. Acquired for the full body of RescanRepositories so the snapshot → branch → mutate sequence is serialized. repoTrackersMu still guards individual field reads from unrelated goroutines (subscribe/unsubscribe/poll-mode). - appendNewRepoTrackers re-checks membership inside the write-lock before appending and drops already-present trackers (Stop + detach outside the lock). Defense in depth. - Test: add `dotted segment is legal as promotion` case so the ..hidden-segment promotion path stays exercised. --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
583 lines
16 KiB
Go
583 lines
16 KiB
Go
package worktree
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// SQLiteStore implements Store interface using SQLite.
|
|
type SQLiteStore struct {
|
|
db *sqlx.DB // writer
|
|
ro *sqlx.DB // reader
|
|
}
|
|
|
|
// NewSQLiteStore creates a new SQLite-backed worktree store.
|
|
// It uses the provided writer and reader connections and ensures the task_session_worktrees table exists.
|
|
func NewSQLiteStore(writer, reader *sqlx.DB) (*SQLiteStore, error) {
|
|
store := &SQLiteStore{db: writer, ro: reader}
|
|
if err := store.initSchema(); err != nil {
|
|
return nil, fmt.Errorf("failed to initialize worktree schema: %w", err)
|
|
}
|
|
return store, nil
|
|
}
|
|
|
|
// initSchema creates the task_session_worktrees table if it doesn't exist.
|
|
// `branch_slug` is required for multi-branch tasks (same repo, multiple
|
|
// branches): without it, reuse lookups by (session_id, repository_id)
|
|
// silently collapse two distinct worktrees into one, which then trickles
|
|
// down to a single on-disk directory shared between rows.
|
|
func (s *SQLiteStore) initSchema() error {
|
|
schema := `
|
|
CREATE TABLE IF NOT EXISTS task_session_worktrees (
|
|
id TEXT PRIMARY KEY,
|
|
session_id TEXT NOT NULL,
|
|
worktree_id TEXT NOT NULL,
|
|
repository_id TEXT NOT NULL,
|
|
branch_slug TEXT NOT NULL DEFAULT '',
|
|
position INTEGER DEFAULT 0,
|
|
worktree_path TEXT DEFAULT '',
|
|
worktree_branch TEXT DEFAULT '',
|
|
status TEXT NOT NULL DEFAULT 'active',
|
|
created_at TIMESTAMP NOT NULL,
|
|
updated_at TIMESTAMP NOT NULL,
|
|
merged_at TIMESTAMP,
|
|
deleted_at TIMESTAMP,
|
|
FOREIGN KEY (session_id) REFERENCES task_sessions(id) ON DELETE CASCADE,
|
|
UNIQUE(session_id, worktree_id)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_task_session_worktrees_session_id ON task_session_worktrees(session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_task_session_worktrees_worktree_id ON task_session_worktrees(worktree_id);
|
|
CREATE INDEX IF NOT EXISTS idx_task_session_worktrees_repository_id ON task_session_worktrees(repository_id);
|
|
CREATE INDEX IF NOT EXISTS idx_task_session_worktrees_status ON task_session_worktrees(status);
|
|
`
|
|
|
|
if _, err := s.db.Exec(schema); err != nil {
|
|
return err
|
|
}
|
|
// Idempotent ALTER for upgrades. Pre-existing rows get branch_slug='' which
|
|
// matches the legacy single-branch identity exactly.
|
|
if _, err := s.db.Exec(`ALTER TABLE task_session_worktrees ADD COLUMN branch_slug TEXT NOT NULL DEFAULT ''`); err != nil {
|
|
// SQLite returns "duplicate column name" when the column already exists;
|
|
// treat that as success so the migration is replay-safe.
|
|
if !isDuplicateColumnError(err) {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func isDuplicateColumnError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
// SQLite emits "duplicate column name: <col>" for ALTER TABLE ADD COLUMN
|
|
// against an existing column. The broader "already exists" substring also
|
|
// matches unrelated DDL errors (e.g. "table X already exists"), which
|
|
// would silently swallow real schema failures — keep the match narrow.
|
|
return strings.Contains(err.Error(), "duplicate column name")
|
|
}
|
|
|
|
// CreateWorktree persists a new worktree record.
|
|
func (s *SQLiteStore) CreateWorktree(ctx context.Context, wt *Worktree) error {
|
|
if wt.ID == "" {
|
|
wt.ID = uuid.New().String()
|
|
}
|
|
if wt.SessionID == "" {
|
|
return fmt.Errorf("session ID is required to persist worktree")
|
|
}
|
|
if wt.Status == "" {
|
|
wt.Status = StatusActive
|
|
}
|
|
now := time.Now().UTC()
|
|
if wt.CreatedAt.IsZero() {
|
|
wt.CreatedAt = now
|
|
}
|
|
if wt.UpdatedAt.IsZero() {
|
|
wt.UpdatedAt = now
|
|
}
|
|
|
|
_, err := s.db.ExecContext(ctx, s.db.Rebind(`
|
|
INSERT INTO task_session_worktrees (
|
|
id, session_id, worktree_id, repository_id, branch_slug, position,
|
|
worktree_path, worktree_branch, status,
|
|
created_at, updated_at, merged_at, deleted_at
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(session_id, worktree_id) DO UPDATE SET
|
|
repository_id = excluded.repository_id,
|
|
branch_slug = excluded.branch_slug,
|
|
worktree_path = excluded.worktree_path,
|
|
worktree_branch = excluded.worktree_branch,
|
|
status = excluded.status,
|
|
updated_at = excluded.updated_at,
|
|
merged_at = excluded.merged_at,
|
|
deleted_at = excluded.deleted_at
|
|
`), uuid.New().String(), wt.SessionID, wt.ID, wt.RepositoryID, wt.BranchSlug, 0,
|
|
wt.Path, wt.Branch, wt.Status,
|
|
wt.CreatedAt, wt.UpdatedAt, wt.MergedAt, wt.DeletedAt)
|
|
|
|
return err
|
|
}
|
|
|
|
// GetWorktreeByID retrieves a worktree by its unique ID.
|
|
func (s *SQLiteStore) GetWorktreeByID(ctx context.Context, id string) (*Worktree, error) {
|
|
wt := &Worktree{}
|
|
var mergedAt, deletedAt sql.NullTime
|
|
var repositoryPath, baseBranch sql.NullString
|
|
|
|
err := s.ro.QueryRowContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
LEFT JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE tsw.worktree_id = ?
|
|
`), id).Scan(
|
|
&wt.ID,
|
|
&wt.SessionID,
|
|
&wt.TaskID,
|
|
&wt.RepositoryID,
|
|
&repositoryPath,
|
|
&wt.Path,
|
|
&wt.Branch,
|
|
&wt.BranchSlug,
|
|
&baseBranch,
|
|
&wt.Status,
|
|
&wt.CreatedAt,
|
|
&wt.UpdatedAt,
|
|
&mergedAt,
|
|
&deletedAt,
|
|
)
|
|
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil // Not found, return nil without error
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if repositoryPath.Valid {
|
|
wt.RepositoryPath = repositoryPath.String
|
|
}
|
|
if baseBranch.Valid {
|
|
wt.BaseBranch = baseBranch.String
|
|
}
|
|
if mergedAt.Valid {
|
|
wt.MergedAt = &mergedAt.Time
|
|
}
|
|
if deletedAt.Valid {
|
|
wt.DeletedAt = &deletedAt.Time
|
|
}
|
|
|
|
return wt, nil
|
|
}
|
|
|
|
func scanWorktreeRow(row *sql.Row) (*Worktree, error) {
|
|
wt := &Worktree{}
|
|
var mergedAt, deletedAt sql.NullTime
|
|
var repositoryPath, baseBranch sql.NullString
|
|
|
|
err := row.Scan(
|
|
&wt.ID,
|
|
&wt.SessionID,
|
|
&wt.TaskID,
|
|
&wt.RepositoryID,
|
|
&repositoryPath,
|
|
&wt.Path,
|
|
&wt.Branch,
|
|
&wt.BranchSlug,
|
|
&baseBranch,
|
|
&wt.Status,
|
|
&wt.CreatedAt,
|
|
&wt.UpdatedAt,
|
|
&mergedAt,
|
|
&deletedAt,
|
|
)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if repositoryPath.Valid {
|
|
wt.RepositoryPath = repositoryPath.String
|
|
}
|
|
if baseBranch.Valid {
|
|
wt.BaseBranch = baseBranch.String
|
|
}
|
|
if mergedAt.Valid {
|
|
wt.MergedAt = &mergedAt.Time
|
|
}
|
|
if deletedAt.Valid {
|
|
wt.DeletedAt = &deletedAt.Time
|
|
}
|
|
|
|
return wt, nil
|
|
}
|
|
|
|
// GetWorktreeBySessionID retrieves the worktree by session ID.
|
|
func (s *SQLiteStore) GetWorktreeBySessionID(ctx context.Context, sessionID string) (*Worktree, error) {
|
|
row := s.ro.QueryRowContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
INNER JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE tsw.session_id = ? AND tsw.status = ?
|
|
`), sessionID, StatusActive)
|
|
return scanWorktreeRow(row)
|
|
}
|
|
|
|
// GetWorktreeByTaskID retrieves the most recent active worktree by task ID.
|
|
// Since multiple worktrees can exist per task, this returns the most recently created active one.
|
|
func (s *SQLiteStore) GetWorktreeByTaskID(ctx context.Context, taskID string) (*Worktree, error) {
|
|
row := s.ro.QueryRowContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
INNER JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE s.task_id = ? AND tsw.status = ?
|
|
ORDER BY tsw.created_at DESC LIMIT 1
|
|
`), taskID, StatusActive)
|
|
return scanWorktreeRow(row)
|
|
}
|
|
|
|
// GetWorktreesByTaskID retrieves all worktrees for a task.
|
|
func (s *SQLiteStore) GetWorktreesByTaskID(ctx context.Context, taskID string) ([]*Worktree, error) {
|
|
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
INNER JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE s.task_id = ? ORDER BY tsw.created_at DESC
|
|
`), taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
return s.scanWorktrees(rows)
|
|
}
|
|
|
|
// GetWorktreesByRepositoryID retrieves all worktrees for a repository.
|
|
func (s *SQLiteStore) GetWorktreesByRepositoryID(ctx context.Context, repoID string) ([]*Worktree, error) {
|
|
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
LEFT JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE tsw.repository_id = ?
|
|
`), repoID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
return s.scanWorktrees(rows)
|
|
}
|
|
|
|
// UpdateWorktree updates an existing worktree record.
|
|
func (s *SQLiteStore) UpdateWorktree(ctx context.Context, wt *Worktree) error {
|
|
wt.UpdatedAt = time.Now().UTC()
|
|
|
|
query := `
|
|
UPDATE task_session_worktrees SET
|
|
repository_id = ?, worktree_path = ?, worktree_branch = ?,
|
|
status = ?, updated_at = ?, merged_at = ?, deleted_at = ?
|
|
WHERE worktree_id = ?
|
|
`
|
|
args := []interface{}{
|
|
wt.RepositoryID,
|
|
wt.Path,
|
|
wt.Branch,
|
|
wt.Status,
|
|
wt.UpdatedAt,
|
|
wt.MergedAt,
|
|
wt.DeletedAt,
|
|
wt.ID,
|
|
}
|
|
if wt.SessionID != "" {
|
|
query += " AND session_id = ?"
|
|
args = append(args, wt.SessionID)
|
|
}
|
|
|
|
result, err := s.db.ExecContext(ctx, s.db.Rebind(query), args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rows, _ := result.RowsAffected()
|
|
if rows == 0 {
|
|
return fmt.Errorf("worktree not found: %s", wt.ID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteWorktree removes a worktree record.
|
|
func (s *SQLiteStore) DeleteWorktree(ctx context.Context, id string) error {
|
|
result, err := s.db.ExecContext(ctx, s.db.Rebind(`DELETE FROM task_session_worktrees WHERE worktree_id = ?`), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rows, _ := result.RowsAffected()
|
|
if rows == 0 {
|
|
return fmt.Errorf("worktree not found: %s", id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListActiveWorktrees returns all worktrees with status 'active'.
|
|
func (s *SQLiteStore) ListActiveWorktrees(ctx context.Context) ([]*Worktree, error) {
|
|
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
LEFT JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE tsw.status = ?
|
|
`), StatusActive)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
return s.scanWorktrees(rows)
|
|
}
|
|
|
|
// ListActiveWorktreePaths returns the worktree_path of every active,
|
|
// non-deleted task_session_worktrees row that has a non-empty path. The
|
|
// office GC uses this set as the authoritative inventory of live worktrees;
|
|
// any directory under the worktree base that does not appear here (and is
|
|
// older than the GC grace period) is considered orphaned.
|
|
func (s *SQLiteStore) ListActiveWorktreePaths(ctx context.Context) ([]string, error) {
|
|
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
|
SELECT worktree_path
|
|
FROM task_session_worktrees
|
|
WHERE status = ?
|
|
AND deleted_at IS NULL
|
|
AND worktree_path <> ''
|
|
`), StatusActive)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
var paths []string
|
|
for rows.Next() {
|
|
var p string
|
|
if err := rows.Scan(&p); err != nil {
|
|
return nil, err
|
|
}
|
|
paths = append(paths, p)
|
|
}
|
|
return paths, rows.Err()
|
|
}
|
|
|
|
// scanWorktrees is a helper to scan multiple worktree rows.
|
|
func (s *SQLiteStore) scanWorktrees(rows *sql.Rows) ([]*Worktree, error) {
|
|
var result []*Worktree
|
|
for rows.Next() {
|
|
wt := &Worktree{}
|
|
var mergedAt, deletedAt sql.NullTime
|
|
var repositoryPath, baseBranch sql.NullString
|
|
|
|
err := rows.Scan(
|
|
&wt.ID,
|
|
&wt.SessionID,
|
|
&wt.TaskID,
|
|
&wt.RepositoryID,
|
|
&repositoryPath,
|
|
&wt.Path,
|
|
&wt.Branch,
|
|
&wt.BranchSlug,
|
|
&baseBranch,
|
|
&wt.Status,
|
|
&wt.CreatedAt,
|
|
&wt.UpdatedAt,
|
|
&mergedAt,
|
|
&deletedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if repositoryPath.Valid {
|
|
wt.RepositoryPath = repositoryPath.String
|
|
}
|
|
if baseBranch.Valid {
|
|
wt.BaseBranch = baseBranch.String
|
|
}
|
|
if mergedAt.Valid {
|
|
wt.MergedAt = &mergedAt.Time
|
|
}
|
|
if deletedAt.Valid {
|
|
wt.DeletedAt = &deletedAt.Time
|
|
}
|
|
|
|
result = append(result, wt)
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
// GetWorktreesBySessionID returns all active worktrees for the session.
|
|
// Implements MultiRepoStore.
|
|
func (s *SQLiteStore) GetWorktreesBySessionID(ctx context.Context, sessionID string) ([]*Worktree, error) {
|
|
rows, err := s.ro.QueryContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
INNER JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE tsw.session_id = ? AND tsw.status = ?
|
|
ORDER BY tsw.position ASC, tsw.created_at ASC
|
|
`), sessionID, StatusActive)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
return s.scanWorktrees(rows)
|
|
}
|
|
|
|
// GetWorktreeBySessionAndRepository returns the active worktree for the
|
|
// given (session, repository, branchSlug) triple, or nil if none exists.
|
|
// branchSlug scopes the lookup so multi-branch tasks (same repo, multiple
|
|
// branches) don't collapse — an empty slug matches the legacy
|
|
// single-branch persistence shape, so single-branch callers remain
|
|
// unchanged. Implements MultiRepoStore.
|
|
func (s *SQLiteStore) GetWorktreeBySessionAndRepository(ctx context.Context, sessionID, repositoryID, branchSlug string) (*Worktree, error) {
|
|
row := s.ro.QueryRowContext(ctx, s.ro.Rebind(`
|
|
SELECT
|
|
tsw.worktree_id,
|
|
tsw.session_id,
|
|
s.task_id,
|
|
tsw.repository_id,
|
|
r.local_path,
|
|
tsw.worktree_path,
|
|
tsw.worktree_branch,
|
|
COALESCE(tsw.branch_slug, ''),
|
|
s.base_branch,
|
|
tsw.status,
|
|
tsw.created_at,
|
|
tsw.updated_at,
|
|
tsw.merged_at,
|
|
tsw.deleted_at
|
|
FROM task_session_worktrees tsw
|
|
INNER JOIN task_sessions s ON tsw.session_id = s.id
|
|
LEFT JOIN repositories r ON tsw.repository_id = r.id
|
|
WHERE tsw.session_id = ? AND tsw.repository_id = ?
|
|
AND COALESCE(tsw.branch_slug, '') = ?
|
|
AND tsw.status = ?
|
|
LIMIT 1
|
|
`), sessionID, repositoryID, branchSlug, StatusActive)
|
|
return scanWorktreeRow(row)
|
|
}
|
|
|
|
// Ensure SQLiteStore implements both Store and MultiRepoStore.
|
|
var (
|
|
_ Store = (*SQLiteStore)(nil)
|
|
_ MultiRepoStore = (*SQLiteStore)(nil)
|
|
)
|