Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
repo locking updates, git error handling updates, set timeouts for db lock wait and statements
Dane Schneidera754a3db78d5
diff --git a/app/server/db/db.go b/app/server/db/db.goindex 441863fe..0bde0f4c 100644--- a/app/server/db/db.go+++ b/app/server/db/db.go@@ -43,12 +43,26 @@ func Connect() error {log.Println("connected to database")- _, err = Conn.Exec("SET TIMEZONE='UTC';")+ if os.Getenv("GOENV") == "production" {+ Conn.SetMaxOpenConns(30) // Allow up to 30 concurrent connections per task+ Conn.SetMaxIdleConns(30) // Keep up to 30 idle connections for reuse+ }+ _, err = Conn.Exec("SET TIMEZONE='UTC';")if err != nil {return fmt.Errorf("error setting timezone: %v", err)}+ _, err = Conn.Exec("SET lock_timeout = '10s';")+ if err != nil {+ return fmt.Errorf("error setting lock timeout: %v", err)+ }++ _, err = Conn.Exec("SET statement_timeout = '10s';")+ if err != nil {+ return fmt.Errorf("error setting statement timeout: %v", err)+ }+return nil}diff --git a/app/server/db/git.go b/app/server/db/git.goindex 9feb3dc9..cc7084c2 100644--- a/app/server/db/git.go+++ b/app/server/db/git.go@@ -14,7 +14,7 @@ import ("github.com/fatih/color")-const maxGitRetries = 5+const maxGitRetries = 6const initialGitRetryInterval = 100 * time.Millisecondfunc init() {@@ -51,14 +51,14 @@ func initGitRepo(dir string) error {func GitAddAndCommit(orgId, planId, branch, message string) error {dir := getPlanDir(orgId, planId)- err := retryGitWriteOperationIfIndexFileErr(func() error {+ err := gitWriteOperation(func() error {return gitAdd(dir, ".")})if err != nil {return fmt.Errorf("error adding files to git repository for dir: %s, err: %v", dir, err)}- err = retryGitWriteOperationIfIndexFileErr(func() error {+ err = gitWriteOperation(func() error {return gitCommit(dir, message)})if err != nil {@@ -71,7 +71,7 @@ func GitAddAndCommit(orgId, planId, branch, message string) error {func GitRewindToSha(orgId, planId, branch, sha string) error {dir := getPlanDir(orgId, planId)- err := retryGitWriteOperationIfIndexFileErr(func() error {+ err := gitWriteOperation(func() error {return gitRewindToSha(dir, sha)})if err != nil {@@ -127,7 +127,7 @@ func GitListBranches(orgId, planId string) ([]string, error) {func GitCreateBranch(orgId, planId, branch, newBranch string) error {dir := getPlanDir(orgId, planId)- err := retryGitWriteOperationIfIndexFileErr(func() error {+ err := gitWriteOperation(func() error {res, err := exec.Command("git", "-C", dir, "checkout", "-b", newBranch).CombinedOutput()if err != nil {return fmt.Errorf("error creating git branch for dir: %s, err: %v, output: %s", dir, err, string(res))@@ -141,7 +141,7 @@ func GitCreateBranch(orgId, planId, branch, newBranch string) error {func GitDeleteBranch(orgId, planId, branchName string) error {dir := getPlanDir(orgId, planId)- err := retryGitWriteOperationIfIndexFileErr(func() error {+ err := gitWriteOperation(func() error {res, err := exec.Command("git", "-C", dir, "branch", "-D", branchName).CombinedOutput()if err != nil {return fmt.Errorf("error deleting git branch for dir: %s, err: %v, output: %s", dir, err, string(res))@@ -155,7 +155,7 @@ func GitDeleteBranch(orgId, planId, branchName string) error {func GitClearUncommittedChanges(orgId, planId string) error {dir := getPlanDir(orgId, planId)- err := retryGitWriteOperationIfIndexFileErr(func() error {+ err := gitWriteOperation(func() error {// Reset staged changesres, err := exec.Command("git", "-C", dir, "reset", "--hard").CombinedOutput()if err != nil {@@ -168,7 +168,7 @@ func GitClearUncommittedChanges(orgId, planId string) error {return err}- err = retryGitWriteOperationIfIndexFileErr(func() error {+ err = gitWriteOperation(func() error {// Clean untracked filesres, err := exec.Command("git", "-C", dir, "clean", "-d", "-f").CombinedOutput()if err != nil {@@ -205,7 +205,7 @@ func gitCheckoutBranch(repoDir, branch string) error {return nil}- err = retryGitWriteOperationIfIndexFileErr(func() error {+ err = gitWriteOperation(func() error {log.Println("checking out branch:", branch)res, err := exec.Command("git", "-C", repoDir, "checkout", branch).CombinedOutput()if err != nil {@@ -364,7 +364,7 @@ func setGitConfig(repoDir, key, value string) error {return nil}-func retryGitWriteOperationIfIndexFileErr(operation func() error) error {+func gitWriteOperation(operation func() error) error {var err errorretryInterval := initialGitRetryInterval@@ -376,22 +376,16 @@ func retryGitWriteOperationIfIndexFileErr(operation func() error) error {log.Printf("Retry attempt %d failed. Error: %v", attempt+1, err)- if exitError, ok := err.(*exec.ExitError); ok {- errorOutput := string(exitError.Stderr)- log.Printf("git operation error output: %s", errorOutput)+ isIndexLockError := strings.Contains(err.Error(), "new_index file") ||+ strings.Contains(err.Error(), "index.lock")- if exitError.ExitCode() == 128 && (strings.Contains(string(exitError.Stderr), "new_index file") ||- strings.Contains(string(exitError.Stderr), "index.lock')")) {-- log.Printf("Retry attempt %d failed due to 'unable to write new_index file'. Waiting %v before retrying. Error: %v", attempt+1, retryInterval, err)- time.Sleep(retryInterval)- retryInterval *= 2- continue- } else {- log.Printf("Non-index file error: %v", err)- }+ if isIndexLockError {+ log.Printf("Retry attempt %d failed due to 'unable to write git index file error'. Waiting %v before retrying. Error: %v", attempt+1, retryInterval, err)+ time.Sleep(retryInterval)+ retryInterval *= 2+ continue} else {- log.Printf("Non-exit error: %v", err)+ log.Printf("Non-index file error: %v", err)}return errdiff --git a/app/server/db/locks.go b/app/server/db/locks.goindex 8d6f53a1..e0209dbd 100644--- a/app/server/db/locks.go+++ b/app/server/db/locks.go@@ -136,10 +136,6 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {}canAcquire := true- canRetry := true-- // log.Println("locks:")- // spew.Dump(locks)for _, lock := range locks {lockBranch := ""@@ -148,20 +144,19 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {}if scope == LockScopeRead {- canAcquireThisLock := lock.Scope == LockScopeRead && lockBranch == branch- if !canAcquireThisLock {- canAcquire = false+ // if we're trying to acquire a read lock, we can do so unless there's a conflicting write lock+ // a write lock is conflicting if it's for the same branch or no branch (i.e. main)+ if lock.Scope == LockScopeWrite {+ if branch == "" || lockBranch == "" || branch == lockBranch {+ canAcquire = false+ break+ }}} else if scope == LockScopeWrite {- canAcquire = false-- // if lock is for the same plan plan and branch, allow parallel writes- if planId == lock.PlanId && branch == lockBranch {- canAcquire = true- }-- if lock.Scope == LockScopeWrite && lockBranch == branch {- canRetry = false+ // if we're trying to acquire a write lock, we can only do so if there's no existing lock (read or write) for the same branch (or no branch)+ if branch == "" || lockBranch == "" || branch == lockBranch {+ canAcquire = false+ break}} else {err = fmt.Errorf("invalid lock scope: %v", scope)@@ -170,19 +165,15 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {}if !canAcquire {- log.Println("can't acquire lock. canRetry:", canRetry, "numRetry:", numRetry)+ log.Println("can't acquire lock.", "numRetry:", numRetry)- if canRetry {- // 10 second timeout- if numRetry > 20 {- err = fmt.Errorf("plan is currently being updated by another user")- return "", err- }- time.Sleep(500 * time.Millisecond)- return lockRepo(params, numRetry+1)+ // 10 second timeout+ if numRetry > 20 {+ err = fmt.Errorf("plan is currently being updated by another user or process")+ return "", err}- err = fmt.Errorf("plan is currently being updated by another user")- return "", err+ time.Sleep(500 * time.Millisecond)+ return lockRepo(params, numRetry+1)}log.Println("can acquire lock - inserting new lock")