Run discards on the state-driven coordinator

This commit is contained in:
Ian Gulliver
2026-10-21 11:53:37 -07:00
parent f3a0e28763
commit 1b5f42a01a
8 changed files with 141 additions and 191 deletions
+4 -11
View File
@@ -42,9 +42,9 @@ func (s *Store) GetDiscard(ctx context.Context, taskID string) (Discard, error)
return d, err
}
func (s *Store) RequestDiscard(ctx context.Context, taskID, actor string) (DiscardState, error) {
state := DiscardNone
err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
func (s *Store) RequestDiscard(ctx context.Context, taskID, actor string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
state := DiscardNone
projectID := ""
status := ""
mergingBy := ""
@@ -75,7 +75,6 @@ func (s *Store) RequestDiscard(ctx context.Context, taskID, actor string) (Disca
case subtasks > 0:
return ErrHasSubtasks
}
state = DiscardHeld
_, err = tx.ExecContext(ctx, `UPDATE tasks SET discard_state = ?, discarding_by = ?, discard_session = '', merge_queued_by = '', merge_queued_at = '', merge_after_review_by = '', merge_after_review_id = '', merge_after_review_target = '' WHERE id = ?`,
string(DiscardHeld), actor, taskID)
if err != nil {
@@ -83,7 +82,6 @@ func (s *Store) RequestDiscard(ctx context.Context, taskID, actor string) (Disca
}
return emit(api.Event{Kind: "task.discard_started", ProjectId: &projectID, TaskId: &taskID, Actor: actor, Payload: map[string]any{"id": taskID}})
})
return state, err
}
func (s *Store) StartDiscardTurn(ctx context.Context, taskID, sessionID, actor string) error {
@@ -176,8 +174,7 @@ func (s *Store) EndTurn(ctx context.Context, sessionID, actor string, payload ma
case state == DiscardHeld:
end.Discard = DiscardHeld
case state == DiscardRunning && discardSession == sessionID:
end.Discard = DiscardClosing
_, err = tx.ExecContext(ctx, `UPDATE tasks SET discard_state = ? WHERE id = ?`, string(DiscardClosing), taskID)
end.Discard = DiscardRunning
case state == DiscardNone && finalizeMerge && lead:
err = tx.QueryRowContext(ctx, `UPDATE tasks SET finalizing = merging_by, merging_by = '' WHERE id = ? AND merging_by != '' AND merge_after_turn = 0 RETURNING finalizing`, taskID).Scan(&end.FinalizeBy)
if errors.Is(err, sql.ErrNoRows) {
@@ -195,7 +192,3 @@ func (s *Store) CloseDiscard(ctx context.Context, taskID string) error {
return err
})
}
func (s *Store) PendingDiscards(ctx context.Context) ([]api.Task, error) {
return s.queryTasks(ctx, `SELECT `+taskColumns+` FROM tasks WHERE status = ? AND discard_state != ''`, string(api.TaskStatusActive))
}