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
big refactor for concurrency and stability - change from local locks to local queue, keep using db locks as backup, centralize all repo reads and writes for better control. also consistent transaction wrapper for (almost) everywhere that uses transactions to ensure good rollback behavior. last but not least, wrap all model calls in ModelRequest for DRY and consistent handling of cancellation/failure. all model calls are streaming now which tolerate cancellation better.
Dane Schneider36af94a9edeb
diff --git a/app/server/db/branch_helpers.go b/app/server/db/branch_helpers.goindex 5dd3b9d7..c1b4691f 100644--- a/app/server/db/branch_helpers.go+++ b/app/server/db/branch_helpers.go@@ -1,9 +1,9 @@package dbimport (+ "context""database/sql""fmt"- "log"shared "plandex-shared"@@ -11,7 +11,7 @@ import ("github.com/lib/pq")-func CreateBranch(plan *Plan, parentBranch *Branch, name string, tx *sqlx.Tx) (*Branch, error) {+func CreateBranch(repo *GitRepo, plan *Plan, parentBranch *Branch, name string, tx *sqlx.Tx) (*Branch, error) {query := `INSERT INTO branches (org_id, owner_id, plan_id, parent_branch_id, name, status, context_tokens, convo_tokens)VALUES ($1, $2, $3, $4, $5, $6, $7, $8)@@ -86,7 +86,7 @@ func CreateBranch(plan *Plan, parentBranch *Branch, name string, tx *sqlx.Tx) (*// parentBranchName = parentBranch.Name// }- err = GitCreateBranch(plan.OrgId, plan.Id, name)+ err = repo.GitCreateBranch(name)if err != nil {return nil, fmt.Errorf("error creating git branch: %v", err)@@ -117,7 +117,7 @@ func GetDbBranch(planId, name string) (*Branch, error) {return &branch, nil}-func ListPlanBranches(orgId, planId string) ([]*Branch, error) {+func ListPlanBranches(repo *GitRepo, planId string) ([]*Branch, error) {var branches []*Brancherr := Conn.Select(&branches, "SELECT * FROM branches WHERE plan_id = $1 ORDER BY created_at", planId)@@ -127,7 +127,7 @@ func ListPlanBranches(orgId, planId string) ([]*Branch, error) {// log.Println("branches: ", spew.Sdump(branches))- gitBranches, err := GitListBranches(orgId, planId)+ gitBranches, err := repo.GitListBranches()if err != nil {return nil, fmt.Errorf("error listing git branches: %v", err)@@ -161,55 +161,27 @@ func ListBranchesForPlans(orgId string, planIds []string) ([]*Branch, error) {return branches, nil}-func DeleteBranch(orgId, planId, branch string) error {- tx, err := Conn.Beginx()+func DeleteBranch(ctx context.Context, repo *GitRepo, planId, branch string) error {+ return WithTx(ctx, "delete branch", func(tx *sqlx.Tx) error {- if err != nil {- return fmt.Errorf("error starting transaction: %v", err)- }-- var committed bool+ _, err := tx.Exec("DELETE FROM branches WHERE plan_id = $1 AND name = $2", planId, branch)- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return- }-- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")+ if err != nil {+ return fmt.Errorf("error deleting branch: %v", err)}- }()-- _, err = tx.Exec("DELETE FROM branches WHERE plan_id = $1 AND name = $2", planId, branch)-- if err != nil {- return fmt.Errorf("error deleting branch: %v", err)- }-- err = IncActiveBranches(planId, -1, tx)- if err != nil {- return fmt.Errorf("error decrementing active branches: %v", err)- }-- err = GitDeleteBranch(orgId, planId, branch)+ err = IncActiveBranches(planId, -1, tx)- if err != nil {- return fmt.Errorf("error deleting branch dir: %v", err)- }+ if err != nil {+ return fmt.Errorf("error decrementing active branches: %v", err)+ }- err = tx.Commit()+ err = repo.GitDeleteBranch(branch)- if err != nil {- return fmt.Errorf("error committing transaction: %v", err)- }+ if err != nil {+ return fmt.Errorf("error deleting branch dir: %v", err)+ }- return nil+ return err+ })}diff --git a/app/server/db/convo_helpers.go b/app/server/db/convo_helpers.goindex 7a0c09a9..9c0efbfe 100644--- a/app/server/db/convo_helpers.go+++ b/app/server/db/convo_helpers.go@@ -89,7 +89,7 @@ func GetConvoMessage(orgId, planId, messageId string) (*ConvoMessage, error) {return &convoMessage, nil}-func StoreConvoMessage(message *ConvoMessage, currentUserId, branch string, commit bool) (string, error) {+func StoreConvoMessage(repo *GitRepo, message *ConvoMessage, currentUserId, branch string, commit bool) (string, error) {convoDir := getPlanConversationDir(message.OrgId, message.PlanId)ts := time.Now().UTC()@@ -181,7 +181,7 @@ func StoreConvoMessage(message *ConvoMessage, currentUserId, branch string, comm// }if commit {- err = GitAddAndCommit(message.OrgId, message.PlanId, branch, msg)+ err = repo.GitAddAndCommit(branch, msg)if err != nil {return "", fmt.Errorf("error committing convo message: %v", err)}diff --git a/app/server/db/git.go b/app/server/db/git.goindex d42cceae..0f59dd15 100644--- a/app/server/db/git.go+++ b/app/server/db/git.go@@ -7,7 +7,6 @@ import ("os""os/exec""path/filepath"- "runtime/debug""strconv""strings""time"@@ -28,6 +27,11 @@ func init() {}}+type GitRepo struct {+ orgId string+ planId string+}+func InitGitRepo(orgId, planId string) error {dir := getPlanDir(orgId, planId)return initGitRepo(dir)@@ -51,15 +55,20 @@ func initGitRepo(dir string) error {return nil}-func GitAddAndCommit(orgId, planId, branch, message string) error {- err := assertWriteOrRootLockSet(planId, branch)- if err != nil {- return err+func getGitRepo(orgId, planId string) *GitRepo {+ return &GitRepo{+ orgId: orgId,+ planId: planId,}+}++func (repo *GitRepo) GitAddAndCommit(branch, message string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(func() error {+ err := gitWriteOperation(func() error {return gitAdd(dir, ".")}, fmt.Sprintf("GitAddAndCommit > gitAdd: plan=%s branch=%s", planId, branch))if err != nil {@@ -76,15 +85,13 @@ func GitAddAndCommit(orgId, planId, branch, message string) error {return nil}-func GitRewindToSha(orgId, planId, branch, sha string) error {- err := assertWriteOrRootLockSet(planId, branch)- if err != nil {- return err- }+func (repo *GitRepo) GitRewindToSha(branch, sha string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(func() error {+ err := gitWriteOperation(func() error {return gitRewindToSha(dir, sha)}, fmt.Sprintf("GitRewindToSha > gitRewindToSha: plan=%s branch=%s", planId, branch))if err != nil {@@ -94,11 +101,9 @@ func GitRewindToSha(orgId, planId, branch, sha string) error {return nil}-func GetCurrentCommitSha(orgId, planId string) (sha string, err error) {- err = assertAnyPlanLockSet(planId, "")- if err != nil {- return "", err- }+func (repo *GitRepo) GetCurrentCommitSha() (sha string, err error) {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)@@ -112,11 +117,9 @@ func GetCurrentCommitSha(orgId, planId string) (sha string, err error) {return sha, nil}-func GetCommitTime(orgId, planId, branch, ref string) (time.Time, error) {- err := assertAnyPlanLockSet(planId, branch)- if err != nil {- return time.Time{}, err- }+func (repo *GitRepo) GetCommitTime(branch, ref string) (time.Time, error) {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)@@ -138,15 +141,13 @@ func GetCommitTime(orgId, planId, branch, ref string) (time.Time, error) {return commitTime, nil}-func GitResetToSha(orgId, planId, sha string) error {- err := assertWriteOrRootLockSet(planId, "")- if err != nil {- return err- }+func (repo *GitRepo) GitResetToSha(sha string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(func() error {+ err := gitWriteOperation(func() error {cmd := exec.Command("git", "-C", dir, "reset", "--hard", sha)_, err := cmd.Output()if err != nil {@@ -163,15 +164,13 @@ func GitResetToSha(orgId, planId, sha string) error {return nil}-func GitCheckoutSha(orgId, planId, sha string) error {- err := assertWriteOrRootLockSet(planId, "")- if err != nil {- return err- }+func (repo *GitRepo) GitCheckoutSha(sha string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(func() error {+ err := gitWriteOperation(func() error {cmd := exec.Command("git", "-C", dir, "checkout", sha)_, err := cmd.Output()if err != nil {@@ -188,11 +187,9 @@ func GitCheckoutSha(orgId, planId, sha string) error {return nil}-func GetGitCommitHistory(orgId, planId, branch string) (body string, shas []string, err error) {- err = assertAnyPlanLockSet(planId, branch)- if err != nil {- return "", nil, err- }+func (repo *GitRepo) GetGitCommitHistory(branch string) (body string, shas []string, err error) {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)@@ -204,11 +201,9 @@ func GetGitCommitHistory(orgId, planId, branch string) (body string, shas []strireturn body, shas, nil}-func GetLatestCommit(orgId, planId, branch string) (sha, body string, err error) {- err = assertAnyPlanLockSet(planId, branch)- if err != nil {- return "", "", err- }+func (repo *GitRepo) GetLatestCommit(branch string) (sha, body string, err error) {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)@@ -220,11 +215,9 @@ func GetLatestCommit(orgId, planId, branch string) (sha, body string, err error)return sha, body, nil}-func GetLatestCommitShaBeforeTime(orgId, planId, branch string, before time.Time) (sha string, err error) {- err = assertAnyPlanLockSet(planId, branch)- if err != nil {- return "", err- }+func (repo *GitRepo) GetLatestCommitShaBeforeTime(branch string, before time.Time) (sha string, err error) {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)@@ -262,11 +255,9 @@ func GetLatestCommitShaBeforeTime(orgId, planId, branch string, before time.Timereturn sha, nil}-func GitListBranches(orgId, planId string) ([]string, error) {- err := assertAnyPlanLockSet(planId, "")- if err != nil {- return nil, err- }+func (repo *GitRepo) GitListBranches() ([]string, error) {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)@@ -274,7 +265,7 @@ func GitListBranches(orgId, planId string) ([]string, error) {cmd := exec.Command("git", "branch", "--format=%(refname:short)")cmd.Dir = dircmd.Stdout = &out- err = cmd.Run()+ err := cmd.Run()if err != nil {return nil, fmt.Errorf("error getting git branches for dir: %s, err: %v", dir, err)}@@ -288,15 +279,13 @@ func GitListBranches(orgId, planId string) ([]string, error) {return branches, nil}-func GitCreateBranch(orgId, planId, newBranch string) error {- err := assertWriteOrRootLockSet(planId, "")- if err != nil {- return err- }+func (repo *GitRepo) GitCreateBranch(newBranch string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(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))@@ -312,15 +301,13 @@ func GitCreateBranch(orgId, planId, newBranch string) error {return nil}-func GitDeleteBranch(orgId, planId, branchName string) error {- err := assertWriteOrRootLockSet(planId, "")- if err != nil {- return err- }+func (repo *GitRepo) GitDeleteBranch(branchName string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(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))@@ -336,15 +323,13 @@ func GitDeleteBranch(orgId, planId, branchName string) error {return nil}-func GitClearUncommittedChanges(orgId, planId, branch string) error {- err := assertWriteOrRootLockSet(planId, branch)- if err != nil {- return err- }+func (repo *GitRepo) GitClearUncommittedChanges(branch string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(func() error {+ err := gitWriteOperation(func() error {// Reset staged changesres, err := exec.Command("git", "-C", dir, "reset", "--hard").CombinedOutput()if err != nil {@@ -370,16 +355,13 @@ func GitClearUncommittedChanges(orgId, planId, branch string) error {return err}-func GitCheckoutBranch(orgId, planId, branch string) error {- // even though we use 'gitWriteOperation' below, we also check out a branch for reads, so we just need to assert that any lock is set- err := assertAnyPlanLockSet(planId, branch)- if err != nil {- return err- }+func (repo *GitRepo) GitCheckoutBranch(branch string) error {+ orgId := repo.orgId+ planId := repo.planIddir := getPlanDir(orgId, planId)- err = gitWriteOperation(func() error {+ err := gitWriteOperation(func() error {return gitCheckoutBranch(dir, branch)}, fmt.Sprintf("GitCheckoutBranch > gitCheckout: plan=%s branch=%s", planId, branch))@@ -686,54 +668,3 @@ func gitWriteOperation(operation func() error, label string) error {}return fmt.Errorf("operation %s failed after %d attempts: %v", label, maxGitRetries, err)}--func assertWriteOrRootLockSet(planId, branch string) error {- planLocksMu.Lock()- defer planLocksMu.Unlock()-- lock, ok := planLocks[planId]- if !ok || lock.currentBranch != branch || (!lock.hasWriter && lock.currentBranch != "") {- stack := debug.Stack()-- if !ok {- log.Printf("Plan %s branch %s NO LOCK SET for git repo write operation\n%s", planId, branch, formatStackTraceLong(stack))-- return fmt.Errorf("plan %s branch %s NO LOCK SET for git repo write operation", planId, branch)- } else if lock.currentBranch != branch {- log.Printf("lock.currentBranch: %s, branch: %s", lock.currentBranch, branch)- log.Printf("Plan %s branch %s WRONG LOCK SET for git repo write operation\n%s", planId, branch, formatStackTraceLong(stack))-- return fmt.Errorf("plan %s branch %s WRONG LOCK SET for git repo write operation", planId, branch)- } else if !lock.hasWriter {- log.Printf("lock.hasWriter: %t", lock.hasWriter)- log.Printf("Plan %s branch %s NO WRITER LOCK SET for git repo write operation\n%s", planId, branch, formatStackTraceLong(stack))-- return fmt.Errorf("plan %s branch %s NO WRITER LOCK SET for git repo write operation", planId, branch)- }-- }-- return nil-}--func assertAnyPlanLockSet(planId, branch string) error {- planLocksMu.Lock()- defer planLocksMu.Unlock()-- lock, ok := planLocks[planId]- if !ok {- stack := debug.Stack()- log.Printf("Plan %s branch %s NO LOCK SET for git repo operation\n%s", planId, branch, formatStackTraceLong(stack))- return fmt.Errorf("plan %s NO LOCK SET", planId)- }-- // if the lock is set on a branch rather than a plan lock, require the branch to match- if lock.currentBranch != "" && lock.currentBranch != branch {- stack := debug.Stack()- log.Printf("Plan %s branch %s WRONG LOCK SET for git repo operation\n%s", planId, branch, formatStackTraceLong(stack))- return fmt.Errorf("plan %s WRONG LOCK SET", planId)- }-- // otherwise it's a root plan lock, so we allow any branch- return nil-}diff --git a/app/server/db/invite_helpers.go b/app/server/db/invite_helpers.goindex a25daea6..146686df 100644--- a/app/server/db/invite_helpers.go+++ b/app/server/db/invite_helpers.go@@ -1,9 +1,9 @@package dbimport (+ "context""database/sql""fmt"- "log""strings""github.com/jmoiron/sqlx"@@ -114,48 +114,26 @@ func DeleteInvite(id string, tx *sqlx.Tx) error {return nil}-func AcceptInvite(invite *Invite, inviteeId string) error {- // start a transaction- tx, err := Conn.Beginx()- if err != nil {- return fmt.Errorf("error starting transaction: %v", err)- }-- var committed bool+func AcceptInvite(ctx context.Context, invite *Invite, inviteeId string) error {+ err := WithTx(ctx, "accept invite", func(tx *sqlx.Tx) error {- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return+ _, err := tx.Exec(`UPDATE invites SET accepted_at = NOW(), invitee_id = $1 WHERE id = $2`, inviteeId, invite.Id)+ if err != nil {+ return fmt.Errorf("error accepting invite: %v", err)}- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")- }- }()-- _, err = tx.Exec(`UPDATE invites SET accepted_at = NOW(), invitee_id = $1 WHERE id = $2`, inviteeId, invite.Id)- if err != nil {- return fmt.Errorf("error accepting invite: %v", err)- }+ // create org user+ err = CreateOrgUser(invite.OrgId, inviteeId, invite.OrgRoleId, tx)- // create org user- err = CreateOrgUser(invite.OrgId, inviteeId, invite.OrgRoleId, tx)+ if err != nil {+ return fmt.Errorf("error creating org user: %v", err)+ }- if err != nil {- return fmt.Errorf("error creating org user: %v", err)- }+ return nil+ })- // commit transaction- err = tx.Commit()if err != nil {- return fmt.Errorf("error committing transaction: %v", err)+ return fmt.Errorf("error accepting invite: %v", err)}invite.InviteeId = &inviteeIddiff --git a/app/server/db/locks.go b/app/server/db/locks.goindex 3f67982b..f953f759 100644--- a/app/server/db/locks.go+++ b/app/server/db/locks.go@@ -12,7 +12,6 @@ import ("runtime/debug""strconv""strings"- "sync""time""github.com/lib/pq"@@ -41,299 +40,9 @@ type LockRepoParams struct {CancelFn context.CancelFunc}-// ------------------------------------------------------------------------------// Local "Branch‐Aware" RW Lock-// -------------------------------------------------------------------------------// planLocksMu protects the global map of planId -> *PlanLockState-var planLocksMu sync.Mutex--// planLocks tracks all plan concurrency states in this process-var planLocks = make(map[string]*PlanLockState)--// PlanLockState tracks concurrency for one plan, with its own mutex and cond.-type PlanLockState struct {- mu sync.Mutex- cond *sync.Cond- currentBranch string- readCount int- hasWriter bool- waitingWriters int // Track number of writers waiting to acquire the lock-}--// getOrCreatePlanLockState fetches or creates a PlanLockState for a planId.-func getOrCreatePlanLockState(planId string) *PlanLockState {- log.Printf("Attempting to acquire planLocksMu for plan %s", planId)-- planLocksMu.Lock()- log.Printf("Acquired planLocksMu for plan %s", planId)- defer planLocksMu.Unlock()-- pls := planLocks[planId]- log.Printf("Current plan lock state for %s: %+v", planId, pls)-- if pls == nil {- log.Printf("Creating new plan lock state for %s", planId)- pls = &PlanLockState{}- pls.cond = sync.NewCond(&pls.mu)- planLocks[planId] = pls- }- return pls-}--// acquireLocalLock attempts to acquire local concurrency for (planId, branch, scope).-// Blocks until successful or context canceled.-func acquireLocalLock(ctx context.Context, planId, branch string, scope LockScope) error {- if locksVerboseLogging {- log.Printf("Attempting to acquire local lock for plan %s branch %s scope %s", planId, branch, scope)- }-- pls := getOrCreatePlanLockState(planId)-- if locksVerboseLogging {- log.Printf("About to acquire mutex for plan %s", planId)- }- pls.mu.Lock()- if locksVerboseLogging {- log.Printf("Acquired mutex for plan %s", planId)- }- defer pls.mu.Unlock()-- // Track waiting writers- if scope == LockScopeWrite {- pls.waitingWriters++- if locksVerboseLogging {- log.Printf("Incrementing waiting writers for plan %s to %d", planId, pls.waitingWriters)- }- defer func() {- pls.waitingWriters--- if locksVerboseLogging {- log.Printf("Decremented waiting writers for plan %s to %d", planId, pls.waitingWriters)- }- }()- }-- for {- if locksVerboseLogging {- log.Printf("Lock state for plan %s: hasWriter=%v readCount=%d currentBranch=%s waitingWriters=%d",- planId, pls.hasWriter, pls.readCount, pls.currentBranch, pls.waitingWriters)- }-- // If there's an active writer, no read or write can proceed- if pls.hasWriter {- if locksVerboseLogging {- log.Printf("Plan %s has writer, waiting...", planId)- }- if err := waitOrCtx(ctx, pls); err != nil {- return err- }- continue- }-- if scope == LockScopeRead {- // If we have existing readers, must match branch- if pls.readCount > 0 {- if pls.currentBranch != branch {- if locksVerboseLogging {- log.Printf("Plan %s has readers on different branch %s, waiting...",- planId, pls.currentBranch)- }- if err := waitOrCtx(ctx, pls); err != nil {- return err- }- continue- }- } else {- // No readers yet => set the branch- if locksVerboseLogging {- log.Printf("Plan %s has no readers, setting branch to %s", planId, branch)- }- pls.currentBranch = branch- }- // Acquire a read slot- pls.readCount++- if locksVerboseLogging {- log.Printf("Acquired read lock for plan %s branch %s (readCount=%d)",- planId, branch, pls.readCount)- }- return nil-- } else {- // scope == LockScopeWrite- // Write can only proceed if no readers- if pls.readCount > 0 {- if locksVerboseLogging {- log.Printf("Plan %s has readers, waiting for write lock...", planId)- }- if err := waitOrCtx(ctx, pls); err != nil {- return err- }- continue- }- // Acquire the writer- pls.hasWriter = true- pls.currentBranch = branch- if locksVerboseLogging {- log.Printf("Acquired write lock for plan %s branch %s", planId, branch)- }- return nil- }- }-}--// releaseLocalLock frees local concurrency for (planId, branch, scope).-func releaseLocalLock(planId, branch string, scope LockScope) {- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Starting to release local lock for plan %s branch %s scope %s", planId, branch, scope)- }-- planLocksMu.Lock()- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Acquired planLocksMu for deletion check for plan %s", planId)- }- pls, ok := planLocks[planId]- if !ok {- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] No lock state found for plan %s", planId)- }- planLocksMu.Unlock()- return- }- planLocksMu.Unlock()-- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] About to acquire mutex for plan %s. Pre-state: hasWriter=%v readCount=%d currentBranch=%s waitingWriters=%d",- planId, pls.hasWriter, pls.readCount, pls.currentBranch, pls.waitingWriters)- }- pls.mu.Lock()- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Acquired mutex for plan %s", planId)- }- defer pls.mu.Unlock()-- if scope == LockScopeRead {- pls.readCount--- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Released read lock for plan %s branch %s (readCount=%d)",- planId, branch, pls.readCount)- }- if pls.readCount < 0 {- log.Printf("BUG: readCount < 0 for plan %s", planId)- pls.readCount = 0- }- } else {- prevState := fmt.Sprintf("hasWriter=%v readCount=%d currentBranch=%s waitingWriters=%d",- pls.hasWriter, pls.readCount, pls.currentBranch, pls.waitingWriters)- pls.hasWriter = false- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Released write lock for plan %s. Previous state: %s, New state: hasWriter=%v readCount=%d currentBranch=%s waitingWriters=%d",- planId, prevState, pls.hasWriter, pls.readCount, pls.currentBranch, pls.waitingWriters)- }- }-- // Only remove state if no active readers/writers AND no waiting writers- if pls.readCount == 0 && !pls.hasWriter && pls.waitingWriters == 0 {- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] No readers/writers/waiters left, removing state for plan %s", planId)- }- pls.currentBranch = ""- planLocksMu.Lock()- delete(planLocks, planId)- planLocksMu.Unlock()- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Removed lock state for plan %s from global map", planId)- }- } else if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Keeping lock state for plan %s (readCount=%d, hasWriter=%v, waitingWriters=%d)",- planId, pls.readCount, pls.hasWriter, pls.waitingWriters)- }-- pls.cond.Broadcast()- if locksVerboseLogging {- log.Printf("[LOCAL_UNLOCK] Completed unlock operation for plan %s", planId)- }-}--// waitOrCtx calls pls.cond.Wait() but also checks if ctx is canceled.-func waitOrCtx(ctx context.Context, pls *PlanLockState) error {- // Since we already hold pls.mu, we do:- if ctx.Err() != nil {- // context canceled?- return ctx.Err()- }-- // Then do the standard cond.Wait() call- pls.cond.Wait()-- // Once we return from Wait(), we hold pls.mu again- // The caller can re-check conditions in its loop- if ctx.Err() != nil {- return ctx.Err()- }- return nil-}--func LockRepo(params LockRepoParams) (string, error) {- var attempt int- for {- // Step 1: Local concurrency- ctx, cancel := context.WithTimeout(params.Ctx, localLockTimeout)- defer cancel()-- err := acquireLocalLock(ctx, params.PlanId, params.Branch, params.Scope)- if err == context.DeadlineExceeded {- return "", fmt.Errorf("local lock acquisition timed out after 30s: %w", err)- }-- // Step 2: Attempt DB lock- id, err := lockRepoDB(params, 0)- if err != nil {- // Release local if DB fails- releaseLocalLock(params.PlanId, params.Branch, params.Scope)-- if ctxErr := params.Ctx.Err(); ctxErr != nil {- return "", ctxErr- }- attempt++- if attempt > maxRetries {- return "", fmt.Errorf("DB lock conflict too many times: %v", err)- }- // Exponential backoff- backoffSleep := time.Duration(float64(initialRetryDelay) * math.Pow(backoffFactor, float64(attempt)))- time.Sleep(backoffSleep)- continue- }- // success- return id, nil- }-}--func DeleteRepoLock(id, planId string) error {- // Try to fetch scope + branch from DB- var scope LockScope- var maybeBranch *string- err := Conn.QueryRow("SELECT scope, branch FROM repo_locks WHERE id = $1", id).Scan(&scope, &maybeBranch)- if err != nil && err != sql.ErrNoRows {- return fmt.Errorf("error getting lock info: %v", err)- }-- branch := ""- if maybeBranch != nil {- branch = *maybeBranch- }-- // Then free local concurrency- releaseLocalLock(planId, branch, scope)-- // Do the DB remove first- dbErr := deleteRepoLockDB(id, planId, 0)-- return dbErr-}-func lockRepoDB(params LockRepoParams, numRetry int) (string, error) {start := time.Now()- goroutineID := getGoroutineID() // You'll need to implement this+ goroutineID := getGoroutineID()if locksVerboseLogging {log.Printf("[Lock][%d] START lock attempt for plan %s scope %s (retry %d) at %v",@@ -361,7 +70,12 @@ func lockRepoDB(params LockRepoParams, numRetry int) (string, error) {}initialJitter := time.Duration(rand.Int63n(int64(5000 * time.Microsecond)))- time.Sleep(initialJitter)++ select {+ case <-params.Ctx.Done():+ return "", params.Ctx.Err()+ case <-time.After(initialJitter):+ }orgId := params.OrgIduserId := params.UserId@@ -399,6 +113,11 @@ func lockRepoDB(params LockRepoParams, numRetry int) (string, error) {return}+ panicErr := recover()+ if panicErr != nil {+ log.Printf("panic in lock repo: %v", panicErr)+ }+if rbErr := tx.Rollback(); rbErr != nil {if rbErr == sql.ErrTxDone {if locksVerboseLogging {@@ -668,7 +387,7 @@ func lockRepoDB(params LockRepoParams, numRetry int) (string, error) {for {select {case <-ctx.Done():- err := DeleteRepoLock(newLock.Id, planId)+ err := deleteRepoLockDB(newLock.Id, planId, 0)if err != nil {log.Printf("Error unlocking repo: %v\n", err)}@@ -676,7 +395,12 @@ func lockRepoDB(params LockRepoParams, numRetry int) (string, error) {default:jitter := time.Duration(rand.Int63n(int64(float64(lockHeartbeatInterval) * 0.1)))- time.Sleep(lockHeartbeatInterval + jitter)++ select {+ case <-ctx.Done():+ return+ case <-time.After(lockHeartbeatInterval + jitter):+ }res, err := Conn.Exec("UPDATE repo_locks SET last_heartbeat_at = NOW() WHERE id = $1", newLock.Id)@@ -949,13 +673,20 @@ func CleanupAllLocks(ctx context.Context) error {// Ensure rollback is attempted in case of failuredefer func() {- if err != nil {- if rbErr := tx.Rollback(); rbErr != nil {- log.Printf("transaction rollback error: %v\n", rbErr)+ panicErr := recover()+ if panicErr != nil {+ log.Printf("panic in cleanup all locks: %v", panicErr)+ }++ if rbErr := tx.Rollback(); rbErr != nil {+ if rbErr == sql.ErrTxDone {+ // log.Println("attempted to roll back transaction, but it was already committed")} else {- if locksVerboseLogging {- log.Println("transaction rolled back")- }+ log.Printf("transaction rollback error: %v\n", rbErr)+ }+ } else {+ if locksVerboseLogging {+ log.Println("transaction rolled back")}}}()diff --git a/app/server/db/plan_helpers.go b/app/server/db/plan_helpers.goindex dc305b5d..dd09f8ef 100644--- a/app/server/db/plan_helpers.go+++ b/app/server/db/plan_helpers.go@@ -1,7 +1,7 @@package dbimport (- "database/sql"+ "context""encoding/json""fmt""log"@@ -18,92 +18,75 @@ import ("github.com/sashabaranov/go-openai")-func CreatePlan(orgId, projectId, userId, name string) (*Plan, error) {- // start a transaction- tx, err := Conn.Beginx()- if err != nil {- return nil, fmt.Errorf("error starting transaction: %v", err)- }+func CreatePlan(ctx context.Context, orgId, projectId, userId, name string) (*Plan, error) {+ var plan *Plan+ err := WithTx(ctx, "create plan", func(tx *sqlx.Tx) error {- var committed bool+ planConfig, err := GetDefaultPlanConfig(userId)- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return+ if err != nil {+ return fmt.Errorf("error getting default plan config: %v", err)}- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")+ query := `INSERT INTO plans (org_id, owner_id, project_id, name, plan_config)+ VALUES ($1, $2, $3, $4, $5)+ RETURNING id, created_at, updated_at`++ plan = &Plan{+ OrgId: orgId,+ OwnerId: userId,+ ProjectId: projectId,+ Name: name,+ PlanConfig: planConfig,}- }()- planConfig, err := GetDefaultPlanConfig(userId)+ err = tx.QueryRow(+ query,+ orgId,+ userId,+ projectId,+ name,+ planConfig,+ ).Scan(+ &plan.Id,+ &plan.CreatedAt,+ &plan.UpdatedAt,+ )- if err != nil {- return nil, fmt.Errorf("error getting default plan config: %v", err)- }+ if err != nil {+ return fmt.Errorf("error creating plan: %v", err)+ }- query := `INSERT INTO plans (org_id, owner_id, project_id, name, plan_config)- VALUES ($1, $2, $3, $4, $5)- RETURNING id, created_at, updated_at`+ _, err = tx.Exec("INSERT INTO lockable_plan_ids (plan_id) VALUES ($1)", plan.Id)- plan := &Plan{- OrgId: orgId,- OwnerId: userId,- ProjectId: projectId,- Name: name,- PlanConfig: planConfig,- }-- err = tx.QueryRow(- query,- orgId,- userId,- projectId,- name,- planConfig,- ).Scan(- &plan.Id,- &plan.CreatedAt,- &plan.UpdatedAt,- )+ if err != nil {+ return fmt.Errorf("error inserting lockable plan id: %v", err)+ }- if err != nil {- return nil, fmt.Errorf("error creating plan: %v", err)- }+ // the one place where we do this to skip the locking queue+ // ok to cheat this once since we're creating a new plan+ repo := getGitRepo(orgId, plan.Id)+ _, err = CreateBranch(repo, plan, nil, "main", tx)- _, err = tx.Exec("INSERT INTO lockable_plan_ids (plan_id) VALUES ($1)", plan.Id)+ if err != nil {+ return fmt.Errorf("error creating main branch: %v", err)+ }- if err != nil {- return nil, fmt.Errorf("error inserting lockable plan id: %v", err)- }+ log.Println("Created branch main")- _, err = CreateBranch(plan, nil, "main", tx)+ err = InitPlan(orgId, plan.Id)- if err != nil {- return nil, fmt.Errorf("error creating main branch: %v", err)- }+ if err != nil {+ return fmt.Errorf("error initializing plan dir: %v", err)+ }- log.Println("Created branch main")+ log.Println("Initialized plan dir")- err = InitPlan(orgId, plan.Id)+ return nil+ })if err != nil {- return nil, fmt.Errorf("error initializing plan dir: %v", err)- }-- log.Println("Initialized plan dir")-- // commit the transaction- if err := tx.Commit(); err != nil {- return nil, fmt.Errorf("error committing transaction: %v", err)+ return nil, err}return plan, nildiff --git a/app/server/db/queue.go b/app/server/db/queue.gonew file mode 100644index 00000000..0e34e564--- /dev/null+++ b/app/server/db/queue.go@@ -0,0 +1,232 @@+package db++import (+ "context"+ "fmt"+ "log"+ "sync"++ "github.com/google/uuid"+)++type repoOpFn func(repo *GitRepo) error++type repoOperation struct {+ orgId string+ userId string+ planId string+ branch string+ scope LockScope+ planBuildId string+ id string+ reason string+ op repoOpFn+ ctx context.Context+ cancelFn context.CancelFunc+ done chan error+ clearRepoOnErr bool+}++type repoQueue struct {+ ops []*repoOperation+ mu sync.Mutex+ isProcessing bool+}++type repoQueueMap map[string]*repoQueue++var queuesMu sync.Mutex+var repoQueues = make(repoQueueMap)++func (m repoQueueMap) getQueue(planId string) *repoQueue {+ queuesMu.Lock()+ defer queuesMu.Unlock()++ q, ok := m[planId]+ if !ok {+ q = &repoQueue{}+ m[planId] = q+ }+ return q+}++func (m repoQueueMap) add(op *repoOperation) int {+ q := m.getQueue(op.planId)+ return q.add(op)+}++// Add enqueues an operation, and then kicks off processing if needed.+func (q *repoQueue) add(op *repoOperation) int {+ var numOps int+ q.mu.Lock()+ q.ops = append(q.ops, op)+ numOps = len(q.ops)++ // If nobody else is processing, we’ll start+ if !q.isProcessing {+ q.isProcessing = true+ go q.runQueue() // run in the background+ }+ q.mu.Unlock()++ return numOps+}++func (q *repoQueue) nextBatch() []*repoOperation {+ q.mu.Lock()+ defer q.mu.Unlock()++ if len(q.ops) == 0 {+ return nil+ }++ firstOp := q.ops[0]+ res := []*repoOperation{firstOp}++ q.ops = q.ops[1:]++ // writes always go one at a time, blocking everything else, as do read locks on the root plan (no branch)+ if firstOp.scope == LockScopeWrite || firstOp.branch == "" {+ return res+ }++ // reads go in parallel as long as they are on the same branch+ for len(q.ops) > 0 {+ op := q.ops[0]+ if op.scope == LockScopeRead && op.branch == firstOp.branch {+ res = append(res, op)+ q.ops = q.ops[1:]+ } else {+ break+ }+ }++ return res+}++func (q *repoQueue) runQueue() {+ for {+ // get the next batch+ ops := q.nextBatch()+ if len(ops) == 0 {+ // Nothing left in the queue, so mark not processing and return+ q.mu.Lock()+ q.isProcessing = false+ q.mu.Unlock()+ return+ }++ log.Printf("processing batch of %d operations", len(ops))++ // Process the batch+ // If it's a writer => single op+ // If multiple same‐branch readers => do them in parallel+ var wg sync.WaitGroup+ for _, op := range ops {+ wg.Add(1)+ go func(op *repoOperation) {+ defer wg.Done()+ select {+ case <-op.ctx.Done():+ op.done <- op.ctx.Err()+ default:+ log.Printf("processing operation %s - (%s)", op.id, op.reason)++ lockId, err := lockRepoDB(LockRepoParams{+ OrgId: op.orgId,+ UserId: op.userId,+ PlanId: op.planId,+ Branch: op.branch,+ Scope: op.scope,+ PlanBuildId: op.planBuildId,+ Ctx: op.ctx,+ CancelFn: op.cancelFn,+ }, 0)++ if err != nil {+ log.Printf("failed to get DB lock: %v", err)+ op.done <- fmt.Errorf("failed to get DB lock: %w", err)+ return+ }++ // actually do the operation+ repo := getGitRepo(op.orgId, op.planId)+ var opErr error++ func() {+ defer func() {+ panicErr := recover()+ if panicErr != nil {+ log.Printf("panic in operation: %v", panicErr)+ opErr = fmt.Errorf("panic in operation: %v", panicErr)+ }++ if opErr != nil && op.scope == LockScopeWrite && op.clearRepoOnErr {+ err := repo.GitClearUncommittedChanges(op.branch)+ if err != nil {+ log.Printf("failed to clear repo after error: %v", err)+ }+ }+ }()++ opErr = op.op(repo)+ }()++ releaseErr := deleteRepoLockDB(lockId, op.planId, 0)+ if releaseErr != nil {+ log.Printf("failed to release DB lock: %v", releaseErr)+ op.done <- fmt.Errorf("failed to release DB lock: %w", releaseErr)+ return+ }++ // signal to the caller via op.done+ op.done <- opErr+ }+ }(op)+ }+ wg.Wait()+ }+}++type ExecRepoOperationParams struct {+ OrgId string+ UserId string+ PlanId string+ Branch string+ Scope LockScope+ PlanBuildId string+ Reason string+ Ctx context.Context+ CancelFn context.CancelFunc+ ClearRepoOnErr bool+}++func ExecRepoOperation(+ params ExecRepoOperationParams,+ op repoOpFn,+) error {+ id := uuid.New().String()+ done := make(chan error)+ numOps := repoQueues.add(&repoOperation{+ id: id,+ orgId: params.OrgId,+ planId: params.PlanId,+ branch: params.Branch,+ scope: params.Scope,+ reason: params.Reason,+ op: op,+ done: done,+ clearRepoOnErr: params.ClearRepoOnErr,+ })++ if numOps > 1 {+ log.Printf("operation %s - (%s) queued behind %d operations", id, params.Reason, numOps-1)+ }++ select {+ case err := <-done:+ return err+ case <-params.Ctx.Done():+ return params.Ctx.Err()+ }+}diff --git a/app/server/db/result_helpers.go b/app/server/db/result_helpers.goindex dd85cbe5..bbc485da 100644--- a/app/server/db/result_helpers.go+++ b/app/server/db/result_helpers.go@@ -440,7 +440,7 @@ type ApplyPlanParams struct {CommitMsg string}-func ApplyPlan(ctx context.Context, params ApplyPlanParams) error {+func ApplyPlan(repo *GitRepo, ctx context.Context, params ApplyPlanParams) error {orgId := params.OrgIduserId := params.UserIdbranchName := params.BranchName@@ -684,7 +684,7 @@ func ApplyPlan(ctx context.Context, params ApplyPlanParams) error {msg += "\n\n" + updateContextRes.Msg}- err = GitAddAndCommit(orgId, plan.Id, branchName, msg)+ err = repo.GitAddAndCommit(branchName, msg)if err != nil {return fmt.Errorf("error committing plan: %v", err)diff --git a/app/server/db/transactions.go b/app/server/db/transactions.gonew file mode 100644index 00000000..97115740--- /dev/null+++ b/app/server/db/transactions.go@@ -0,0 +1,72 @@+package db++import (+ "context"+ "database/sql"+ "fmt"+ "log"+ "runtime/debug"++ "github.com/jmoiron/sqlx"+)++func WithTx(ctx context.Context, reason string, fn func(tx *sqlx.Tx) error) error {+ return withTx(ctx, nil, reason, fn)+}++func WithTxOpts(ctx context.Context, opts *sql.TxOptions, reason string, fn func(tx *sqlx.Tx) error) error {+ return withTx(ctx, opts, reason, fn)+}++func withTx(ctx context.Context, opts *sql.TxOptions, reason string, fn func(tx *sqlx.Tx) error) error {+ log.Printf("starting transaction: (%s)", reason)++ tx, err := Conn.BeginTxx(ctx, opts)+ if err != nil {+ return fmt.Errorf("error starting transaction: %v", err)+ }++ var committed bool++ // Ensure that rollback is attempted in case of failure+ defer func() {+ panicErr := recover()+ if panicErr != nil {+ log.Printf("panic in WithTx (%s): %v", reason, panicErr)+ log.Printf("stack trace (panic - %s):\n%s", reason, debug.Stack())+ }++ if committed {+ return+ }++ if rbErr := tx.Rollback(); rbErr != nil {+ if rbErr == sql.ErrTxDone {+ log.Printf("attempted to roll back transaction, but it was already committed: (%s)", reason)+ } else {+ log.Printf("transaction rollback error: (%s) %v\n", reason, rbErr)+ }+ } else {+ log.Printf("transaction rolled back: (%s)", reason)+ }+ }()++ err = fn(tx)++ if err != nil {+ log.Printf("error in WithTx (%s): %v", reason, err)+ return err+ }++ err = tx.Commit()+ if err != nil {+ log.Printf("error committing transaction: (%s) %v", reason, err)+ return fmt.Errorf("error committing transaction: %v", err)+ }++ committed = true++ log.Printf("committed transaction: (%s)", reason)++ return nil+}diff --git a/app/server/db/utils.go b/app/server/db/utils.goindex 39d8e543..7bdc4bbe 100644--- a/app/server/db/utils.go+++ b/app/server/db/utils.go@@ -1,6 +1,8 @@package db-import "github.com/lib/pq"+import (+ "github.com/lib/pq"+)func IsNonUniqueErr(err error) bool {if err, ok := err.(*pq.Error); ok {diff --git a/app/server/handlers/accounts.go b/app/server/handlers/accounts.goindex a03af9ee..3c4177e6 100644--- a/app/server/handlers/accounts.go+++ b/app/server/handlers/accounts.go@@ -1,8 +1,8 @@package handlersimport (- "database/sql""encoding/json"+ "fmt""io""log""net/http"@@ -13,6 +13,8 @@ import ("strings"shared "plandex-shared"++ "github.com/jmoiron/sqlx")func CreateAccountHandler(w http.ResponseWriter, r *http.Request) {@@ -56,69 +58,45 @@ func CreateAccountHandler(w http.ResponseWriter, r *http.Request) {}}- // start a transaction- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)- return- }-var apiErr *shared.ApiError+ var user *db.User+ var userId string+ var token string+ var orgId string- var committed bool+ err = db.WithTx(r.Context(), "create account", func(tx *sqlx.Tx) error {+ res, err := db.CreateAccount(req.UserName, req.Email, emailVerificationId, tx)- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return+ if err != nil {+ return fmt.Errorf("error creating account: %v", err)}- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")- }- }()+ user = res.User+ userId = user.Id+ token = res.Token+ orgId = res.OrgId- res, err := db.CreateAccount(req.UserName, req.Email, emailVerificationId, tx)+ _, apiErr = hooks.ExecHook(hooks.CreateAccount, hooks.HookParams{+ Auth: &types.ServerAuth{+ User: user,+ OrgId: orgId,+ },+ })- if err != nil {- log.Printf("Error creating account: %v\n", err)- http.Error(w, "Error creating account: "+err.Error(), http.StatusInternalServerError)- return- }-- user := res.User- userId := user.Id- token := res.Token- orgId := res.OrgId-- _, apiErr = hooks.ExecHook(hooks.CreateAccount, hooks.HookParams{- Auth: &types.ServerAuth{- User: user,- OrgId: orgId,- },+ return nil})+if apiErr != nil {writeApiError(w, *apiErr)return}- // commit transaction- err = tx.Commit()if err != nil {- log.Printf("Error committing transaction: %v\n", err)- http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)+ log.Printf("Error creating account: %v\n", err)+ http.Error(w, "Error creating account: "+err.Error(), http.StatusInternalServerError)return}- committed = true-// get orgsorgs, err := db.GetAccessibleOrgsForUser(user)diff --git a/app/server/handlers/auth_helpers.go b/app/server/handlers/auth_helpers.goindex fff4e79b..7f14c8ea 100644--- a/app/server/handlers/auth_helpers.go+++ b/app/server/handlers/auth_helpers.go@@ -1,7 +1,6 @@package handlersimport (- "database/sql""encoding/base64""encoding/json""fmt"@@ -15,6 +14,8 @@ import ("time"shared "plandex-shared"++ "github.com/jmoiron/sqlx")func Authenticate(w http.ResponseWriter, r *http.Request, requireOrg bool) *types.ServerAuth {@@ -346,65 +347,44 @@ func ValidateAndSignIn(w http.ResponseWriter, r *http.Request, req shared.SignIn}}- // start a transaction- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- return nil, fmt.Errorf("error starting transaction: %v", err)- }-- var committed bool+ var token string+ var authTokenId string- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return- }+ err = db.WithTx(r.Context(), "validate and sign in", func(tx *sqlx.Tx) error {+ var err error+ // create auth token+ token, authTokenId, err = db.CreateAuthToken(user.Id, tx)- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")+ if err != nil {+ log.Printf("Error creating auth token: %v\n", err)+ return fmt.Errorf("error creating auth token: %v", err)}- }()-- // create auth token- token, authTokenId, err := db.CreateAuthToken(user.Id, tx)- if err != nil {- log.Printf("Error creating auth token: %v\n", err)- return nil, fmt.Errorf("error creating auth token: %v", err)- }+ if req.IsSignInCode {+ // update sign in code with auth token id+ _, err = tx.Exec("UPDATE sign_in_codes SET auth_token_id = $1 WHERE id = $2", authTokenId, signInCodeId)- if req.IsSignInCode {- // update sign in code with auth token id- _, err = tx.Exec("UPDATE sign_in_codes SET auth_token_id = $1 WHERE id = $2", authTokenId, signInCodeId)+ if err != nil {+ log.Printf("Error updating sign in code: %v\n", err)+ return fmt.Errorf("error updating sign in code: %v", err)+ }+ } else if !isLocalMode { // only update email verification in non-local mode+ // update email verification with user and auth token ids+ _, err = tx.Exec("UPDATE email_verifications SET user_id = $1, auth_token_id = $2 WHERE id = $3", user.Id, authTokenId, emailVerificationId)- if err != nil {- log.Printf("Error updating sign in code: %v\n", err)- return nil, fmt.Errorf("error updating sign in code: %v", err)- }- } else if !isLocalMode { // only update email verification in non-local mode- // update email verification with user and auth token ids- _, err = tx.Exec("UPDATE email_verifications SET user_id = $1, auth_token_id = $2 WHERE id = $3", user.Id, authTokenId, emailVerificationId)+ if err != nil {+ log.Printf("Error updating email verification: %v\n", err)+ return fmt.Errorf("error updating email verification: %v", err)+ }- if err != nil {- log.Printf("Error updating email verification: %v\n", err)- return nil, fmt.Errorf("error updating email verification: %v", err)+ log.Println("Email verification updated")}- log.Println("Email verification updated")- }+ return nil+ })- // commit transaction- err = tx.Commit()if err != nil {- log.Printf("Error committing transaction: %v\n", err)- return nil, fmt.Errorf("error committing transaction: %v", err)+ return nil, fmt.Errorf("error validating and signing in: %v", err)}// get orgs@@ -544,7 +524,7 @@ func execAuthenticate(w http.ResponseWriter, r *http.Request, requireOrg bool, rif invite != nil {log.Println("accepting invite")- err := db.AcceptInvite(invite, authToken.UserId)+ err := db.AcceptInvite(r.Context(), invite, authToken.UserId)if err != nil {log.Printf("error accepting invite: %v\n", err)diff --git a/app/server/handlers/branches.go b/app/server/handlers/branches.goindex a38f3167..6aaf31a7 100644--- a/app/server/handlers/branches.go+++ b/app/server/handlers/branches.go@@ -2,8 +2,8 @@ package handlersimport ("context"- "database/sql""encoding/json"+ "fmt""io""log""net/http"@@ -12,6 +12,7 @@ import (shared "plandex-shared""github.com/gorilla/mux"+ "github.com/jmoiron/sqlx")func ListBranchesHandler(w http.ResponseWriter, r *http.Request) {@@ -34,16 +35,28 @@ func ListBranchesHandler(w http.ResponseWriter, r *http.Request) {var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, false)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }+ var branches []*db.Branch++ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: "main",+ Reason: "list branches",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ res, err := db.ListPlanBranches(repo, planId)++ if err != nil {+ return err+ }++ branches = res- branches, err := db.ListPlanBranches(auth.OrgId, planId)+ return nil+ })if err != nil {log.Printf("Error getting branches: %v\n", err)@@ -110,42 +123,30 @@ func CreateBranchHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }-- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)- return- }-- var committed bool-- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return- }- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: "main",+ Reason: "create branch",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {++ err := db.WithTx(ctx, "create branch", func(tx *sqlx.Tx) error {+ _, err = db.CreateBranch(repo, plan, parentBranch, req.Name, tx)++ if err != nil {+ return fmt.Errorf("error creating branch: %v", err)}- } else {- log.Println("transaction rolled back")- }- }()- _, err = db.CreateBranch(plan, parentBranch, req.Name, tx)+ return nil+ })++ return err+ })if err != nil {log.Printf("Error creating branch: %v\n", err)@@ -153,13 +154,6 @@ func CreateBranchHandler(w http.ResponseWriter, r *http.Request) {return}- // commit the transaction- if err := tx.Commit(); err != nil {- log.Printf("Error committing transaction: %v\n", err)- http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)- return- }-log.Println("Successfully created branch")}@@ -188,32 +182,20 @@ func DeleteBranchHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- repoLockId, err := db.LockRepo(- db.LockRepoParams{- OrgId: auth.OrgId,- UserId: auth.User.Id,- PlanId: planId,- Branch: "main",- Scope: db.LockScopeRead,- Ctx: ctx,- CancelFn: cancel,- },- )-- if err != nil {- log.Printf("Error locking repo: %v\n", err)- http.Error(w, "Error locking repo: "+err.Error(), http.StatusInternalServerError)- return- }-- defer func() {- err := db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- }- }()- err = db.DeleteBranch(auth.OrgId, planId, branch)+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: "main",+ Reason: "delete branch",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ err := repo.GitDeleteBranch(branch)+ return err+ })if err != nil {log.Printf("Error deleting branch: %v\n", err)diff --git a/app/server/handlers/context_helper.go b/app/server/handlers/context_helper.goindex 25deb187..cc836554 100644--- a/app/server/handlers/context_helper.go+++ b/app/server/handlers/context_helper.go@@ -154,28 +154,68 @@ func loadContexts(}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return nil, nil- } else {- defer func() {- (*unlockFn)(err)- }()- }- log.Printf("Calling db.LoadContexts with %d contexts, %d cached maps", len(*loadReq), len(cachedMapsByPath))- for path := range cachedMapsByPath {- log.Printf("Using cached map for path: %s", path)- }+ var loadRes *shared.LoadContextResponse+ var dbContexts []*db.Context++ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: plan.Id,+ Branch: branchName,+ Reason: "load contexts",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ ClearRepoOnErr: true,+ }, func(repo *db.GitRepo) error {+ log.Printf("Calling db.LoadContexts with %d contexts, %d cached maps", len(*loadReq), len(cachedMapsByPath))+ for path := range cachedMapsByPath {+ log.Printf("Using cached map for path: %s", path)+ }++ res, dbContextsRes, err := db.LoadContexts(ctx, db.LoadContextsParams{+ OrgId: auth.OrgId,+ Plan: plan,+ BranchName: branchName,+ Req: loadReq,+ UserId: auth.User.Id,+ CachedMapsByPath: cachedMapsByPath,+ AutoLoaded: autoLoaded,+ })++ if err != nil {+ return err+ }++ loadRes = res+ dbContexts = dbContextsRes++ log.Printf("db.LoadContexts completed successfully, loaded %d contexts", len(dbContexts))++ // Log information about loaded map contexts+ mapContextsCount := 0+ for _, context := range dbContexts {+ if context.ContextType == shared.ContextMapType {+ mapContextsCount+++ log.Printf("Loaded map context: %s, path: %s, tokens: %d", context.Name, context.FilePath, context.NumTokens)+ }+ }+ if mapContextsCount > 0 {+ log.Printf("Successfully loaded %d map contexts out of %d total contexts", mapContextsCount, len(dbContexts))+ }++ if loadRes.MaxTokensExceeded {+ return nil+ }- res, dbContexts, err := db.LoadContexts(ctx, db.LoadContextsParams{- OrgId: auth.OrgId,- Plan: plan,- BranchName: branchName,- Req: loadReq,- UserId: auth.User.Id,- CachedMapsByPath: cachedMapsByPath,- AutoLoaded: autoLoaded,+ err = repo.GitAddAndCommit(branchName, res.Msg)++ if err != nil {+ return fmt.Errorf("error committing changes: %v", err)+ }++ return nil})if err != nil {@@ -184,23 +224,9 @@ func loadContexts(return nil, nil}- log.Printf("db.LoadContexts completed successfully, loaded %d contexts", len(dbContexts))-- // Log information about loaded map contexts- mapContextsCount := 0- for _, context := range dbContexts {- if context.ContextType == shared.ContextMapType {- mapContextsCount++- log.Printf("Loaded map context: %s, path: %s, tokens: %d", context.Name, context.FilePath, context.NumTokens)- }- }- if mapContextsCount > 0 {- log.Printf("Successfully loaded %d map contexts out of %d total contexts", mapContextsCount, len(dbContexts))- }-- if res.MaxTokensExceeded {- log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", res.TotalTokens, res.MaxTokens)- bytes, err := json.Marshal(res)+ if loadRes.MaxTokensExceeded {+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", loadRes.TotalTokens, loadRes.MaxTokens)+ bytes, err := json.Marshal(loadRes)if err != nil {log.Printf("Error marshalling response: %v\n", err)@@ -212,13 +238,5 @@ func loadContexts(return nil, nil}- err = db.GitAddAndCommit(auth.OrgId, plan.Id, branchName, res.Msg)-- if err != nil {- log.Printf("Error committing changes: %v\n", err)- http.Error(w, "Error committing changes: "+err.Error(), http.StatusInternalServerError)- return nil, nil- }-- return res, dbContexts+ return loadRes, dbContexts}diff --git a/app/server/handlers/fs_helper.go b/app/server/handlers/fs_helper.godeleted file mode 100644index 92768007..00000000--- a/app/server/handlers/fs_helper.go+++ /dev/null@@ -1,124 +0,0 @@-package handlers--import (- "context"- "fmt"- "log"- "net/http"- "plandex-server/db"- "plandex-server/types"- "runtime/debug"-- "github.com/gorilla/mux"-)--const locksVerboseLogging = true--func LockRepo(w http.ResponseWriter, r *http.Request, auth *types.ServerAuth, scope db.LockScope, ctx context.Context, cancelFn context.CancelFunc, requireBranch bool) *func(err error) {- vars := mux.Vars(r)- planId := vars["planId"]- branch := vars["branch"]-- if locksVerboseLogging {- log.Printf("LockRepo: %s, planId: %s, branch: %s", scope, planId, branch)- }-- if requireBranch && branch == "" {- if locksVerboseLogging {- log.Println("Branch not specified")- }- http.Error(w, "Branch not specified", http.StatusBadRequest)- return nil- }-- return lockRepo(w, r, auth, scope, ctx, cancelFn, planId, branch)-}--func LockRepoForBranch(w http.ResponseWriter, r *http.Request, auth *types.ServerAuth, scope db.LockScope, ctx context.Context, cancelFn context.CancelFunc, branch string) *func(err error) {- vars := mux.Vars(r)- planId := vars["planId"]-- if locksVerboseLogging {- log.Printf("LockRepoForBranch: %s, planId: %s, branch: %s", scope, planId, branch)- }-- return lockRepo(w, r, auth, scope, ctx, cancelFn, planId, branch)-}--func lockRepo(w http.ResponseWriter, r *http.Request, auth *types.ServerAuth, scope db.LockScope, ctx context.Context, cancelFn context.CancelFunc, planId, branch string) *func(err error) {- params := db.LockRepoParams{- PlanId: planId,- Branch: branch,- Scope: scope,- Ctx: ctx,- CancelFn: cancelFn,- }- if auth == nil {- // error out- log.Println("Auth required")- http.Error(w, "Auth required", http.StatusInternalServerError)- return nil- } else {- params.OrgId = auth.OrgId- if auth.User != nil {- params.UserId = auth.User.Id- }- }-- if locksVerboseLogging {- log.Printf("lockRepo: %s, planId: %s, branch: %s", params.Scope, params.PlanId, params.Branch)- }-- repoLockId, err := db.LockRepo(params)-- if err != nil {- log.Printf("Error locking repo: %v\n", err)- http.Error(w, "Error locking repo: "+err.Error(), http.StatusInternalServerError)- return nil- }-- fn := func(err error) {- log.Println("Unlocking repo in deferred unlock function")- log.Printf("err: %v\n", err)-- if r := recover(); r != nil {- stackTrace := debug.Stack()- log.Printf("Recovered from panic: %v\n", r)- log.Printf("Stack trace: %s\n", stackTrace)- err = fmt.Errorf("server panic: %v", r)- http.Error(w, "Error locking repo: "+err.Error(), http.StatusInternalServerError)- }-- // log.Println("Rolling back repo if error")- err = RollbackRepoIfErr(auth.OrgId, planId, branch, err)- if err != nil {- log.Printf("Error rolling back repo: %v\n", err)- }-- err = db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- }- }-- return &fn-}--func RollbackRepoIfErr(orgId, planId, branch string, err error) error {- // if no error, return nil- if err == nil {- log.Println("No error, not rolling back repo")- return nil- }-- log.Println("Rolling back repo due to error")-- // if any errors, rollback repo- err = db.GitClearUncommittedChanges(orgId, planId, branch)-- if err != nil {- return fmt.Errorf("error clearing uncommitted changes: %v", err)- }-- return nil-}diff --git a/app/server/handlers/invites.go b/app/server/handlers/invites.goindex c57d7e8f..b504ca57 100644--- a/app/server/handlers/invites.go+++ b/app/server/handlers/invites.go@@ -1,8 +1,8 @@package handlersimport (- "database/sql""encoding/json"+ "fmt""log""net/http""os"@@ -13,6 +13,7 @@ import (shared "plandex-shared""github.com/gorilla/mux"+ "github.com/jmoiron/sqlx")func InviteUserHandler(w http.ResponseWriter, r *http.Request) {@@ -124,65 +125,37 @@ func InviteUserHandler(w http.ResponseWriter, r *http.Request) {return}- // start a transaction- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)- return- }+ err = db.WithTx(r.Context(), "invite user", func(tx *sqlx.Tx) error {- var committed bool+ err = db.CreateInvite(&db.Invite{+ OrgId: auth.OrgId,+ OrgRoleId: req.OrgRoleId,+ Email: req.Email,+ Name: req.Name,+ InviterId: currentUserId,+ }, tx)- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return- }-- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")+ if err != nil {+ log.Printf("Error creating invite: %v\n", err)+ return fmt.Errorf("error creating invite: %v", err)}- }()- err = db.CreateInvite(&db.Invite{- OrgId: auth.OrgId,- OrgRoleId: req.OrgRoleId,- Email: req.Email,- Name: req.Name,- InviterId: currentUserId,- }, tx)+ err = email.SendInviteEmail(req.Email, req.Name, auth.User.Name, org.Name)- if err != nil {- log.Printf("Error creating invite: %v\n", err)- http.Error(w, "Error creating invite: "+err.Error(), http.StatusInternalServerError)- return- }-- err = email.SendInviteEmail(req.Email, req.Name, auth.User.Name, org.Name)+ if err != nil {+ log.Printf("Error sending invite email: %v\n", err)+ return fmt.Errorf("error sending invite email: %v", err)+ }- if err != nil {- log.Printf("Error sending invite email: %v\n", err)- http.Error(w, "Error sending invite email: "+err.Error(), http.StatusInternalServerError)- return- }+ return nil+ })- // commit transaction- err = tx.Commit()if err != nil {- log.Printf("Error committing transaction: %v\n", err)- http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)+ log.Printf("Error inviting user: %v\n", err)+ http.Error(w, "Error inviting user: "+err.Error(), http.StatusInternalServerError)return}- committed = true-log.Println("Successfully created invite")}diff --git a/app/server/handlers/orgs.go b/app/server/handlers/orgs.goindex bdc22f19..2f4318cd 100644--- a/app/server/handlers/orgs.go+++ b/app/server/handlers/orgs.go@@ -1,8 +1,8 @@package handlersimport (- "database/sql""encoding/json"+ "fmt""io""log""net/http"@@ -11,6 +11,8 @@ import ("plandex-server/hooks"shared "plandex-shared"++ "github.com/jmoiron/sqlx")func ListOrgsHandler(w http.ResponseWriter, r *http.Request) {@@ -84,82 +86,56 @@ func CreateOrgHandler(w http.ResponseWriter, r *http.Request) {}var apiErr *shared.ApiError+ var org *db.Org+ err = db.WithTx(r.Context(), "create org", func(tx *sqlx.Tx) error {+ var err error+ var domain *string+ if req.AutoAddDomainUsers {+ if shared.IsEmailServiceDomain(auth.User.Domain) {+ log.Printf("Invalid domain: %v\n", auth.User.Domain)+ return fmt.Errorf("invalid domain: %v", auth.User.Domain)+ }- // start a transaction- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)- return- }+ domain = &auth.User.Domain+ }- var committed bool+ // create a new org+ org, err = db.CreateOrg(&req, auth.AuthToken.UserId, domain, tx)- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return+ if err != nil {+ log.Printf("Error creating org: %v\n", err)+ return fmt.Errorf("error creating org: %v", err)}- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)+ if org.AutoAddDomainUsers && org.Domain != nil {+ err = db.AddOrgDomainUsers(org.Id, *org.Domain, tx)++ if err != nil {+ log.Printf("Error adding org domain users: %v\n", err)+ return fmt.Errorf("error adding org domain users: %v", err)}- } else {- log.Println("transaction rolled back")- }- }()-- var domain *string- if req.AutoAddDomainUsers {- if shared.IsEmailServiceDomain(auth.User.Domain) {- log.Printf("Invalid domain: %v\n", auth.User.Domain)- http.Error(w, "Invalid domain: "+auth.User.Domain, http.StatusBadRequest)- return}- domain = &auth.User.Domain- }-- // create a new org- org, err := db.CreateOrg(&req, auth.AuthToken.UserId, domain, tx)+ _, apiErr = hooks.ExecHook(hooks.CreateOrg, hooks.HookParams{+ Auth: auth,+ Tx: tx,- if err != nil {- log.Printf("Error creating org: %v\n", err)- http.Error(w, "Error creating org: "+err.Error(), http.StatusInternalServerError)- return- }-- if org.AutoAddDomainUsers && org.Domain != nil {- err = db.AddOrgDomainUsers(org.Id, *org.Domain, tx)-- if err != nil {- log.Printf("Error adding org domain users: %v\n", err)- http.Error(w, "Error adding org domain users: "+err.Error(), http.StatusInternalServerError)- return- }- }-- _, apiErr = hooks.ExecHook(hooks.CreateOrg, hooks.HookParams{- Auth: auth,- Tx: tx,+ CreateOrgHookRequestParams: &hooks.CreateOrgHookRequestParams{+ Org: org,+ },+ })- CreateOrgHookRequestParams: &hooks.CreateOrgHookRequestParams{- Org: org,- },+ return nil})+if apiErr != nil {writeApiError(w, *apiErr)return}- err = tx.Commit()-if err != nil {- log.Printf("Error committing transaction: %v\n", err)- http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)+ log.Printf("Error creating org: %v\n", err)+ http.Error(w, "Error creating org: "+err.Error(), http.StatusInternalServerError)return}diff --git a/app/server/handlers/plan_config.go b/app/server/handlers/plan_config.goindex 1cc32dc7..dc101e44 100644--- a/app/server/handlers/plan_config.go+++ b/app/server/handlers/plan_config.go@@ -2,6 +2,7 @@ package handlersimport ("encoding/json"+ "fmt""log""net/http""plandex-server/db"@@ -9,6 +10,7 @@ import (shared "plandex-shared""github.com/gorilla/mux"+ "github.com/jmoiron/sqlx")func GetPlanConfigHandler(w http.ResponseWriter, r *http.Request) {@@ -133,32 +135,20 @@ func UpdateDefaultPlanConfigHandler(w http.ResponseWriter, r *http.Request) {return}- tx, err := db.Conn.Beginx()- if err != nil {- log.Println("Error starting transaction: ", err)- http.Error(w, "Error starting transaction", http.StatusInternalServerError)- return- }+ err = db.WithTx(r.Context(), "update default plan config", func(tx *sqlx.Tx) error {- defer func() {+ err := db.StoreDefaultPlanConfig(auth.User.Id, req.Config, tx)if err != nil {- if rbErr := tx.Rollback(); rbErr != nil {- log.Printf("Error rolling back transaction: %v\n", rbErr)- }+ log.Println("Error storing default plan config: ", err)+ return fmt.Errorf("error storing default plan config: %v", err)}- }()- err = db.StoreDefaultPlanConfig(auth.User.Id, req.Config, tx)- if err != nil {- log.Println("Error storing default plan config: ", err)- http.Error(w, "Error storing default plan config", http.StatusInternalServerError)- return- }+ return nil+ })- err = tx.Commit()if err != nil {- log.Println("Error committing transaction: ", err)- http.Error(w, "Error committing transaction", http.StatusInternalServerError)+ log.Println("Error updating default plan config: ", err)+ http.Error(w, "Error updating default plan config", http.StatusInternalServerError)return}diff --git a/app/server/handlers/plans_changes.go b/app/server/handlers/plans_changes.goindex f24384a6..ac32ae5a 100644--- a/app/server/handlers/plans_changes.go+++ b/app/server/handlers/plans_changes.go@@ -38,44 +38,49 @@ func CurrentPlanHandler(w http.ResponseWriter, r *http.Request) {// Just in case this was sent immediately after a stream finished, wait a little before locking to allow for cleanuptime.Sleep(100 * time.Millisecond)- var err error-ctx, cancel := context.WithCancel(r.Context())scope := db.LockScopeRead- requireBranch := trueif sha != "" {scope = db.LockScopeWrite- requireBranch = false}log.Printf("locking with scope: %s", scope)- unlockFn := LockRepo(w, r, auth, scope, ctx, cancel, requireBranch)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- if sha != "" {- err = db.GitCheckoutSha(auth.OrgId, planId, sha)- if err != nil {- log.Printf("Error checking out sha: %v\n", err)- http.Error(w, "Error checking out sha: "+err.Error(), http.StatusInternalServerError)- return+ var planState *shared.CurrentPlanState++ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: scope,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ var err error+ if sha != "" {+ err = repo.GitCheckoutSha(sha)+ if err != nil {+ return fmt.Errorf("error checking out sha: %v", err)+ }++ defer func() {+ checkoutErr := repo.GitCheckoutBranch(branch)+ if checkoutErr != nil {+ log.Printf("Error checking out branch: %v\n", checkoutErr)+ }+ }()}- defer func() {- checkoutErr := db.GitCheckoutBranch(auth.OrgId, planId, branch)- if checkoutErr != nil {- log.Printf("Error checking out branch: %v\n", checkoutErr)- }- }()- }+ planState, err = db.GetCurrentPlanState(db.CurrentPlanStateParams{+ OrgId: auth.OrgId,+ PlanId: planId,+ })- planState, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{- OrgId: auth.OrgId,- PlanId: planId,+ if err != nil {+ return fmt.Errorf("error getting current plan state: %v", err)+ }++ return nil})if err != nil {@@ -137,30 +142,38 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {time.Sleep(100 * time.Millisecond)ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- settings, err := db.GetPlanSettings(plan, true)- if err != nil {- log.Printf("Error getting plan settings: %v\n", err)- http.Error(w, "Error getting plan settings: "+err.Error(), http.StatusInternalServerError)- return- }+ var settings *shared.PlanSettings+ var currentPlanParams db.CurrentPlanStateParams+ var currentPlan *shared.CurrentPlanState++ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ var err error+ settings, err = db.GetPlanSettings(plan, true)+ if err != nil {+ return fmt.Errorf("error getting plan settings: %v", err)+ }- currentPlanParams, err := db.GetFullCurrentPlanStateParams(auth.OrgId, planId)- if err != nil {- log.Printf("Error getting current plan state params: %v\n", err)- http.Error(w, "Error getting current plan state params: "+err.Error(), http.StatusInternalServerError)- return- }+ currentPlanParams, err = db.GetFullCurrentPlanStateParams(auth.OrgId, planId)+ if err != nil {+ return fmt.Errorf("error getting current plan state params: %v", err)+ }- currentPlan, err := db.GetCurrentPlanState(currentPlanParams)+ currentPlan, err = db.GetCurrentPlanState(currentPlanParams)+ if err != nil {+ return fmt.Errorf("error getting current plan state: %v", err)+ }++ return nil+ })if err != nil {log.Printf("Error getting current plan state: %v\n", err)@@ -189,14 +202,25 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {return}- err = db.ApplyPlan(ctx, db.ApplyPlanParams{- OrgId: auth.OrgId,- UserId: auth.User.Id,- BranchName: branch,- Plan: plan,- CurrentPlanState: currentPlan,- CurrentPlanStateParams: ¤tPlanParams,- CommitMsg: commitMsg,+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ ClearRepoOnErr: true,+ }, func(repo *db.GitRepo) error {+ return db.ApplyPlan(repo, ctx, db.ApplyPlanParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ BranchName: branch,+ Plan: plan,+ CurrentPlanState: currentPlan,+ CurrentPlanStateParams: ¤tPlanParams,+ CommitMsg: commitMsg,+ })})if err != nil {@@ -227,30 +251,34 @@ func RejectAllChangesHandler(w http.ResponseWriter, r *http.Request) {return}- var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- err = db.RejectAllResults(auth.OrgId, planId)+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ ClearRepoOnErr: true,+ }, func(repo *db.GitRepo) error {+ err := db.RejectAllResults(auth.OrgId, planId)+ if err != nil {+ return err+ }- if err != nil {- log.Printf("Error rejecting all changes: %v\n", err)- http.Error(w, "Error rejecting all changes: "+err.Error(), http.StatusInternalServerError)- return- }+ err = repo.GitAddAndCommit(branch, "🚫 Rejected all pending changes")+ if err != nil {+ return fmt.Errorf("error committing rejected changes: %v", err)+ }- err = db.GitAddAndCommit(auth.OrgId, planId, branch, "🚫 Rejected all pending changes")+ return nil+ })if err != nil {- log.Printf("Error committing rejected changes: %v\n", err)- http.Error(w, "Error committing rejected changes: "+err.Error(), http.StatusInternalServerError)+ log.Printf("Error rejecting all changes: %v\n", err)+ http.Error(w, "Error rejecting all changes: "+err.Error(), http.StatusInternalServerError)return}@@ -284,28 +312,33 @@ func RejectFileHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- err = db.RejectPlanFile(auth.OrgId, planId, req.FilePath, time.Now())+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ ClearRepoOnErr: true,+ }, func(repo *db.GitRepo) error {+ err = db.RejectPlanFile(auth.OrgId, planId, req.FilePath, time.Now())+ if err != nil {+ return err+ }- if err != nil {- log.Printf("Error rejecting result: %v\n", err)- http.Error(w, "Error rejecting result: "+err.Error(), http.StatusInternalServerError)- return- }+ err = repo.GitAddAndCommit(branch, fmt.Sprintf("🚫 Rejected pending changes to file: %s", req.FilePath))+ if err != nil {+ return fmt.Errorf("error committing rejected changes: %v", err)+ }- err = db.GitAddAndCommit(auth.OrgId, planId, branch, fmt.Sprintf("🚫 Rejected pending changes to file: %s", req.FilePath))+ return nil+ })if err != nil {- log.Printf("Error committing rejected changes: %v\n", err)- http.Error(w, "Error committing rejected changes: "+err.Error(), http.StatusInternalServerError)+ log.Printf("Error rejecting result: %v\n", err)+ http.Error(w, "Error rejecting result: "+err.Error(), http.StatusInternalServerError)return}@@ -339,37 +372,43 @@ func RejectFilesHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- err = db.RejectPlanFiles(auth.OrgId, planId, req.Paths, time.Now())+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ ClearRepoOnErr: true,+ }, func(repo *db.GitRepo) error {+ err = db.RejectPlanFiles(auth.OrgId, planId, req.Paths, time.Now())+ if err != nil {+ return err+ }- if err != nil {- log.Printf("Error rejecting result: %v\n", err)- http.Error(w, "Error rejecting result: "+err.Error(), http.StatusInternalServerError)- return- }+ msg := "🚫 Rejected pending changes to file"+ if len(req.Paths) > 1 {+ msg += "s"+ }+ msg += ":"- msg := "🚫 Rejected pending changes to file"- if len(req.Paths) > 1 {- msg += "s"- }- msg += ":"+ for _, path := range req.Paths {+ msg += fmt.Sprintf("\n • %s", path)+ }- for _, path := range req.Paths {- msg += fmt.Sprintf("\n • %s", path)- }- err = db.GitAddAndCommit(auth.OrgId, planId, branch, msg)+ err = repo.GitAddAndCommit(branch, msg)+ if err != nil {+ return fmt.Errorf("error committing rejected changes: %v", err)+ }++ return nil+ })if err != nil {- log.Printf("Error committing rejected changes: %v\n", err)- http.Error(w, "Error committing rejected changes: "+err.Error(), http.StatusInternalServerError)+ log.Printf("Error rejecting result: %v\n", err)+ http.Error(w, "Error rejecting result: "+err.Error(), http.StatusInternalServerError)return}@@ -491,18 +530,26 @@ func GetPlanDiffsHandler(w http.ResponseWriter, r *http.Request) {return}- var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }+ var diffs string++ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ var err error+ diffs, err = db.GetPlanDiffs(auth.OrgId, planId, plain)+ if err != nil {+ return err+ }- diffs, err := db.GetPlanDiffs(auth.OrgId, planId, plain)+ return nil+ })if err != nil {log.Printf("Error getting plan diffs: %v\n", err)diff --git a/app/server/handlers/plans_context.go b/app/server/handlers/plans_context.goindex 04600f62..2a378824 100644--- a/app/server/handlers/plans_context.go+++ b/app/server/handlers/plans_context.go@@ -3,6 +3,7 @@ package handlersimport ("context""encoding/json"+ "fmt""io""log""net/http"@@ -23,24 +24,35 @@ func ListContextHandler(w http.ResponseWriter, r *http.Request) {vars := mux.Vars(r)planId := vars["planId"]- log.Println("planId: ", planId)+ branch := vars["branch"]+ log.Println("planId: ", planId, "branch: ", branch)if authorizePlan(w, planId, auth) == nil {return}- var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }+ var dbContexts []*db.Context++ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "list contexts",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ res, err := db.GetPlanContexts(auth.OrgId, planId, false, false)+ if err != nil {+ return err+ }- dbContexts, err := db.GetPlanContexts(auth.OrgId, planId, false, false)+ dbContexts = res++ return nil+ })if err != nil {log.Printf("Error getting contexts: %v\n", err)@@ -75,25 +87,37 @@ func GetContextBodyHandler(w http.ResponseWriter, r *http.Request) {vars := mux.Vars(r)planId := vars["planId"]+ branch := vars["branch"]contextId := vars["contextId"]- log.Println("planId:", planId, "contextId:", contextId)+ log.Println("planId:", planId, "branch:", branch, "contextId:", contextId)if authorizePlan(w, planId, auth) == nil {return}- var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- dbContexts, err := db.GetPlanContexts(auth.OrgId, planId, true, false)+ var dbContexts []*db.Context+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "get context body",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ res, err := db.GetPlanContexts(auth.OrgId, planId, true, false)+ if err != nil {+ return err+ }++ dbContexts = res++ return nil+ })+if err != nil {log.Printf("Error getting contexts: %v\n", err)http.Error(w, "Error getting contexts: "+err.Error(), http.StatusInternalServerError)@@ -222,20 +246,42 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- updateRes, err := db.UpdateContexts(db.UpdateContextsParams{- Req: &requestBody,- OrgId: auth.OrgId,- Plan: plan,- BranchName: branchName,+ var updateRes *shared.UpdateContextResponse+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branchName,+ Reason: "update contexts",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ ClearRepoOnErr: true,+ }, func(repo *db.GitRepo) error {+ var err error+ updateRes, err = db.UpdateContexts(db.UpdateContextsParams{+ Req: &requestBody,+ OrgId: auth.OrgId,+ Plan: plan,+ BranchName: branchName,+ })++ if err != nil {+ return err+ }++ if updateRes.MaxTokensExceeded {+ return nil+ }++ err = repo.GitAddAndCommit(branchName, updateRes.Msg)++ if err != nil {+ return fmt.Errorf("error committing changes: %v", err)+ }++ return nil})if err != nil {@@ -258,14 +304,6 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {return}- err = db.GitAddAndCommit(auth.OrgId, planId, branchName, updateRes.Msg)-- if err != nil {- log.Printf("Error committing changes: %v\n", err)- http.Error(w, "Error committing changes: "+err.Error(), http.StatusInternalServerError)- return- }-bytes, err := json.Marshal(updateRes)if err != nil {@@ -323,51 +361,62 @@ func DeleteContextHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- dbContexts, err := db.GetPlanContexts(auth.OrgId, planId, false, false)+ var dbContexts []*db.Context+ var toRemove []*db.Context+ var commitMsg string+ removeTokens := 0+ var toRemoveApiContexts []*shared.Context- if err != nil {- log.Printf("Error getting contexts: %v\n", err)- http.Error(w, "Error getting contexts: "+err.Error(), http.StatusInternalServerError)- return- }+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branchName,+ Reason: "delete contexts",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ ClearRepoOnErr: true,+ }, func(repo *db.GitRepo) error {+ var err error+ dbContexts, err = db.GetPlanContexts(auth.OrgId, planId, false, false)- var toRemove []*db.Context- for _, dbContext := range dbContexts {- if _, ok := requestBody.Ids[dbContext.Id]; ok {- toRemove = append(toRemove, dbContext)+ if err != nil {+ return fmt.Errorf("error getting contexts: %v", err)}- }- err = db.ContextRemove(auth.OrgId, planId, toRemove)+ for _, dbContext := range dbContexts {+ if _, ok := requestBody.Ids[dbContext.Id]; ok {+ toRemove = append(toRemove, dbContext)+ }+ }- if err != nil {- log.Printf("Error deleting contexts: %v\n", err)- http.Error(w, "Error deleting contexts: "+err.Error(), http.StatusInternalServerError)- return- }+ err = db.ContextRemove(auth.OrgId, planId, toRemove)- removeTokens := 0- var toRemoveApiContexts []*shared.Context- for _, dbContext := range toRemove {- toRemoveApiContexts = append(toRemoveApiContexts, dbContext.ToApi())- removeTokens += dbContext.NumTokens- }+ if err != nil {+ return fmt.Errorf("error removing contexts: %v", err)+ }++ for _, dbContext := range toRemove {+ toRemoveApiContexts = append(toRemoveApiContexts, dbContext.ToApi())+ removeTokens += dbContext.NumTokens+ }- commitMsg := shared.SummaryForRemoveContext(toRemoveApiContexts, branch.ContextTokens) + "\n\n" + shared.TableForRemoveContext(toRemoveApiContexts)- err = db.GitAddAndCommit(auth.OrgId, planId, branchName, commitMsg)+ commitMsg = shared.SummaryForRemoveContext(toRemoveApiContexts, branch.ContextTokens) + "\n\n" + shared.TableForRemoveContext(toRemoveApiContexts)++ err = repo.GitAddAndCommit(branchName, commitMsg)++ if err != nil {+ return fmt.Errorf("error committing changes: %v", err)+ }++ return nil+ })if err != nil {- log.Printf("Error committing changes: %v\n", err)- http.Error(w, "Error committing changes: "+err.Error(), http.StatusInternalServerError)+ log.Printf("Error deleting contexts: %v\n", err)+ http.Error(w, "Error deleting contexts: "+err.Error(), http.StatusInternalServerError)return}diff --git a/app/server/handlers/plans_convo.go b/app/server/handlers/plans_convo.goindex 8aa22d9c..ea077f0e 100644--- a/app/server/handlers/plans_convo.go+++ b/app/server/handlers/plans_convo.go@@ -21,25 +21,38 @@ func ListConvoHandler(w http.ResponseWriter, r *http.Request) {vars := mux.Vars(r)planId := vars["planId"]-- log.Println("planId: ", planId)+ branch := vars["branch"]+ log.Println("planId: ", planId, "branch: ", branch)if authorizePlan(w, planId, auth) == nil {return}var err error+ var convoMessages []*db.ConvoMessage+ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- convoMessages, err := db.GetPlanConvo(auth.OrgId, planId)+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "list convo",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ res, err := db.GetPlanConvo(auth.OrgId, planId)++ if err != nil {+ return err+ }++ convoMessages = res++ return nil+ })if err != nil {log.Println("Error getting plan convo: ", err)@@ -84,18 +97,29 @@ func GetPlanStatusHandler(w http.ResponseWriter, r *http.Request) {return}- var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- convoMessages, err := db.GetPlanConvo(auth.OrgId, planId)+ var convoMessages []*db.ConvoMessage+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "get plan status",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ res, err := db.GetPlanConvo(auth.OrgId, planId)++ if err != nil {+ return err+ }++ convoMessages = res++ return nil+ })if err != nil {log.Println("Error getting plan convo: ", err)diff --git a/app/server/handlers/plans_crud.go b/app/server/handlers/plans_crud.goindex d4a41a7c..32774b63 100644--- a/app/server/handlers/plans_crud.go+++ b/app/server/handlers/plans_crud.go@@ -98,7 +98,7 @@ func CreatePlanHandler(w http.ResponseWriter, r *http.Request) {}}- plan, err := db.CreatePlan(auth.OrgId, projectId, auth.User.Id, name)+ plan, err := db.CreatePlan(r.Context(), auth.OrgId, projectId, auth.User.Id, name)if err != nil {log.Printf("Error creating plan: %v\n", err)diff --git a/app/server/handlers/plans_exec.go b/app/server/handlers/plans_exec.goindex 537b862e..86702b73 100644--- a/app/server/handlers/plans_exec.go+++ b/app/server/handlers/plans_exec.go@@ -242,29 +242,32 @@ func StopPlanHandler(w http.ResponseWriter, r *http.Request) {var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- if err == nil {- err = modelPlan.Stop(planId, branch, auth.User.Id, auth.OrgId)+ // this is here to ensure that the plan is stopped even if the db operation fails+ defer func() {+ err = modelPlan.Stop(planId, branch, auth.User.Id, auth.OrgId)- if err != nil {- log.Printf("Error stopping plan: %v\n", err)- http.Error(w, "Error stopping plan", http.StatusInternalServerError)- return- }+ if err != nil {+ log.Printf("Error stopping plan: %v\n", err)+ }- log.Println("Successfully processed request for StopPlanHandler")- }- }()- }+ log.Println("Successfully processed request for StopPlanHandler")+ }()- log.Println("Stopping plan")- err = modelPlan.StorePartialReply(planId, branch, auth.User.Id, auth.OrgId)+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "stop plan",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ log.Println("Stopping plan")+ err = modelPlan.StorePartialReply(repo, planId, branch, auth.User.Id, auth.OrgId)+ return err+ })if err != nil {log.Printf("Error storing partial reply: %v\n", err)diff --git a/app/server/handlers/plans_versions.go b/app/server/handlers/plans_versions.goindex 4ac63390..ae19fe05 100644--- a/app/server/handlers/plans_versions.go+++ b/app/server/handlers/plans_versions.go@@ -31,18 +31,29 @@ func ListLogsHandler(w http.ResponseWriter, r *http.Request) {return}- var err errorctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- body, shas, err := db.GetGitCommitHistory(auth.OrgId, planId, branch)+ var body string+ var shas []string++ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "list logs",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ var err error+ body, shas, err = repo.GetGitCommitHistory(branch)+ if err != nil {+ return err+ }++ return nil+ })if err != nil {log.Println("Error getting logs: ", err)@@ -103,16 +114,19 @@ func RewindPlanHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- err = db.GitRewindToSha(auth.OrgId, planId, branch, requestBody.Sha)+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "rewind plan",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ return repo.GitRewindToSha(branch, requestBody.Sha)+ })if err != nil {log.Println("Error rewinding plan: ", err)@@ -128,7 +142,22 @@ func RewindPlanHandler(w http.ResponseWriter, r *http.Request) {return}- sha, latest, err := db.GetLatestCommit(auth.OrgId, planId, branch)+ var sha string+ var latest string++ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "get latest commit",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ sha, latest, err = repo.GetLatestCommit(branch)+ return err+ })if err != nil {log.Println("Error getting latest commit: ", err)diff --git a/app/server/handlers/projects.go b/app/server/handlers/projects.goindex 4b7818ea..80337c43 100644--- a/app/server/handlers/projects.go+++ b/app/server/handlers/projects.go@@ -1,8 +1,8 @@package handlersimport (- "database/sql""encoding/json"+ "fmt""io""log""net/http"@@ -11,6 +11,7 @@ import (shared "plandex-shared""github.com/gorilla/mux"+ "github.com/jmoiron/sqlx")func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {@@ -43,34 +44,19 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {return}- // start a transaction- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)- return- }+ var projectId string+ err = db.WithTx(r.Context(), "create project", func(tx *sqlx.Tx) error {+ var err error- var committed bool-- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return- }+ projectId, err = db.CreateProject(auth.OrgId, requestBody.Name, tx)- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")+ if err != nil {+ log.Printf("Error creating project: %v\n", err)+ return fmt.Errorf("error creating project: %v", err)}- }()- projectId, err := db.CreateProject(auth.OrgId, requestBody.Name, tx)+ return nil+ })if err != nil {log.Printf("Error creating project: %v\n", err)@@ -78,13 +64,6 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {return}- err = tx.Commit()- if err != nil {- log.Printf("Error committing transaction: %v\n", err)- http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)- return- }-resp := shared.CreateProjectResponse{Id: projectId,}diff --git a/app/server/handlers/settings.go b/app/server/handlers/settings.goindex d777dc32..ab6bccd6 100644--- a/app/server/handlers/settings.go+++ b/app/server/handlers/settings.go@@ -2,7 +2,6 @@ package handlersimport ("context"- "database/sql""encoding/json""fmt""log"@@ -13,6 +12,7 @@ import (shared "plandex-shared""github.com/gorilla/mux"+ "github.com/jmoiron/sqlx")func GetSettingsHandler(w http.ResponseWriter, r *http.Request) {@@ -34,18 +34,28 @@ func GetSettingsHandler(w http.ResponseWriter, r *http.Request) {return}- var err error+ var settings *shared.PlanSettingsctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- settings, err := db.GetPlanSettings(plan, true)+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "get settings",+ Scope: db.LockScopeRead,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ res, err := db.GetPlanSettings(plan, true)+ if err != nil {+ return err+ }++ settings = res++ return nil+ })if err != nil {log.Println("Error getting settings: ", err)@@ -96,44 +106,51 @@ func UpdateSettingsHandler(w http.ResponseWriter, r *http.Request) {}ctx, cancel := context.WithCancel(r.Context())- unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)- if unlockFn == nil {- return- } else {- defer func() {- (*unlockFn)(err)- }()- }- originalSettings, err := db.GetPlanSettings(plan, true)+ var commitMsg string++ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Reason: "update settings",+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancel,+ }, func(repo *db.GitRepo) error {+ originalSettings, err := db.GetPlanSettings(plan, true)++ if err != nil {+ return fmt.Errorf("error getting settings: %v", err)+ }- if err != nil {- log.Println("Error getting settings: ", err)- http.Error(w, "Error getting settings", http.StatusInternalServerError)- return- }+ // log.Println("Original settings:")+ // spew.Dump(originalSettings)- // log.Println("Original settings:")- // spew.Dump(originalSettings)+ // log.Println("req.Settings:")+ // spew.Dump(req.Settings)- // log.Println("req.Settings:")- // spew.Dump(req.Settings)+ err = db.StorePlanSettings(plan, req.Settings)- err = db.StorePlanSettings(plan, req.Settings)+ if err != nil {+ return fmt.Errorf("error storing settings: %v", err)+ }- if err != nil {- log.Println("Error storing settings: ", err)- http.Error(w, "Error storing settings", http.StatusInternalServerError)- return- }+ commitMsg = getUpdateCommitMsg(req.Settings, originalSettings, false)- commitMsg := getUpdateCommitMsg(req.Settings, originalSettings, false)+ err = repo.GitAddAndCommit(branch, commitMsg)- err = db.GitAddAndCommit(auth.OrgId, planId, branch, commitMsg)+ if err != nil {+ return fmt.Errorf("error committing settings: %v", err)+ }++ return nil+ })if err != nil {- log.Println("Error committing settings: ", err)- http.Error(w, "Error committing settings", http.StatusInternalServerError)+ log.Println("Error updating settings: ", err)+ http.Error(w, "Error updating settings", http.StatusInternalServerError)return}@@ -201,66 +218,43 @@ func UpdateDefaultSettingsHandler(w http.ResponseWriter, r *http.Request) {return}- tx, err := db.Conn.Beginx()+ var originalSettings *shared.PlanSettings- if err != nil {- log.Println("Error starting transaction: ", err)- http.Error(w, "Error starting transaction", http.StatusInternalServerError)- return- }- var committed bool+ err = db.WithTx(r.Context(), "update default settings", func(tx *sqlx.Tx) error {+ var err error- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return- }+ originalSettings, err = db.GetOrgDefaultSettingsForUpdate(auth.OrgId, tx, true)- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")+ if err != nil {+ log.Println("Error getting default settings: ", err)+ return fmt.Errorf("error getting default settings: %v", err)}- }()-- originalSettings, err := db.GetOrgDefaultSettingsForUpdate(auth.OrgId, tx, true)-- if err != nil {- log.Println("Error getting default settings: ", err)- http.Error(w, "Error getting default settings", http.StatusInternalServerError)- return- }- // log.Println("Original settings:")- // spew.Dump(originalSettings)+ // log.Println("Original settings:")+ // spew.Dump(originalSettings)- // log.Println("req.Settings:")- // spew.Dump(req.Settings)+ // log.Println("req.Settings:")+ // spew.Dump(req.Settings)- if !req.Settings.UpdatedAt.Equal(originalSettings.UpdatedAt) {- err = fmt.Errorf("default settings have been updated since you last fetched them")- log.Println("Error updating default settings: ", err)- http.Error(w, "Error updating default settings: "+err.Error(), http.StatusBadRequest)- return- }+ if !req.Settings.UpdatedAt.Equal(originalSettings.UpdatedAt) {+ err = fmt.Errorf("default settings have been updated since you last fetched them")+ log.Println("Error updating default settings: ", err)+ return fmt.Errorf("error updating default settings: %v", err)+ }- err = db.StoreOrgDefaultSettings(auth.OrgId, req.Settings, tx)+ err = db.StoreOrgDefaultSettings(auth.OrgId, req.Settings, tx)- if err != nil {- log.Println("Error storing default settings: ", err)- http.Error(w, "Error storing default settings: "+err.Error(), http.StatusInternalServerError)- return- }+ if err != nil {+ log.Println("Error storing default settings: ", err)+ return fmt.Errorf("error storing default settings: %v", err)+ }- err = tx.Commit()+ return nil+ })if err != nil {- log.Println("Error committing transaction: ", err)- http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)+ log.Println("Error updating default settings: ", err)+ http.Error(w, "Error updating default settings", http.StatusInternalServerError)return}diff --git a/app/server/handlers/users.go b/app/server/handlers/users.goindex 60f62020..dee22a34 100644--- a/app/server/handlers/users.go+++ b/app/server/handlers/users.go@@ -1,8 +1,8 @@package handlersimport (- "database/sql""encoding/json"+ "fmt""log""net/http""os"@@ -12,6 +12,7 @@ import (shared "plandex-shared""github.com/gorilla/mux"+ "github.com/jmoiron/sqlx")func ListUsersHandler(w http.ResponseWriter, r *http.Request) {@@ -184,64 +185,37 @@ func DeleteOrgUserHandler(w http.ResponseWriter, r *http.Request) {}}- // start a transaction- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)- return- }+ err = db.WithTx(r.Context(), "delete org user", func(tx *sqlx.Tx) error {- var committed bool+ err = db.DeleteOrgUser(auth.OrgId, userId, tx)- // Ensure that rollback is attempted in case of failure- defer func() {- if committed {- return- }-- if rbErr := tx.Rollback(); rbErr != nil {- if rbErr == sql.ErrTxDone {- // log.Println("attempted to roll back transaction, but it was already committed")- } else {- log.Printf("transaction rollback error: %v\n", rbErr)- }- } else {- log.Println("transaction rolled back")+ if err != nil {+ log.Println("Error deleting org user: ", err)+ return fmt.Errorf("error deleting org user: %v", err)}- }()-- err = db.DeleteOrgUser(auth.OrgId, userId, tx)-- if err != nil {- log.Println("Error deleting org user: ", err)- http.Error(w, "Error deleting org user: "+err.Error(), http.StatusInternalServerError)- return- }- invite, err := db.GetActiveInviteByEmail(auth.OrgId, auth.User.Email)+ invite, err := db.GetActiveInviteByEmail(auth.OrgId, auth.User.Email)- if err != nil {- log.Println("Error getting invite for org user: ", err)- http.Error(w, "Error getting invite for org user: "+err.Error(), http.StatusInternalServerError)- return- }+ if err != nil {+ log.Println("Error getting invite for org user: ", err)+ return fmt.Errorf("error getting invite for org user: %v", err)+ }- if invite != nil {- err = db.DeleteInvite(invite.Id, tx)+ if invite != nil {+ err = db.DeleteInvite(invite.Id, tx)- if err != nil {- log.Println("Error deleting invite: ", err)- http.Error(w, "Error deleting invite: "+err.Error(), http.StatusInternalServerError)- return+ if err != nil {+ log.Println("Error deleting invite: ", err)+ return fmt.Errorf("error deleting invite: %v", err)+ }}- }- err = tx.Commit()+ return nil+ })if err != nil {- log.Println("Error committing transaction: ", err)- http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)+ log.Println("Error deleting org user: ", err)+ http.Error(w, "Error deleting org user: "+err.Error(), http.StatusInternalServerError)return}diff --git a/app/server/model/client_stream.go b/app/server/model/client_stream.goindex 1ccb2a3b..174b4e24 100644--- a/app/server/model/client_stream.go+++ b/app/server/model/client_stream.go@@ -79,7 +79,7 @@ func processChatCompletionStream(return accumulator.Result(false, nil), nil} else {log.Println("Stream timed out due to inactivity")- return accumulator.Result(true, fmt.Errorf("stream timed out due to inactivity. This usually means the model is not responding.")), nil+ return accumulator.Result(true, fmt.Errorf("stream timed out due to inactivity. The model is not responding.")), nil}default:response, err := stream.Recv()@@ -123,7 +123,7 @@ func processChatCompletionStream(}if len(response.Choices) > 1 {- err = fmt.Errorf("stream finished with more than one choice | This usually means the model failed to generate a valid response.")+ err = fmt.Errorf("stream finished with more than one choice | The model failed to generate a valid response.")return accumulator.Result(true, err), err}@@ -131,7 +131,7 @@ func processChatCompletionStream(if choice.FinishReason != "" {if choice.FinishReason == "error" {- err = fmt.Errorf("model stopped with error status | This usually means the model is not responding.")+ err = fmt.Errorf("model stopped with error status | The model is not responding.")return accumulator.Result(true, err), err} else {// Reset the timer for the usage chunkdiff --git a/app/server/model/name.go b/app/server/model/name.goindex bb725e7f..9fe549ff 100644--- a/app/server/model/name.go+++ b/app/server/model/name.go@@ -4,13 +4,10 @@ import ("context""encoding/json""fmt"- "log""plandex-server/db"- "plandex-server/hooks""plandex-server/model/prompts""plandex-server/types""plandex-server/utils"- "time"shared "plandex-shared"@@ -50,7 +47,7 @@ func GenPlanName(toolChoice = &choice}- content := prompts.GetPlanNamePrompt(sysPrompt, planContent)+ prompt := prompts.GetPlanNamePrompt(sysPrompt, planContent)messages := []types.ExtendedChatMessage{{@@ -58,81 +55,44 @@ func GenPlanName(Content: []types.ExtendedChatMessagePart{{Type: openai.ChatMessagePartTypeText,- Text: content,+ Text: prompt,},},},}- numTokens := GetMessagesTokenEstimate(messages...) + TokensPerRequest-- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: numTokens,- OutputTokens: config.BaseModelConfig.MaxOutputTokens - numTokens,- ModelName: config.BaseModelConfig.ModelName,- },- })- if apiErr != nil {- return "", fmt.Errorf("error executing hook: %v", apiErr)- }-- reqStarted := time.Now()- req := types.ExtendedChatCompletionRequest{- Model: config.BaseModelConfig.ModelName,+ modelRes, err := ModelRequest(ctx, ModelRequestParams{+ Clients: clients,+ Auth: auth,+ Plan: plan,+ ModelConfig: &config,+ Purpose: "Generate plan name",Messages: messages,- Temperature: config.Temperature,- TopP: config.TopP,- }-- if tools != nil {- req.Tools = tools- }- if toolChoice != nil {- req.ToolChoice = *toolChoice- }-- resp, err := CreateChatCompletion(- clients,- &config,- ctx,- req,- )-- var planName string+ Tools: tools,+ ToolChoice: toolChoice,+ })if err != nil {fmt.Printf("Error during plan name model call: %v\n", err)return "", err}+ var planName string+ content := modelRes.Content+if config.BaseModelConfig.PreferredModelOutputFormat == shared.ModelOutputFormatXml {- content := resp.Choices[0].Message.ContentplanName = utils.GetXMLContent(content, "planName")if planName == "" {return "", fmt.Errorf("No planName tag found in XML response")}} else {- var res string- for _, choice := range resp.Choices {- if len(choice.Message.ToolCalls) == 1 &&- choice.Message.ToolCalls[0].Function.Name == prompts.PlanNameFn.Name {- fnCall := choice.Message.ToolCalls[0].Function- res = fnCall.Arguments- break- }- }-- if res == "" {+ if content == "" {fmt.Println("no namePlan function call found in response")- return "", fmt.Errorf("No namePlan function call found in response. This usually means the model failed to generate a valid response.")+ return "", fmt.Errorf("No namePlan function call found in response. The model failed to generate a valid response.")}var nameRes prompts.PlanNameRes- bytes := []byte(res)- err = json.Unmarshal(bytes, &nameRes)+ err = json.Unmarshal([]byte(content), &nameRes)if err != nil {fmt.Printf("Error unmarshalling plan description response: %v\n", err)return "", err@@ -140,49 +100,6 @@ func GenPlanName(planName = nameRes.PlanName}- var inputTokens int- var outputTokens int- if resp.Usage.CompletionTokens > 0 {- inputTokens = resp.Usage.PromptTokens- outputTokens = resp.Usage.CompletionTokens- } else {- inputTokens = numTokens- outputTokens = shared.GetNumTokensEstimate(planName)- }-- var cachedTokens int- if resp.Usage.PromptTokensDetails != nil {- cachedTokens = resp.Usage.PromptTokensDetails.CachedTokens- }-- go func() {- _, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: outputTokens,- CachedTokens: cachedTokens,- ModelName: config.BaseModelConfig.ModelName,- ModelProvider: config.BaseModelConfig.Provider,- ModelPackName: settings.ModelPack.Name,- ModelRole: shared.ModelRolePlanSummary,- Purpose: "Generated plan name",- GenerationId: resp.ID,- PlanId: plan.Id,- RequestStartedAt: reqStarted,- Streaming: false,- Req: &req,- Res: &resp,- ModelConfig: &config,- },- })-- if apiErr != nil {- log.Printf("GenPlanName - error executing DidSendModelRequest hook: %v", apiErr)- }- }()-return planName, nil}@@ -219,7 +136,7 @@ func GenPipedDataName(toolChoice = &choice}- content := prompts.GetPipedDataNamePrompt(sysPrompt, pipedContent)+ prompt := prompts.GetPipedDataNamePrompt(sysPrompt, pipedContent)messages := []types.ExtendedChatMessage{{@@ -227,83 +144,44 @@ func GenPipedDataName(Content: []types.ExtendedChatMessagePart{{Type: openai.ChatMessagePartTypeText,- Text: content,+ Text: prompt,},},},}- numTokens := GetMessagesTokenEstimate(messages...) + TokensPerRequest-- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: numTokens,- OutputTokens: config.BaseModelConfig.MaxOutputTokens - numTokens,- ModelName: config.BaseModelConfig.ModelName,- },- })- if apiErr != nil {- return "", fmt.Errorf("error executing hook: %v", apiErr)- }-- log.Println("calling piped data name model")-- reqStarted := time.Now()- req := types.ExtendedChatCompletionRequest{- Model: config.BaseModelConfig.ModelName,+ modelRes, err := ModelRequest(ctx, ModelRequestParams{+ Clients: clients,+ Auth: auth,+ Plan: plan,+ ModelConfig: &config,+ Purpose: "Generate piped data name",Messages: messages,- Temperature: config.Temperature,- TopP: config.TopP,- }-- if tools != nil {- req.Tools = tools- }- if toolChoice != nil {- req.ToolChoice = *toolChoice- }-- resp, err := CreateChatCompletion(- clients,- &config,- ctx,- req,- )-- var name string+ Tools: tools,+ ToolChoice: toolChoice,+ })if err != nil {fmt.Printf("Error during piped data name model call: %v\n", err)return "", err}+ var name string+ content := modelRes.Content+if config.BaseModelConfig.PreferredModelOutputFormat == shared.ModelOutputFormatXml {- content := resp.Choices[0].Message.Contentname = utils.GetXMLContent(content, "name")if name == "" {return "", fmt.Errorf("No name tag found in XML response")}} else {- var res string- for _, choice := range resp.Choices {- if len(choice.Message.ToolCalls) == 1 &&- choice.Message.ToolCalls[0].Function.Name == prompts.PipedDataNameFn.Name {- fnCall := choice.Message.ToolCalls[0].Function- res = fnCall.Arguments- break- }- }-- if res == "" {+ if content == "" {fmt.Println("no namePipedData function call found in response")- return "", fmt.Errorf("No namePipedData function call found in response. This usually means the model failed to generate a valid response.")+ return "", fmt.Errorf("No namePipedData function call found in response. The model failed to generate a valid response.")}var nameRes prompts.PipedDataNameRes- bytes := []byte(res)- err = json.Unmarshal(bytes, &nameRes)+ err = json.Unmarshal([]byte(content), &nameRes)if err != nil {fmt.Printf("Error unmarshalling piped data name response: %v\n", err)return "", err@@ -311,49 +189,6 @@ func GenPipedDataName(name = nameRes.Name}- var inputTokens int- var outputTokens int- if resp.Usage.CompletionTokens > 0 {- inputTokens = resp.Usage.PromptTokens- outputTokens = resp.Usage.CompletionTokens- } else {- inputTokens = numTokens- outputTokens = shared.GetNumTokensEstimate(name)- }-- var cachedTokens int- if resp.Usage.PromptTokensDetails != nil {- cachedTokens = resp.Usage.PromptTokensDetails.CachedTokens- }-- go func() {- _, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: outputTokens,- CachedTokens: cachedTokens,- ModelName: config.BaseModelConfig.ModelName,- ModelProvider: config.BaseModelConfig.Provider,- ModelPackName: settings.ModelPack.Name,- ModelRole: shared.ModelRolePlanSummary,- Purpose: "Generated name for data piped into context",- GenerationId: resp.ID,- PlanId: plan.Id,- RequestStartedAt: reqStarted,- Streaming: false,- Req: &req,- Res: &resp,- ModelConfig: &config,- },- })-- if apiErr != nil {- log.Printf("GenPipedDataName - error executing DidSendModelRequest hook: %v", apiErr)- }- }()-return name, nil}@@ -390,7 +225,7 @@ func GenNoteName(toolChoice = &choice}- content := prompts.GetNoteNamePrompt(sysPrompt, note)+ prompt := prompts.GetNoteNamePrompt(sysPrompt, note)messages := []types.ExtendedChatMessage{{@@ -398,83 +233,44 @@ func GenNoteName(Content: []types.ExtendedChatMessagePart{{Type: openai.ChatMessagePartTypeText,- Text: content,+ Text: prompt,},},},}- numTokens := GetMessagesTokenEstimate(messages...) + TokensPerRequest-- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: numTokens,- OutputTokens: config.BaseModelConfig.MaxOutputTokens - numTokens,- ModelName: config.BaseModelConfig.ModelName,- },- })- if apiErr != nil {- return "", fmt.Errorf("error executing hook: %v", apiErr)- }-- log.Println("calling note name model")-- reqStarted := time.Now()- req := types.ExtendedChatCompletionRequest{- Model: config.BaseModelConfig.ModelName,+ modelRes, err := ModelRequest(ctx, ModelRequestParams{+ Clients: clients,+ Auth: auth,+ Plan: plan,+ ModelConfig: &config,+ Purpose: "Generate note name",Messages: messages,- Temperature: config.Temperature,- TopP: config.TopP,- }-- if tools != nil {- req.Tools = tools- }- if toolChoice != nil {- req.ToolChoice = *toolChoice- }-- resp, err := CreateChatCompletion(- clients,- &config,- ctx,- req,- )-- var name string+ Tools: tools,+ ToolChoice: toolChoice,+ })if err != nil {fmt.Printf("Error during note name model call: %v\n", err)return "", err}+ var name string+ content := modelRes.Content+if config.BaseModelConfig.PreferredModelOutputFormat == shared.ModelOutputFormatXml {- content := resp.Choices[0].Message.Contentname = utils.GetXMLContent(content, "name")if name == "" {return "", fmt.Errorf("No name tag found in XML response")}} else {- var res string- for _, choice := range resp.Choices {- if len(choice.Message.ToolCalls) == 1 &&- choice.Message.ToolCalls[0].Function.Name == prompts.NoteNameFn.Name {- fnCall := choice.Message.ToolCalls[0].Function- res = fnCall.Arguments- break- }- }-- if res == "" {+ if content == "" {fmt.Println("no nameNote function call found in response")- return "", fmt.Errorf("No nameNote function call found in response. This usually means the model failed to generate a valid response.")+ return "", fmt.Errorf("No nameNote function call found in response. The model failed to generate a valid response.")}var nameRes prompts.NoteNameRes- bytes := []byte(res)- err = json.Unmarshal(bytes, &nameRes)+ err = json.Unmarshal([]byte(content), &nameRes)if err != nil {fmt.Printf("Error unmarshalling note name response: %v\n", err)return "", err@@ -482,48 +278,5 @@ func GenNoteName(name = nameRes.Name}- var inputTokens int- var outputTokens int- if resp.Usage.CompletionTokens > 0 {- inputTokens = resp.Usage.PromptTokens- outputTokens = resp.Usage.CompletionTokens- } else {- inputTokens = numTokens- outputTokens = shared.GetNumTokensEstimate(name)- }-- var cachedTokens int- if resp.Usage.PromptTokensDetails != nil {- cachedTokens = resp.Usage.PromptTokensDetails.CachedTokens- }-- go func() {- _, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: outputTokens,- CachedTokens: cachedTokens,- ModelName: config.BaseModelConfig.ModelName,- ModelProvider: config.BaseModelConfig.Provider,- ModelPackName: settings.ModelPack.Name,- ModelRole: shared.ModelRolePlanSummary,- Purpose: "Generated name for note added to context",- GenerationId: resp.ID,- PlanId: plan.Id,- RequestStartedAt: reqStarted,- Streaming: false,- Req: &req,- Res: &resp,- ModelConfig: &config,- },- })-- if apiErr != nil {- log.Printf("GenNoteName - error executing DidSendModelRequest hook: %v", apiErr)- }- }()-return name, nil}diff --git a/app/server/model/plan/build_exec.go b/app/server/model/plan/build_exec.goindex 7929303d..d1446755 100644--- a/app/server/model/plan/build_exec.go+++ b/app/server/model/plan/build_exec.go@@ -278,39 +278,26 @@ func (fileState *activeBuildStreamFileState) buildFile() {if activeBuild.IsResetOp {log.Printf("File %s is a reset operation. Resetting file.\n", filePath)- repoLockId, err := db.LockRepo(db.LockRepoParams{+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{OrgId: currentOrgId,UserId: fileState.currentUserId,PlanId: planId,Branch: branch,PlanBuildId: build.Id,Scope: db.LockScopeWrite,+ Reason: "reset file op",Ctx: activePlan.Ctx,CancelFn: activePlan.CancelFn,+ }, func(repo *db.GitRepo) error {+ now := time.Now()+ return db.RejectPlanFile(currentOrgId, planId, filePath, now)})- if err != nil {- log.Printf("Error locking repo for reset: %v\n", err)- fileState.onBuildFileError(fmt.Errorf("error locking repo for reset: %v", err))- return- }- now := time.Now()- err = db.RejectPlanFile(currentOrgId, planId, filePath, now)if err != nil {log.Printf("Error rejecting plan file: %v\n", err)- unlockErr := db.DeleteRepoLock(repoLockId, planId)- if unlockErr != nil {- log.Printf("Error unlocking repo for reset: %v\n", unlockErr)- }fileState.onBuildFileError(fmt.Errorf("error rejecting plan file: %v", err))return}- err = db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo for reset: %v\n", err)- fileState.onBuildFileError(fmt.Errorf("error unlocking repo for reset: %v", err))- return- }buildInfo := &shared.BuildInfo{Path: filePath,diff --git a/app/server/model/plan/build_finish.go b/app/server/model/plan/build_finish.goindex 44e86f94..a5898910 100644--- a/app/server/model/plan/build_finish.go+++ b/app/server/model/plan/build_finish.go@@ -58,54 +58,20 @@ func (state *activeBuildStreamFileState) onFinishBuild() {log.Println("Locking repo for finished build")- repoLockId, err := db.LockRepo(- db.LockRepoParams{- OrgId: currentOrgId,- UserId: currentUserId,- PlanId: planId,- Branch: branch,- PlanBuildId: build.Id,- Scope: db.LockScopeWrite,- Ctx: ap.Ctx,- CancelFn: ap.CancelFn,- },- )-- if err != nil {- log.Printf("Error locking repo for finished build: %v\n", err)- ap.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo for finished build: " + err.Error(),- }- return- }-- log.Println("Locked repo for finished build")-- err = func() error {- var err error- defer func() {- if err != nil {- log.Printf("Finish build error: %v\n", err)- err = db.GitClearUncommittedChanges(currentOrgId, planId, branch)- if err != nil {- log.Printf("Error clearing uncommitted changes: %v\n", err)- }- log.Println("Cleared uncommitted changes")- }-- err := db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- }-- log.Println("Unlocked repo")- }()-+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: currentOrgId,+ UserId: currentUserId,+ PlanId: planId,+ Branch: branch,+ PlanBuildId: build.Id,+ Scope: db.LockScopeWrite,+ Ctx: ap.Ctx,+ CancelFn: ap.CancelFn,+ Reason: "finish build",+ }, func(repo *db.GitRepo) error {// get plan descriptionsvar planDescs []*db.ConvoMessageDescription- planDescs, err = db.GetConvoMessageDescriptions(currentOrgId, planId)+ planDescs, err := db.GetConvoMessageDescriptions(currentOrgId, planId)if err != nil {log.Printf("Error getting pending build descriptions: %v\n", err)return fmt.Errorf("error getting pending build descriptions: %v", err)@@ -157,30 +123,28 @@ func (state *activeBuildStreamFileState) onFinishBuild() {}}- err = db.GitAddAndCommit(currentOrgId, planId, branch, currentPlan.PendingChangesSummaryForBuild())+ err = repo.GitAddAndCommit(branch, currentPlan.PendingChangesSummaryForBuild())if err != nil {if strings.Contains(err.Error(), "nothing to commit") {log.Println("Nothing to commit")return nil}-- log.Printf("Error committing plan build: %v\n", err)- ap.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error committing plan build: " + err.Error(),- }- return err+ return fmt.Errorf("error committing plan build: %v", err)}log.Println("Plan build committed")return nil-- }()+ })if err != nil {+ log.Printf("Error finishing build: %v\n", err)+ ap.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error finishing build: " + err.Error(),+ }return}@@ -219,7 +183,7 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanFreturn}- repoLockId, err := db.LockRepo(db.LockRepoParams{+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{OrgId: currentOrgId,UserId: fileState.currentUserId,PlanId: planId,@@ -228,62 +192,26 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanFScope: db.LockScopeWrite,Ctx: activePlan.Ctx,CancelFn: activePlan.CancelFn,- })- if err != nil {- log.Printf("Error locking repo for build file: %v\n", err)- activePlan.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo for build file: " + err.Error(),- }- return- }-- err = func() error {- var err error- defer func() {- if err != nil {- log.Printf("Error storing plan result: %v\n", err)- err = db.GitClearUncommittedChanges(currentOrgId, planId, branch)- if err != nil {- log.Printf("Error clearing uncommitted changes: %v\n", err)- }- } else {- log.Println("Plan result stored successfully.")- }- log.Println("Unlocking repo")-- activePlan = GetActivePlan(planId, branch)- if activePlan == nil {- log.Println("onFinishBuildFile - Active plan not found")- return- }-- err := db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- }- }()-+ Reason: "store plan result",+ }, func(repo *db.GitRepo) error {log.Println("Storing plan result", planRes.Path)- // spew.Dump(planRes)- err = db.StorePlanResult(planRes)+ err := db.StorePlanResult(planRes)if err != nil {log.Printf("Error storing plan result: %v\n", err)- activePlan.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error storing plan result: " + err.Error(),- }return err}- // log.Println("Plan result stored", planRes.Path)return nil- }()+ })if err != nil {+ log.Printf("Error storing plan build result: %v\n", err)+ activePlan.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error storing plan build result: " + err.Error(),+ }return}@@ -340,7 +268,6 @@ func (fileState *activeBuildStreamFileState) onBuildFileError(err error) {filePath := fileState.filePathbuild := fileState.buildactiveBuild := fileState.activeBuild- currentOrgId := fileState.currentOrgIdactivePlan := GetActivePlan(planId, branch)@@ -370,38 +297,6 @@ func (fileState *activeBuildStreamFileState) onBuildFileError(err error) {if err != nil {log.Printf("Error setting build error: %v\n", err)}-- repoLockId, err := db.LockRepo(db.LockRepoParams{- OrgId: currentOrgId,- UserId: fileState.currentUserId,- PlanId: planId,- Branch: branch,- PlanBuildId: build.Id,- Scope: db.LockScopeWrite,- Ctx: activePlan.Ctx,- CancelFn: activePlan.CancelFn,- })- if err != nil {- log.Printf("Error locking repo for build error: %v\n", err)- activePlan.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo for build error: " + err.Error(),- }- return- }-- // rollback repo in case there are uncommitted builds- err = db.GitClearUncommittedChanges(currentOrgId, planId, branch)-- if err != nil {- log.Printf("Error clearing uncommitted changes: %v\n", err)- }-- err = db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- }}func (fileState *activeBuildStreamFileState) buildNextInQueue() bool {diff --git a/app/server/model/plan/build_load.go b/app/server/model/plan/build_load.goindex 77337160..7959fdd6 100644--- a/app/server/model/plan/build_load.go+++ b/app/server/model/plan/build_load.go@@ -26,33 +26,20 @@ func (state *activeBuildStreamState) loadPendingBuilds() (map[string][]*types.AcmodelStreamId := active.ModelStreamIdstate.modelStreamId = modelStreamId- repoLockId, err := db.LockRepo(- db.LockRepoParams{- OrgId: auth.OrgId,- UserId: auth.User.Id,- PlanId: plan.Id,- Branch: branch,- Scope: db.LockScopeRead,- Ctx: active.Ctx,- CancelFn: active.CancelFn,- },- )- if err != nil {- return nil, fmt.Errorf("error locking repo for build: %v", err)- }-var modelContext []*db.Contextvar pendingBuildsByPath map[string][]*types.ActiveBuildvar settings *shared.PlanSettings- err = func() error {- defer func() {- err := db.DeleteRepoLock(repoLockId, plan.Id)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- }- }()-+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: plan.Id,+ Branch: branch,+ Scope: db.LockScopeRead,+ Ctx: active.Ctx,+ CancelFn: active.CancelFn,+ Reason: "load pending builds",+ }, func(repo *db.GitRepo) error {errCh := make(chan error)go func() {@@ -101,10 +88,10 @@ func (state *activeBuildStreamState) loadPendingBuilds() (map[string][]*types.Ac}}return nil- }()+ })if err != nil {- return nil, err+ return nil, fmt.Errorf("error getting plan data: %v", err)}UpdateActivePlan(plan.Id, branch, func(ap *types.ActivePlan) {@@ -183,49 +170,17 @@ func (state *activeBuildStreamFileState) loadBuildFile(activeBuild *types.Activelog.Println("Locking repo for load build file")- // For file operations, use write lock so that the same lock can be shared for both the load and write phase - resolves lock contention issue with many near-instantaneous file operations- var lockScope db.LockScope- if activeBuild.IsFileOperation() {- lockScope = db.LockScopeWrite- } else {- lockScope = db.LockScopeRead- }-- repoLockId, err := db.LockRepo(db.LockRepoParams{+ err = db.ExecRepoOperation(db.ExecRepoOperationParams{OrgId: currentOrgId,UserId: state.activeBuildStreamState.currentUserId,PlanId: planId,Branch: branch,PlanBuildId: build.Id,- Scope: lockScope,+ Scope: db.LockScopeRead,Ctx: activePlan.Ctx,CancelFn: activePlan.CancelFn,- })- if err != nil {- log.Printf("Error locking repo for build file: %v\n", err)- UpdateActivePlan(activePlan.Id, activePlan.Branch, func(ap *types.ActivePlan) {- ap.IsBuildingByPath[filePath] = false- })- activePlan.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo for build file: " + err.Error(),- }- return err- }-- log.Println("Locked repo for load build file")-- err = func() error {- defer func() {- log.Printf("Unlocking repo for load build file")-- err := db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- }- }()-+ Reason: "load build file",+ }, func(repo *db.GitRepo) error {errCh := make(chan error)go func() {@@ -274,10 +229,18 @@ func (state *activeBuildStreamFileState) loadBuildFile(activeBuild *types.Active}return nil-- }()+ })if err != nil {+ log.Printf("Error loading build file: %v\n", err)+ UpdateActivePlan(activePlan.Id, activePlan.Branch, func(ap *types.ActivePlan) {+ ap.IsBuildingByPath[filePath] = false+ })+ activePlan.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error loading build file: " + err.Error(),+ }return err}diff --git a/app/server/model/plan/commit_msg.go b/app/server/model/plan/commit_msg.goindex 841903cf..c24f98e6 100644--- a/app/server/model/plan/commit_msg.go+++ b/app/server/model/plan/commit_msg.go@@ -3,21 +3,17 @@ package planimport ("context""encoding/json"- "errors""fmt""log""net/http""plandex-server/db"- "plandex-server/hooks""plandex-server/model""plandex-server/model/prompts""plandex-server/types""plandex-server/utils"- "time"shared "plandex-shared"- "github.com/davecgh/go-spew/spew""github.com/sashabaranov/go-openai")@@ -83,47 +79,27 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescri},}- numTokens := model.GetMessagesTokenEstimate(messages...) + model.TokensPerRequest-- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: numTokens,- OutputTokens: config.BaseModelConfig.MaxOutputTokens - numTokens,- ModelName: config.BaseModelConfig.ModelName,- },- })- if apiErr != nil {- return nil, apiErr- }-- log.Println("Sending plan description model request")-- reqStarted := time.Now()- req := types.ExtendedChatCompletionRequest{- Model: config.BaseModelConfig.ModelName,- Messages: messages,- Temperature: config.Temperature,- TopP: config.TopP,+ reqParams := model.ModelRequestParams{+ Clients: clients,+ Auth: auth,+ Plan: plan,+ ModelConfig: &config,+ Purpose: "Plan description",+ Messages: messages,+ ModelStreamId: state.modelStreamId,+ ConvoMessageId: state.replyId,}if tools != nil {- req.Tools = tools+ reqParams.Tools = tools}if toolChoice != nil {- req.ToolChoice = *toolChoice+ reqParams.ToolChoice = toolChoice}- descResp, err := model.CreateChatCompletion(- clients,- &config,- activePlan.Ctx,- req,- )+ modelRes, err := model.ModelRequest(activePlan.Ctx, reqParams)if err != nil {- fmt.Printf("Error during plan description model call: %v\n", err)return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther,Status: http.StatusInternalServerError,@@ -133,10 +109,11 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescrilog.Println("Plan description model call complete")+ content := modelRes.Content+var commitMsg stringif config.BaseModelConfig.PreferredModelOutputFormat == shared.ModelOutputFormatXml {- content := descResp.Choices[0].Message.ContentcommitMsg = utils.GetXMLContent(content, "commitMsg")if commitMsg == "" {return nil, &shared.ApiError{@@ -146,28 +123,19 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescri}}} else {- var descStrRes string- for _, choice := range descResp.Choices {- if len(choice.Message.ToolCalls) == 1 &&- choice.Message.ToolCalls[0].Function.Name == prompts.DescribePlanFn.Name {- fnCall := choice.Message.ToolCalls[0].Function- descStrRes = fnCall.Arguments- break- }- }- if descStrRes == "" {+ if content == "" {fmt.Println("no describePlan function call found in response")- spew.Dump(descResp)+return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther,Status: http.StatusInternalServerError,- Msg: "No describePlan function call found in response. This usually means the model failed to generate a valid response.",+ Msg: "No describePlan function call found in response. The model failed to generate a valid response.",}}var desc shared.ConvoMessageDescription- err = json.Unmarshal([]byte(descStrRes), &desc)+ err = json.Unmarshal([]byte(content), &desc)if err != nil {fmt.Printf("Error unmarshalling plan description response: %v\n", err)return nil, &shared.ApiError{@@ -179,54 +147,6 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescricommitMsg = desc.CommitMsg}- var inputTokens int- var outputTokens int- if descResp.Usage.CompletionTokens > 0 {- inputTokens = descResp.Usage.PromptTokens- outputTokens = descResp.Usage.CompletionTokens- } else {- inputTokens = numTokens- outputTokens = shared.GetNumTokensEstimate(commitMsg)- }-- var cachedTokens int- if descResp.Usage.PromptTokensDetails != nil {- cachedTokens = descResp.Usage.PromptTokensDetails.CachedTokens- }-- log.Println("Sending DidSendModelRequest hook")-- go func() {- _, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: outputTokens,- CachedTokens: cachedTokens,- ModelName: config.BaseModelConfig.ModelName,- ModelProvider: config.BaseModelConfig.Provider,- ModelPackName: settings.ModelPack.Name,- ModelRole: shared.ModelRoleCommitMsg,- Purpose: "Generated commit message for suggested changes",- GenerationId: descResp.ID,- PlanId: planId,- ModelStreamId: state.modelStreamId,- ConvoMessageId: state.replyId,-- RequestStartedAt: reqStarted,- Streaming: false,- Req: &req,- Res: &descResp,- ModelConfig: &config,- },- })-- if apiErr != nil {- log.Printf("genPlanDescription - error executing DidSendModelRequest hook: %v", apiErr)- }- }()-return &db.ConvoMessageDescription{PlanId: planId,CommitMsg: commitMsg,@@ -250,7 +170,7 @@ func GenCommitMsgForPendingResults(auth *types.ServerAuth, plan *db.Plan, clientreturn s, nil}- content := "Pending changes:\n\n" + s+ prompt := "Pending changes:\n\n" + smessages := []types.ExtendedChatMessage{{@@ -267,94 +187,32 @@ func GenCommitMsgForPendingResults(auth *types.ServerAuth, plan *db.Plan, clientContent: []types.ExtendedChatMessagePart{{Type: openai.ChatMessagePartTypeText,- Text: content,+ Text: prompt,},},},}- numTokens := model.GetMessagesTokenEstimate(messages...) + model.TokensPerRequest-- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: numTokens,- OutputTokens: config.BaseModelConfig.MaxOutputTokens - numTokens,- ModelName: config.BaseModelConfig.ModelName,- },- })- if apiErr != nil {- return "", errors.New(apiErr.Msg)- }-- reqStarted := time.Now()- req := types.ExtendedChatCompletionRequest{- Model: config.BaseModelConfig.ModelName,+ modelRes, err := model.ModelRequest(ctx, model.ModelRequestParams{+ Clients: clients,+ Auth: auth,+ Plan: plan,+ ModelConfig: &config,+ Purpose: "Commit message for pending changes",Messages: messages,- Temperature: config.Temperature,- TopP: config.TopP,- }-- resp, err := model.CreateChatCompletion(- clients,- &config,- ctx,- req,- )+ })if err != nil {- fmt.Println("PlanSummary err:", err)+ fmt.Println("Generate commit message error:", err)return "", err}- if len(resp.Choices) == 0 {- return "", fmt.Errorf("no response from GPT")- }-- commitMsg := resp.Choices[0].Message.Content-- var inputTokens int- var outputTokens int- if resp.Usage.CompletionTokens > 0 {- inputTokens = resp.Usage.PromptTokens- outputTokens = resp.Usage.CompletionTokens- } else {- inputTokens = numTokens- outputTokens = shared.GetNumTokensEstimate(commitMsg)- }-- var cachedTokens int- if resp.Usage.PromptTokensDetails != nil {- cachedTokens = resp.Usage.PromptTokensDetails.CachedTokens- }-- _, apiErr = hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: outputTokens,- CachedTokens: cachedTokens,- ModelName: config.BaseModelConfig.ModelName,- ModelProvider: config.BaseModelConfig.Provider,- ModelPackName: settings.ModelPack.Name,- ModelRole: shared.ModelRoleCommitMsg,- Purpose: "Generated commit message for pending changes",- GenerationId: resp.ID,-- RequestStartedAt: reqStarted,- Streaming: false,- Req: &req,- Res: &resp,- ModelConfig: &config,- },- })+ content := modelRes.Content- if apiErr != nil {- return "", errors.New(apiErr.Msg)+ if content == "" {+ return "", fmt.Errorf("no response from model")}- return commitMsg, nil+ return content, nil}diff --git a/app/server/model/plan/exec_status.go b/app/server/model/plan/exec_status.goindex fa62ae9d..6f2b6755 100644--- a/app/server/model/plan/exec_status.go+++ b/app/server/model/plan/exec_status.go@@ -6,13 +6,11 @@ import ("fmt""log""net/http"- "plandex-server/hooks""plandex-server/model""plandex-server/model/prompts""plandex-server/types""plandex-server/utils""strings"- "time"shared "plandex-shared"@@ -110,7 +108,7 @@ func (state *activeTellStreamState) execStatusShouldContinue(currentMessage stri}, nil}- content := prompts.GetExecStatusFinishedSubtask(prompts.GetExecStatusFinishedSubtaskParams{+ prompt := prompts.GetExecStatusFinishedSubtask(prompts.GetExecStatusFinishedSubtaskParams{UserPrompt: state.userPrompt,CurrentSubtask: fullSubtask,CurrentMessage: currentMessage,@@ -124,69 +122,34 @@ func (state *activeTellStreamState) execStatusShouldContinue(currentMessage striContent: []types.ExtendedChatMessagePart{{Type: openai.ChatMessagePartTypeText,- Text: content,+ Text: prompt,},},},}- numTokens := model.GetMessagesTokenEstimate(messages...) + model.TokensPerRequest-- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: numTokens,- OutputTokens: config.BaseModelConfig.MaxOutputTokens - numTokens,- ModelName: config.BaseModelConfig.ModelName,- },+ modelRes, err := model.ModelRequest(ctx, model.ModelRequestParams{+ Clients: clients,+ Auth: auth,+ Plan: plan,+ ModelConfig: &config,+ Purpose: "Check if task finished",+ Messages: messages,+ ModelStreamId: state.modelStreamId,+ ConvoMessageId: state.replyId,})- if apiErr != nil {- return execStatusShouldContinueResult{}, apiErr- }-- log.Println("Calling model to check if plan should continue")-- reqStarted := time.Now()- req := types.ExtendedChatCompletionRequest{- Model: config.BaseModelConfig.ModelName,- Messages: messages,- Temperature: config.Temperature,- TopP: config.TopP,- }-- if config.BaseModelConfig.PreferredModelOutputFormat != shared.ModelOutputFormatXml {- req.Tools = []openai.Tool{- {- Type: "function",- Function: &prompts.DidFinishSubtaskFn,- },- }- req.ToolChoice = openai.ToolChoice{- Type: "function",- Function: openai.ToolFunction{- Name: prompts.DidFinishSubtaskFn.Name,- },- }- }-- resp, err := model.CreateChatCompletion(- clients,- &config,- ctx,- req,- )if err != nil {log.Printf("[ExecStatus] Error in model call: %v", err)return execStatusShouldContinueResult{}, nil}+ content := modelRes.Content+var reasoning stringvar subtaskFinished boolif config.BaseModelConfig.PreferredModelOutputFormat == shared.ModelOutputFormatXml {- content := resp.Choices[0].Message.Contentreasoning = utils.GetXMLContent(content, "reasoning")subtaskFinishedStr := utils.GetXMLContent(content, "subtaskFinished")subtaskFinished = subtaskFinishedStr == "true"@@ -199,23 +162,14 @@ func (state *activeTellStreamState) execStatusShouldContinue(currentMessage stri}}} else {- var strRes string- for _, choice := range resp.Choices {- if len(choice.Message.ToolCalls) == 1 &&- choice.Message.ToolCalls[0].Function.Name == prompts.DidFinishSubtaskFn.Name {- strRes = choice.Message.ToolCalls[0].Function.Arguments- log.Printf("[ExecStatus] Got function response: %s", strRes)- break- }- }- if strRes == "" {+ if content == "" {log.Printf("[ExecStatus] No function response found in model output")return execStatusShouldContinueResult{}, nil}var res types.ExecStatusResponse- if err := json.Unmarshal([]byte(strRes), &res); err != nil {+ if err := json.Unmarshal([]byte(content), &res); err != nil {log.Printf("[ExecStatus] Failed to parse response: %v", err)return execStatusShouldContinueResult{}, nil}@@ -227,52 +181,6 @@ func (state *activeTellStreamState) execStatusShouldContinue(currentMessage strilog.Printf("[ExecStatus] Decision: subtaskFinished=%v, reasoning=%v",subtaskFinished, reasoning)- var inputTokens int- var outputTokens int- if resp.Usage.CompletionTokens > 0 {- inputTokens = resp.Usage.PromptTokens- outputTokens = resp.Usage.CompletionTokens- } else {- inputTokens = numTokens- outputTokens = shared.GetNumTokensEstimate(reasoning)- }-- var cachedTokens int- if resp.Usage.PromptTokensDetails != nil {- cachedTokens = resp.Usage.PromptTokensDetails.CachedTokens- }-- go func() {- _, apiErr = hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: outputTokens,- CachedTokens: cachedTokens,- ModelName: config.BaseModelConfig.ModelName,- ModelProvider: config.BaseModelConfig.Provider,- ModelPackName: settings.ModelPack.Name,- ModelRole: shared.ModelRoleExecStatus,- Purpose: "Evaluate if task was finished",- GenerationId: resp.ID,- PlanId: plan.Id,- ModelStreamId: state.modelStreamId,- ConvoMessageId: state.replyId,-- RequestStartedAt: reqStarted,- Streaming: false,- Req: &req,- Res: &resp,- ModelConfig: &config,- },- })-- if apiErr != nil {- log.Printf("execStatusShouldContinue - error executing DidSendModelRequest hook: %v", apiErr)- }- }()-return execStatusShouldContinueResult{subtaskFinished: subtaskFinished,}, nildiff --git a/app/server/model/plan/state.go b/app/server/model/plan/state.goindex d9ed74cf..70197698 100644--- a/app/server/model/plan/state.go+++ b/app/server/model/plan/state.go@@ -93,33 +93,24 @@ func DeleteActivePlan(orgId, userId, planId, branch string) {return}- repoLockId, err := db.LockRepo(- db.LockRepoParams{- UserId: userId,- OrgId: orgId,- PlanId: planId,- Branch: branch,- Scope: db.LockScopeWrite,- Ctx: context.Background(),- CancelFn: func() {},- },- )+ ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancelFn()++ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: orgId,+ UserId: userId,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancelFn,+ Reason: "delete active plan",+ }, func(repo *db.GitRepo) error {+ return repo.GitClearUncommittedChanges(branch)+ })if err != nil {- log.Printf("Error locking repo for plan %s: %v\n", planId, err)- } else {-- defer func() {- err := db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo for plan %s: %v\n", planId, err)- }- }()-- err := db.GitClearUncommittedChanges(orgId, planId, branch)- if err != nil {- log.Printf("Error clearing uncommitted changes for plan %s: %v\n", planId, err)- }+ log.Printf("Error clearing uncommitted changes for plan %s: %v\n", planId, err)}activePlans.Delete(strings.Join([]string{planId, branch}, "|"))diff --git a/app/server/model/plan/stop.go b/app/server/model/plan/stop.goindex 8433ea67..99a6ee90 100644--- a/app/server/model/plan/stop.go+++ b/app/server/model/plan/stop.go@@ -20,7 +20,7 @@ func Stop(planId, branch, currentUserId, currentOrgId string) error {return nil}-func StorePartialReply(planId, branch, currentUserId, currentOrgId string) error {+func StorePartialReply(repo *db.GitRepo, planId, branch, currentUserId, currentOrgId string) error {active := GetActivePlan(planId, branch)if active == nil {@@ -41,7 +41,7 @@ func StorePartialReply(planId, branch, currentUserId, currentOrgId string) errorMessage: active.CurrentReplyContent,}- _, err := db.StoreConvoMessage(&userMsg, currentUserId, branch, true)+ _, err := db.StoreConvoMessage(repo, &userMsg, currentUserId, branch, true)if err != nil {return fmt.Errorf("error storing convo message: %v", err)diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex 367e0923..033e89c1 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -13,7 +13,6 @@ import (shared "plandex-shared"- "github.com/davecgh/go-spew/spew""github.com/google/uuid""github.com/sashabaranov/go-openai")@@ -326,7 +325,7 @@ func execTellPlan(params execTellPlanParams) {// log.Println("Stop:", stop)// spew.Dump(state.messages)- log.Println("modelConfig:", spew.Sdump(modelConfig))+ // log.Println("modelConfig:", spew.Sdump(modelConfig))modelReq := types.ExtendedChatCompletionRequest{Model: modelConfig.BaseModelConfig.ModelName,diff --git a/app/server/model/plan/tell_load.go b/app/server/model/plan/tell_load.goindex 2715005d..b75512e9 100644--- a/app/server/model/plan/tell_load.go+++ b/app/server/model/plan/tell_load.go@@ -10,6 +10,7 @@ import (shared "plandex-shared"+ "github.com/jmoiron/sqlx""github.com/sashabaranov/go-openai")@@ -35,29 +36,7 @@ func (state *activeTellStreamState) loadTellPlan() error {if iteration > 0 || missingFileResponse != "" {lockScope = db.LockScopeRead}- repoLockId, err := db.LockRepo(- db.LockRepoParams{- OrgId: auth.OrgId,- UserId: auth.User.Id,- PlanId: planId,- Branch: branch,- Scope: lockScope,- Ctx: active.Ctx,- CancelFn: active.CancelFn,- },- )- if err != nil {- log.Printf("execTellPlan: Error locking repo for plan ID %s on branch %s: %v\n", plan.Id, branch, err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo",- }- return err- }-- errCh := make(chan error)var modelContext []*db.Contextvar convo []*db.ConvoMessagevar promptMsg *db.ConvoMessage@@ -67,218 +46,191 @@ func (state *activeTellStreamState) loadTellPlan() error {var latestSummaryTokens intvar currentPlan *shared.CurrentPlanState- // get name for plan and rename if it's a draft- go func() {- res, err := db.GetPlanSettings(plan, true)- if err != nil {- log.Printf("Error getting plan settings: %v\n", err)- errCh <- fmt.Errorf("error getting plan settings: %v", err)- return- }- settings = res-- if plan.Name == "draft" {- name, err := model.GenPlanName(- auth,- plan,- settings,- clients,- req.Prompt,- active.Ctx,- )-+ db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: lockScope,+ Ctx: active.Ctx,+ CancelFn: active.CancelFn,+ Reason: "load tell plan",+ }, func(repo *db.GitRepo) error {+ errCh := make(chan error)++ // get name for plan and rename if it's a draft+ go func() {+ res, err := db.GetPlanSettings(plan, true)if err != nil {- log.Printf("Error generating plan name: %v\n", err)- errCh <- fmt.Errorf("error generating plan name: %v", err)+ log.Printf("Error getting plan settings: %v\n", err)+ errCh <- fmt.Errorf("error getting plan settings: %v", err)return}+ settings = res++ if plan.Name == "draft" {+ name, err := model.GenPlanName(+ auth,+ plan,+ settings,+ clients,+ req.Prompt,+ active.Ctx,+ )- tx, err := db.Conn.Beginx()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- errCh <- fmt.Errorf("error starting transaction: %v", err)- }-- // Ensure that rollback is attempted in case of failure- defer func() {if err != nil {- if rbErr := tx.Rollback(); rbErr != nil {- log.Printf("transaction rollback error: %v\n", rbErr)- } else {- log.Println("transaction rolled back")- }+ log.Printf("Error generating plan name: %v\n", err)+ errCh <- fmt.Errorf("error generating plan name: %v", err)+ return}- }()- err = db.RenamePlan(planId, name, tx)+ err = db.WithTx(active.Ctx, "rename plan", func(tx *sqlx.Tx) error {+ err := db.RenamePlan(planId, name, tx)- if err != nil {- log.Printf("Error renaming plan: %v\n", err)- errCh <- fmt.Errorf("error renaming plan: %v", err)- return- }-- err = db.IncNumNonDraftPlans(currentUserId, tx)+ if err != nil {+ log.Printf("Error renaming plan: %v\n", err)+ return fmt.Errorf("error renaming plan: %v", err)+ }- if err != nil {- log.Printf("Error incrementing num non draft plans: %v\n", err)- errCh <- fmt.Errorf("error incrementing num non draft plans: %v", err)- return- }+ err = db.IncNumNonDraftPlans(currentUserId, tx)- err = tx.Commit()- if err != nil {- log.Printf("Error committing transaction: %v\n", err)- errCh <- fmt.Errorf("error committing transaction: %v", err)- return- }- }+ if err != nil {+ log.Printf("Error incrementing num non draft plans: %v\n", err)+ return fmt.Errorf("error incrementing num non draft plans: %v", err)+ }- errCh <- nil- }()+ return nil+ })- go func() {- if iteration > 0 || missingFileResponse != "" {- modelContext = active.Contexts- } else {- res, err := db.GetPlanContexts(currentOrgId, planId, true, false)- if err != nil {- log.Printf("Error getting plan modelContext: %v\n", err)- errCh <- fmt.Errorf("error getting plan modelContext: %v", err)- return+ if err != nil {+ log.Printf("Error renaming plan: %v\n", err)+ errCh <- fmt.Errorf("error renaming plan: %v", err)+ return+ }}- modelContext = res- }-- errCh <- nil- }()-- go func() {- res, err := db.GetPlanConvo(currentOrgId, planId)- if err != nil {- log.Printf("Error getting plan convo: %v\n", err)- errCh <- fmt.Errorf("error getting plan convo: %v", err)- return- }- convo = res- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.MessageNum = len(convo)- })- promptTokens := shared.GetNumTokensEstimate(req.Prompt)- innerErrCh := make(chan error)+ errCh <- nil+ }()go func() {- if iteration == 0 && missingFileResponse == "" && !req.IsUserContinue {- num := len(convo) + 1-- log.Printf("storing user message | len(convo): %d | num: %d\n", len(convo), num)-- promptMsg = &db.ConvoMessage{- OrgId: currentOrgId,- PlanId: planId,- UserId: currentUserId,- Role: openai.ChatMessageRoleUser,- Tokens: promptTokens,- Num: num,- Message: req.Prompt,- Flags: shared.ConvoMessageFlags{- IsApplyDebug: req.IsApplyDebug,- IsUserDebug: req.IsUserDebug,- IsChat: req.IsChatOnly,- },- }-- _, err = db.StoreConvoMessage(promptMsg, auth.User.Id, branch, true)-+ if iteration > 0 || missingFileResponse != "" {+ modelContext = active.Contexts+ } else {+ res, err := db.GetPlanContexts(currentOrgId, planId, true, false)if err != nil {- log.Printf("Error storing user message: %v\n", err)- innerErrCh <- fmt.Errorf("error storing user message: %v", err)+ log.Printf("Error getting plan modelContext: %v\n", err)+ errCh <- fmt.Errorf("error getting plan modelContext: %v", err)return}-- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.MessageNum = num- })+ modelContext = res}- innerErrCh <- nil+ errCh <- nil}()go func() {- var convoMessageIds []string-- for _, convoMessage := range convo {- convoMessageIds = append(convoMessageIds, convoMessage.Id)- }-- log.Println("getting plan summaries")- log.Println("convoMessageIds:", convoMessageIds)-- res, err := db.GetPlanSummaries(planId, convoMessageIds)+ res, err := db.GetPlanConvo(currentOrgId, planId)if err != nil {- log.Printf("Error getting plan summaries: %v\n", err)- innerErrCh <- fmt.Errorf("error getting plan summaries: %v", err)+ log.Printf("Error getting plan convo: %v\n", err)+ errCh <- fmt.Errorf("error getting plan convo: %v", err)return}- summaries = res+ convo = res+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.MessageNum = len(convo)+ })- log.Printf("got %d plan summaries", len(summaries))+ promptTokens := shared.GetNumTokensEstimate(req.Prompt)+ innerErrCh := make(chan error)++ go func() {+ if iteration == 0 && missingFileResponse == "" && !req.IsUserContinue {+ num := len(convo) + 1++ log.Printf("storing user message | len(convo): %d | num: %d\n", len(convo), num)++ promptMsg = &db.ConvoMessage{+ OrgId: currentOrgId,+ PlanId: planId,+ UserId: currentUserId,+ Role: openai.ChatMessageRoleUser,+ Tokens: promptTokens,+ Num: num,+ Message: req.Prompt,+ Flags: shared.ConvoMessageFlags{+ IsApplyDebug: req.IsApplyDebug,+ IsUserDebug: req.IsUserDebug,+ IsChat: req.IsChatOnly,+ },+ }- if len(summaries) > 0 {- latestSummaryTokens = shared.GetNumTokensEstimate(summaries[len(summaries)-1].Summary)- }+ _, err = db.StoreConvoMessage(repo, promptMsg, auth.User.Id, branch, true)- innerErrCh <- nil- }()+ if err != nil {+ log.Printf("Error storing user message: %v\n", err)+ innerErrCh <- fmt.Errorf("error storing user message: %v", err)+ return+ }- for i := 0; i < 2; i++ {- err := <-innerErrCh- if err != nil {- errCh <- err- return- }- }+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.MessageNum = num+ })+ }- if promptMsg != nil {- convo = append(convo, promptMsg)- }+ innerErrCh <- nil+ }()- errCh <- nil- }()+ go func() {+ var convoMessageIds []string- go func() {- res, err := db.GetPlanSubtasks(auth.OrgId, planId)- if err != nil {- log.Printf("Error getting plan subtasks: %v\n", err)- errCh <- fmt.Errorf("error getting plan subtasks: %v", err)- return- }- subtasks = res- errCh <- nil- }()+ for _, convoMessage := range convo {+ convoMessageIds = append(convoMessageIds, convoMessage.Id)+ }- err = func() error {- var err error- defer func() {- if err != nil {- log.Printf("Error: %v\n", err)- err = db.GitClearUncommittedChanges(auth.OrgId, planId, branch)+ log.Println("getting plan summaries")+ log.Println("convoMessageIds:", convoMessageIds)++ res, err := db.GetPlanSummaries(planId, convoMessageIds)if err != nil {- log.Printf("Error clearing uncommitted changes: %v\n", err)+ log.Printf("Error getting plan summaries: %v\n", err)+ innerErrCh <- fmt.Errorf("error getting plan summaries: %v", err)+ return}+ summaries = res++ log.Printf("got %d plan summaries", len(summaries))++ if len(summaries) > 0 {+ latestSummaryTokens = shared.GetNumTokensEstimate(summaries[len(summaries)-1].Summary)+ }++ innerErrCh <- nil+ }()++ for i := 0; i < 2; i++ {+ err := <-innerErrCh+ if err != nil {+ errCh <- err+ return+ }+ }++ if promptMsg != nil {+ convo = append(convo, promptMsg)}- err = db.DeleteRepoLock(repoLockId, planId)+ errCh <- nil+ }()++ go func() {+ res, err := db.GetPlanSubtasks(auth.OrgId, planId)if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error unlocking repo",- }+ log.Printf("Error getting plan subtasks: %v\n", err)+ errCh <- fmt.Errorf("error getting plan subtasks: %v", err)return}+ subtasks = res+ errCh <- nil}()for i := 0; i < 4; i++ {@@ -298,20 +250,23 @@ func (state *activeTellStreamState) loadTellPlan() error {PlanId: planId,Contexts: modelContext,})+if err != nil {- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: fmt.Sprintf("Error getting current plan state: %v", err),- }- return err+ return fmt.Errorf("error getting current plan state: %v", err)}+currentPlan = resreturn nil- }()+ })if err != nil {+ log.Printf("execTellPlan: error loading tell plan: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error loading tell plan",+ }return err}diff --git a/app/server/model/plan/tell_stream_error.go b/app/server/model/plan/tell_stream_error.goindex d9affa7f..c1f85593 100644--- a/app/server/model/plan/tell_stream_error.go+++ b/app/server/model/plan/tell_stream_error.go@@ -81,97 +81,84 @@ func (state *activeTellStreamState) onError(params onErrorParams) onErrorResultstoreDescAndReply := func() error {ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)- repoLockId, err := db.LockRepo(- db.LockRepoParams{- UserId: state.currentUserId,- OrgId: state.currentOrgId,- PlanId: planId,- Branch: branch,- Scope: db.LockScopeWrite,- Ctx: ctx,- CancelFn: cancelFn,- },- )-- if err != nil {- log.Printf("Error locking repo for plan %s: %v\n", planId, err)- return err- } else {-- defer func() {- err := db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo for plan %s: %v\n", planId, err)+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: currentOrgId,+ UserId: state.currentUserId,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancelFn,+ Reason: "store desc and reply",+ }, func(repo *db.GitRepo) error {+ storedMessage := false+ storedDesc := false++ if convoMessageId == "" {+ hasUnfinishedSubtasks := false+ for _, subtask := range state.subtasks {+ if !subtask.IsFinished {+ hasUnfinishedSubtasks = true+ break+ }}- }()- err := db.GitClearUncommittedChanges(state.currentOrgId, planId, branch)- if err != nil {- log.Printf("Error clearing uncommitted changes for plan %s: %v\n", planId, err)- return err+ assistantMsg, msg, err := state.storeAssistantReply(repo, storeAssistantReplyParams{+ flags: shared.ConvoMessageFlags{+ CurrentStage: state.currentStage,+ HasUnfinishedSubtasks: hasUnfinishedSubtasks,+ },+ subtask: nil,+ addedSubtasks: nil,+ })+ if err == nil {+ convoMessageId = assistantMsg.Id+ commitMsg = msg+ storedMessage = true+ } else {+ log.Printf("Error storing assistant message after stream error: %v\n", err)+ return err+ }}- }-- storedMessage := false- storedDesc := false- if convoMessageId == "" {- hasUnfinishedSubtasks := false- for _, subtask := range state.subtasks {- if !subtask.IsFinished {- hasUnfinishedSubtasks = true- break+ if storeDesc && convoMessageId != "" {+ err := db.StoreDescription(&db.ConvoMessageDescription{+ OrgId: currentOrgId,+ PlanId: planId,+ SummarizedToMessageId: summarizedToMessageId,+ WroteFiles: false,+ ConvoMessageId: convoMessageId,+ BuildPathsInvalidated: map[string]bool{},+ Error: streamErr.Error(),+ })+ if err == nil {+ storedDesc = true+ } else {+ log.Printf("Error storing description after stream error: %v\n", err)+ return err}}- assistantMsg, msg, err := state.storeAssistantReply(storeAssistantReplyParams{- flags: shared.ConvoMessageFlags{- CurrentStage: state.currentStage,- HasUnfinishedSubtasks: hasUnfinishedSubtasks,- },- subtask: nil,- addedSubtasks: nil,- })- if err == nil {- convoMessageId = assistantMsg.Id- commitMsg = msg- storedMessage = true- } else {- log.Printf("Error storing assistant message after stream error: %v\n", err)- return err+ if storedMessage || storedDesc {+ err := repo.GitAddAndCommit(branch, commitMsg)+ if err != nil {+ log.Printf("Error committing after stream error: %v\n", err)+ return err+ }}- }- if storeDesc && convoMessageId != "" {- err := db.StoreDescription(&db.ConvoMessageDescription{- OrgId: currentOrgId,- PlanId: planId,- SummarizedToMessageId: summarizedToMessageId,- WroteFiles: false,- ConvoMessageId: convoMessageId,- BuildPathsInvalidated: map[string]bool{},- Error: streamErr.Error(),- })- if err == nil {- storedDesc = true- } else {- log.Printf("Error storing description after stream error: %v\n", err)- return err- }- }+ return nil+ })- if storedMessage || storedDesc {- err := db.GitAddAndCommit(currentOrgId, planId, branch, commitMsg)- if err != nil {- log.Printf("Error committing after stream error: %v\n", err)- return err- }+ if err != nil {+ log.Printf("Error storing description and reply after stream error: %v\n", err)+ return err}return nil}- storeDescAndReply()+ storeDescAndReply() // best effort to store description and reply, ignore errorsif params.streamApiErr != nil {active.StreamDoneCh <- params.streamApiErrdiff --git a/app/server/model/plan/tell_stream_main.go b/app/server/model/plan/tell_stream_main.goindex 7b54834c..32af1919 100644--- a/app/server/model/plan/tell_stream_main.go+++ b/app/server/model/plan/tell_stream_main.go@@ -56,7 +56,7 @@ mainLoop:return} else {res := state.onError(onErrorParams{- streamErr: fmt.Errorf("stream timeout due to inactivity | This usually means the model is not responding."),+ streamErr: fmt.Errorf("stream timeout due to inactivity | The model is not responding."),storeDesc: true,canRetry: true,})@@ -113,7 +113,7 @@ mainLoop:}res := state.onError(onErrorParams{- streamErr: fmt.Errorf("stream finished with no choices | This usually means the model failed to generate a valid response."),+ streamErr: fmt.Errorf("stream finished with no choices | The model failed to generate a valid response."),storeDesc: true,canRetry: true,})@@ -128,7 +128,7 @@ mainLoop:if len(response.Choices) > 1 {res := state.onError(onErrorParams{- streamErr: fmt.Errorf("stream finished with more than one choice | This usually means the model failed to generate a valid response."),+ streamErr: fmt.Errorf("stream finished with more than one choice | The model failed to generate a valid response."),storeDesc: true,canRetry: true,})@@ -154,7 +154,7 @@ mainLoop:if choice.FinishReason == "error" {res := state.onError(onErrorParams{- streamErr: fmt.Errorf("model stopped with error status | This usually means the model is not responding."),+ streamErr: fmt.Errorf("model stopped with error status | The model is not responding."),storeDesc: true,canRetry: true,})diff --git a/app/server/model/plan/tell_stream_store.go b/app/server/model/plan/tell_stream_store.goindex 27011840..ff599c17 100644--- a/app/server/model/plan/tell_stream_store.go+++ b/app/server/model/plan/tell_stream_store.go@@ -36,7 +36,6 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParamscurrentUserId := state.currentUserIdplanId := state.plan.Idbranch := state.branch- auth := state.authsummarizedToMessageId := state.summarizedToMessageIdactive := state.activePlanaddedSubtasks := params.addedSubtasks@@ -45,59 +44,16 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParamslog.Println("[storeOnFinished] Locking repo to store assistant reply and description")- repoLockId, err := db.LockRepo(- db.LockRepoParams{- OrgId: currentOrgId,- UserId: currentUserId,- PlanId: planId,- Branch: branch,- Scope: db.LockScopeWrite,- Ctx: active.Ctx,- CancelFn: active.CancelFn,- },- )-- if err != nil {- log.Printf("Error locking repo: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo",- }- return storeOnFinishedResult{- handleStreamFinishedResult: handleStreamFinishedResult{- shouldContinueMainLoop: true,- shouldReturn: false,- },- allSubtasksFinished: false,- }- }-- log.Println("[storeOnFinished] Locked repo for assistant reply and description")-- err = func() error {- defer func() {- if err != nil {- log.Printf("Error storing reply and description: %v\n", err)- err = db.GitClearUncommittedChanges(auth.OrgId, planId, branch)- if err != nil {- log.Printf("Error clearing uncommitted changes: %v\n", err)- }- }-- log.Println("[storeOnFinished] Unlocking repo for assistant reply and description")-- err = db.DeleteRepoLock(repoLockId, planId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error unlocking repo",- }- }- }()-+ err := db.ExecRepoOperation(db.ExecRepoOperationParams{+ OrgId: currentOrgId,+ UserId: currentUserId,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: active.Ctx,+ CancelFn: active.CancelFn,+ Reason: "store on finished",+ }, func(repo *db.GitRepo) error {// first resolve subtask stateif hasNewSubtasks || len(removedSubtasks) > 0 || subtaskFinished {if subtaskFinished && state.currentSubtask != nil {@@ -161,7 +117,7 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParamsflags.DidMakeDebuggingPlan = true}- assistantMsg, convoCommitMsg, err := state.storeAssistantReply(storeAssistantReplyParams{+ assistantMsg, convoCommitMsg, err := state.storeAssistantReply(repo, storeAssistantReplyParams{flags: flags,subtask: state.currentSubtask,addedSubtasks: addedSubtasks,@@ -223,7 +179,7 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParamslog.Println("Comitting after store on finished")- err = db.GitAddAndCommit(currentOrgId, planId, branch, convoCommitMsg)+ err = repo.GitAddAndCommit(branch, convoCommitMsg)if err != nil {state.onError(onErrorParams{streamErr: fmt.Errorf("failed to commit: %v", err),@@ -236,7 +192,25 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParamslog.Println("Assistant reply, description, and subtasks committed")return nil- }()+ })++ if err != nil {+ log.Printf("Error storing on finished: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error storing on finished",+ }+ return storeOnFinishedResult{+ handleStreamFinishedResult: handleStreamFinishedResult{+ shouldContinueMainLoop: true,+ shouldReturn: false,+ },+ allSubtasksFinished: false,+ }+ }++ log.Println("[storeOnFinished] Locked repo for assistant reply and description")if err != nil {return storeOnFinishedResult{@@ -262,7 +236,7 @@ type storeAssistantReplyParams struct {removedSubtasks []string}-func (state *activeTellStreamState) storeAssistantReply(params storeAssistantReplyParams) (*db.ConvoMessage, string, error) {+func (state *activeTellStreamState) storeAssistantReply(repo *db.GitRepo, params storeAssistantReplyParams) (*db.ConvoMessage, string, error) {flags := params.flagssubtask := params.subtaskaddedSubtasks := params.addedSubtasks@@ -301,7 +275,7 @@ func (state *activeTellStreamState) storeAssistantReply(params storeAssistantRepRemovedSubtasks: removedSubtasks,}- commitMsg, err := db.StoreConvoMessage(&assistantMsg, auth.User.Id, branch, false)+ commitMsg, err := db.StoreConvoMessage(repo, &assistantMsg, auth.User.Id, branch, false)if err != nil {log.Printf("Error storing assistant message: %v\n", err)diff --git a/app/server/model/summarize.go b/app/server/model/summarize.goindex 30cb4758..e0d6eb21 100644--- a/app/server/model/summarize.go+++ b/app/server/model/summarize.go@@ -3,10 +3,8 @@ package modelimport ("context""fmt"- "log""net/http""plandex-server/db"- "plandex-server/hooks""plandex-server/model/prompts""plandex-server/types""strings"@@ -56,42 +54,18 @@ func PlanSummary(clients map[string]ClientInfo, config shared.ModelRoleConfig, p},})- numTokens := GetMessagesTokenEstimate(messages...) + TokensPerRequest-- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: params.Auth,- Plan: params.Plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: numTokens,- OutputTokens: config.BaseModelConfig.MaxOutputTokens - numTokens,- ModelName: config.BaseModelConfig.ModelName,- },+ modelRes, err := ModelRequest(ctx, ModelRequestParams{+ Clients: clients,+ Auth: params.Auth,+ Plan: params.Plan,+ ModelConfig: &config,+ Purpose: "Plan summary",+ ConvoMessageId: params.LatestConvoMessageId,+ ModelStreamId: params.ModelStreamId,+ Messages: messages,})- if apiErr != nil {- return nil, apiErr- }-- fmt.Println("summarizing messages:")- // spew.Dump(messages)-- reqStarted := time.Now()- req := types.ExtendedChatCompletionRequest{- Model: config.BaseModelConfig.ModelName,- Messages: messages,- Temperature: config.Temperature,- TopP: config.TopP,- }-- resp, err := CreateChatCompletion(- clients,- &config,- ctx,- req,- )if err != nil {- fmt.Println("PlanSummary err:", err)-return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther,Status: http.StatusInternalServerError,@@ -99,76 +73,21 @@ func PlanSummary(clients map[string]ClientInfo, config shared.ModelRoleConfig, p}}- if len(resp.Choices) == 0 {- return nil, &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "plan summary - no choices in response. This usually means the model failed to generate a valid response.",- }- }-- content := resp.Choices[0].Message.Content-- var inputTokens int- var outputTokens int- if resp.Usage.CompletionTokens > 0 {- inputTokens = resp.Usage.PromptTokens- outputTokens = resp.Usage.CompletionTokens- } else {- inputTokens = numTokens- outputTokens = shared.GetNumTokensEstimate(content)- }-- var cachedTokens int- if resp.Usage.PromptTokensDetails != nil {- cachedTokens = resp.Usage.PromptTokensDetails.CachedTokens- }-- go func() {- _, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: params.Auth,- Plan: params.Plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: outputTokens,- CachedTokens: cachedTokens,- ModelName: config.BaseModelConfig.ModelName,- ModelProvider: config.BaseModelConfig.Provider,- ModelPackName: params.ModelPackName,- ModelRole: shared.ModelRolePlanSummary,- Purpose: "Generated plan summary",- GenerationId: resp.ID,- PlanId: params.Plan.Id,- ModelStreamId: params.ModelStreamId,- ConvoMessageId: params.LatestConvoMessageId,- BuildId: params.Plan.Id,-- RequestStartedAt: reqStarted,- Streaming: false,- Req: &req,- Res: &resp,- ModelConfig: &config,- },- })-- if apiErr != nil {- log.Printf("PlanSummary - error executing DidSendModelRequest hook: %v", apiErr)- }- }()-- // log.Println("Plan summary content:")- // log.Println(content)-- summary := content+ summary := modelRes.Contentif !strings.HasPrefix(summary, "## Summary of the plan so far:") {summary = "## Summary of the plan so far:\n\n" + summary}+ var tokens int+ if modelRes.Usage != nil {+ tokens = modelRes.Usage.CompletionTokens+ }+return &db.ConvoSummary{OrgId: params.Auth.OrgId,PlanId: params.Plan.Id,Summary: summary,- Tokens: resp.Usage.CompletionTokens,+ Tokens: tokens,LatestConvoMessageId: params.LatestConvoMessageId,LatestConvoMessageCreatedAt: params.LatestConvoMessageCreatedAt,NumMessages: params.NumMessages,