summaryrefslogtreecommitdiff
path: root/internal/store/chains.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/store/chains.go')
-rw-r--r--internal/store/chains.go111
1 files changed, 89 insertions, 22 deletions
diff --git a/internal/store/chains.go b/internal/store/chains.go
index f3bd0d7..f854678 100644
--- a/internal/store/chains.go
+++ b/internal/store/chains.go
@@ -84,6 +84,31 @@ func (s *Store) GetChain(id string) (*models.Chain, error) {
return &c, nil
}
+// GetChains returns every active or paused chain, oldest first. Completed
+// and abandoned chains are excluded -- they're done, not part of "current
+// work" surfaces like the Tasks tab.
+func (s *Store) GetChains() ([]models.Chain, error) {
+ rows, err := s.db.Query(`
+ SELECT id, project_id, status, created_at FROM task_chains
+ WHERE status IN ('active', 'paused')
+ ORDER BY created_at ASC
+ `)
+ if err != nil {
+ return nil, err
+ }
+ defer func() { _ = rows.Close() }()
+
+ var chains []models.Chain
+ for rows.Next() {
+ var c models.Chain
+ if err := rows.Scan(&c.ID, &c.ProjectID, &c.Status, &c.CreatedAt); err != nil {
+ return nil, err
+ }
+ chains = append(chains, c)
+ }
+ return chains, rows.Err()
+}
+
// GetChainTasks returns every task in the chain, locked and unlocked both,
// ordered by chain_position -- the full checklist view.
func (s *Store) GetChainTasks(chainID string) ([]models.Task, error) {
@@ -104,8 +129,34 @@ func (s *Store) GetChainTasks(chainID string) ([]models.Task, error) {
}
// SetChainStatus updates a chain's status (active/paused/abandoned/completed).
-// Returns ErrNativeTaskNotFound if id doesn't match any row.
+// Resuming a paused chain (status "active" from a current status of
+// "paused") catches up any advancement that was deferred while paused --
+// per the design, "the chain must be explicitly resumed for the next task
+// to unlock," i.e. resuming itself performs the unlock, not merely
+// re-arming future completions to do so. Without this, a chain paused
+// immediately after a completion (before its successor could unlock) would
+// stay stuck forever: no task is ever unlocked, so no future completion
+// could trigger advanceChain either. Returns ErrNativeTaskNotFound if id
+// doesn't match any row.
func (s *Store) SetChainStatus(id, status string) error {
+ if status == "active" {
+ current, err := s.GetChain(id)
+ if err != nil {
+ return err
+ }
+ if current.Status == "paused" {
+ maxCompleted, err := s.maxCompletedChainPosition(id)
+ if err != nil {
+ return err
+ }
+ if maxCompleted != nil {
+ if err := s.advanceChain(id, *maxCompleted); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
result, err := s.db.Exec(`UPDATE task_chains SET status = ? WHERE id = ?`, status, id)
if err != nil {
return err
@@ -113,34 +164,50 @@ func (s *Store) SetChainStatus(id, status string) error {
return checkRowsAffected(result)
}
-// advanceChain is called from CompleteNativeTask when the just-completed
-// task has a chain_id set. Per the design, a paused chain does not
-// auto-advance -- it must be explicitly resumed first. Completing the last
-// position marks the chain completed instead of advancing.
-func (s *Store) advanceChain(chainID string, completedPosition int) error {
- chain, err := s.GetChain(chainID)
+// maxCompletedChainPosition returns the highest chain_position among
+// completed tasks in the chain, or nil if none are completed yet.
+func (s *Store) maxCompletedChainPosition(chainID string) (*int, error) {
+ var pos sql.NullInt64
+ err := s.db.QueryRow(`
+ SELECT MAX(chain_position) FROM native_tasks WHERE chain_id = ? AND completed = 1
+ `, chainID).Scan(&pos)
if err != nil {
- return err
+ return nil, err
}
- if chain.Status == "paused" {
- return nil
+ if !pos.Valid {
+ return nil, nil
}
+ p := int(pos.Int64)
+ return &p, nil
+}
- now := config.Now()
- result, err := s.db.Exec(`
- UPDATE native_tasks SET chain_unlocked = 1, due_date = ?, updated_at = ?
- WHERE chain_id = ? AND chain_position = ?
- `, now, now, chainID, completedPosition+1)
- if err != nil {
- return err
+// advanceChain is called from CompleteNativeTask (when the just-completed
+// task has a chain_id set and the chain isn't paused) and from
+// SetChainStatus's resume catch-up. Idempotent: if the successor position
+// is already unlocked, it's left untouched (so a resume catch-up run
+// against a chain that was never actually stuck is a no-op, not a
+// due_date-resetting re-unlock). Completing/catching-up-to the last
+// position marks the chain completed instead of unlocking a successor.
+func (s *Store) advanceChain(chainID string, completedPosition int) error {
+ var successorID string
+ var successorUnlocked bool
+ err := s.db.QueryRow(`
+ SELECT id, chain_unlocked FROM native_tasks WHERE chain_id = ? AND chain_position = ?
+ `, chainID, completedPosition+1).Scan(&successorID, &successorUnlocked)
+ if err == sql.ErrNoRows {
+ // No next position -- the completed task was the last in the chain.
+ return s.SetChainStatus(chainID, "completed")
}
- affected, err := result.RowsAffected()
if err != nil {
return err
}
- if affected == 0 {
- // No next position -- the completed task was the last in the chain.
- return s.SetChainStatus(chainID, "completed")
+ if successorUnlocked {
+ return nil
}
- return nil
+
+ now := config.Now()
+ _, err = s.db.Exec(`
+ UPDATE native_tasks SET chain_unlocked = 1, due_date = ?, updated_at = ? WHERE id = ?
+ `, now, now, successorID)
+ return err
}