Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

- Fix for "conversation summary timestamp not found in conversation" error (https://github.com/plandex-ai/plandex/issues/274) - Fix for potential panic/crash during plan stream (https://github.com/plandex-ai/plandex/issues/275) - Better protection against panics/crashes in server goroutines across the board

Dane Schneider7db4b0729d57
diff --git a/app/server/db/context_helpers_conflicts.go b/app/server/db/context_helpers_conflicts.go
index e2a16702..f6f38851 100644
--- a/app/server/db/context_helpers_conflicts.go
+++ b/app/server/db/context_helpers_conflicts.go
@@ -2,7 +2,10 @@ package db
import (
"fmt"
+ "log"
shared "plandex-shared"
+ "runtime"
+ "runtime/debug"
)
type invalidateConflictedResultsParams struct {
@@ -74,6 +77,14 @@ func invalidateConflictedResults(params invalidateConflictedResultsParams) error
for _, desc := range toUpdateDescs {
go func(desc *ConvoMessageDescription) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in StoreDescription: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in StoreDescription: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
err := StoreDescription(desc)
if err != nil {
diff --git a/app/server/db/context_helpers_get.go b/app/server/db/context_helpers_get.go
index 02f2a33b..aa6170b9 100644
--- a/app/server/db/context_helpers_get.go
+++ b/app/server/db/context_helpers_get.go
@@ -3,8 +3,11 @@ package db
import (
"encoding/json"
"fmt"
+ "log"
"os"
"path/filepath"
+ "runtime"
+ "runtime/debug"
"sort"
"strings"
"sync"
@@ -31,6 +34,13 @@ func GetPlanContexts(orgId, planId string, includeBody, includeMapParts bool) ([
for _, file := range files {
if strings.HasSuffix(file.Name(), ".meta") {
go func(file os.DirEntry) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetPlanContexts: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetPlanContexts: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
context, err := GetContext(orgId, planId, strings.TrimSuffix(file.Name(), ".meta"), includeBody, includeMapParts)
mu.Lock()
diff --git a/app/server/db/context_helpers_load.go b/app/server/db/context_helpers_load.go
index 01a6ffc2..7c09ad06 100644
--- a/app/server/db/context_helpers_load.go
+++ b/app/server/db/context_helpers_load.go
@@ -7,6 +7,8 @@ import (
"fmt"
"log"
shared "plandex-shared"
+ "runtime"
+ "runtime/debug"
"strings"
"sync"
@@ -289,6 +291,13 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextRespon
for tempId, loadParams := range paramsByTempId {
go func(tempId string, loadParams *shared.LoadContextParams) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in LoadContexts: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in LoadContexts: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
hash := sha256.Sum256([]byte(loadParams.Body))
sha := hex.EncodeToString(hash[:])
diff --git a/app/server/db/context_helpers_remove.go b/app/server/db/context_helpers_remove.go
index c9e313b0..1303d737 100644
--- a/app/server/db/context_helpers_remove.go
+++ b/app/server/db/context_helpers_remove.go
@@ -2,9 +2,12 @@ package db
import (
"fmt"
+ "log"
"os"
"path/filepath"
shared "plandex-shared"
+ "runtime"
+ "runtime/debug"
)
func ContextRemove(orgId, planId string, contexts []*Context) error {
@@ -40,6 +43,13 @@ func contextRemove(params contextRemoveParams) error {
for _, ext := range []string{".meta", ".body", ".map-parts"} {
numFiles++
go func(context *Context, dir, ext string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in contextRemove: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in contextRemove: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
errCh <- os.Remove(filepath.Join(dir, context.Id+ext))
}(context, contextDir, ext)
}
diff --git a/app/server/db/context_helpers_update.go b/app/server/db/context_helpers_update.go
index 2d2a5379..26ca8fa8 100644
--- a/app/server/db/context_helpers_update.go
+++ b/app/server/db/context_helpers_update.go
@@ -4,7 +4,10 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
+ "log"
shared "plandex-shared"
+ "runtime"
+ "runtime/debug"
"sync"
)
@@ -126,6 +129,13 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
for id, params := range *req {
go func(id string, params *shared.UpdateContextParams) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in UpdateContexts: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in UpdateContexts: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
var context *Context
if _, ok := contextsById[id]; ok {
context = contextsById[id]
@@ -229,6 +239,13 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
for id, params := range *req {
go func(id string, params *shared.UpdateContextParams) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in UpdateContexts: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in UpdateContexts: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
context := contextsById[id]
if context.ContextType == shared.ContextMapType {
diff --git a/app/server/db/convo_helpers.go b/app/server/db/convo_helpers.go
index 79702216..442c57ea 100644
--- a/app/server/db/convo_helpers.go
+++ b/app/server/db/convo_helpers.go
@@ -6,6 +6,8 @@ import (
"log"
"os"
"path/filepath"
+ "runtime"
+ "runtime/debug"
"sort"
"strings"
"time"
@@ -35,6 +37,13 @@ func GetPlanConvo(orgId, planId string) ([]*ConvoMessage, error) {
for _, file := range files {
go func(file os.DirEntry) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetPlanConvo: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetPlanConvo: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
bytes, err := os.ReadFile(filepath.Join(convoDir, file.Name()))
if err != nil {
diff --git a/app/server/db/diff_helpers.go b/app/server/db/diff_helpers.go
index 3cf758cc..57f00189 100644
--- a/app/server/db/diff_helpers.go
+++ b/app/server/db/diff_helpers.go
@@ -2,9 +2,12 @@ package db
import (
"fmt"
+ "log"
"os"
"os/exec"
"path/filepath"
+ "runtime"
+ "runtime/debug"
shared "plandex-shared"
)
@@ -46,6 +49,13 @@ func GetPlanDiffs(orgId, planId string, plain bool) (string, error) {
for path, context := range planState.ContextsByPath {
go func(path string, context *shared.Context) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
_, hasPath := files[path]
_, hasRemoved := removed[path]
if hasPath || hasRemoved {
@@ -92,6 +102,13 @@ func GetPlanDiffs(orgId, planId string, plain bool) (string, error) {
for path, file := range files {
go func(path, file string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
// ensure file directory exists
err = os.MkdirAll(filepath.Dir(filepath.Join(tempDirPath, path)), 0755)
if err != nil {
@@ -110,6 +127,13 @@ func GetPlanDiffs(orgId, planId string, plain bool) (string, error) {
for path, shouldRemove := range removed {
go func(path string, shouldRemove bool) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetPlanDiffs: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
if shouldRemove {
err = os.RemoveAll(filepath.Join(tempDirPath, path))
if err != nil {
diff --git a/app/server/db/git.go b/app/server/db/git.go
index ca186996..b2d970a3 100644
--- a/app/server/db/git.go
+++ b/app/server/db/git.go
@@ -7,6 +7,8 @@ import (
"os"
"os/exec"
"path/filepath"
+ "runtime"
+ "runtime/debug"
"strconv"
"strings"
"time"
@@ -612,6 +614,13 @@ func gitRemoveIndexLockFileIfExists(repoDir string) error {
for _, path := range paths {
go func(path string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in gitRemoveIndexLockFileIfExists: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in gitRemoveIndexLockFileIfExists: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
if err := removeLockFile(path); err != nil {
errCh <- err
return
diff --git a/app/server/db/locks.go b/app/server/db/locks.go
index 4c5289b3..eee53277 100644
--- a/app/server/db/locks.go
+++ b/app/server/db/locks.go
@@ -8,8 +8,10 @@ import (
"log"
"math"
"math/rand"
+ "plandex-server/notify"
"plandex-server/shutdown"
"runtime"
+ "runtime/debug"
"strconv"
"strings"
"sync"
@@ -384,6 +386,14 @@ func lockRepoDB(params LockRepoParams, numRetry int) (string, error) {
// Start a goroutine to keep the lock alive
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in heartbeat goroutine: %v\n%s", r, debug.Stack())
+ cancelFn()
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("panic in lock heartbeat goroutine: %v\n%s", r, debug.Stack()))
+ }
+ }()
+
onCancel := func() {
log.Printf("[Lock][Heartbeat] Timeout or context canceled during heartbeat loop for lock %s for plan %s | reason: %s", newLock.Id, planId, params.Reason)
}
diff --git a/app/server/db/plan_helpers.go b/app/server/db/plan_helpers.go
index 90231411..71256b31 100644
--- a/app/server/db/plan_helpers.go
+++ b/app/server/db/plan_helpers.go
@@ -7,6 +7,8 @@ import (
"log"
"os"
"path/filepath"
+ "runtime"
+ "runtime/debug"
"strconv"
"time"
@@ -138,9 +140,17 @@ func AddPlanContextTokens(planId, branch string, addTokens int) error {
}
func AddPlanConvoMessage(msg *ConvoMessage, branch string) error {
- errCh := make(chan error)
+ errCh := make(chan error, 2)
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in AddPlanConvoMessage: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in AddPlanConvoMessage: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
_, err := Conn.Exec("UPDATE branches SET convo_tokens = convo_tokens + $1 WHERE plan_id = $2 AND name = $3", msg.Tokens, msg.PlanId, branch)
if err != nil {
@@ -152,6 +162,14 @@ func AddPlanConvoMessage(msg *ConvoMessage, branch string) error {
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in AddPlanConvoMessage: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in AddPlanConvoMessage: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
if msg.Role != openai.ChatMessageRoleAssistant {
errCh <- nil
return
@@ -177,15 +195,29 @@ func AddPlanConvoMessage(msg *ConvoMessage, branch string) error {
func SyncPlanTokens(orgId, planId, branch string) error {
var contexts []*Context
var convos []*ConvoMessage
- errCh := make(chan error)
+ errCh := make(chan error, 2)
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in SyncPlanTokens: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in SyncPlanTokens: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
var err error
contexts, err = GetPlanContexts(orgId, planId, false, false)
errCh <- err
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in SyncPlanTokens: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in SyncPlanTokens: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
var err error
convos, err = GetPlanConvo(orgId, planId)
errCh <- err
@@ -337,9 +369,16 @@ func DeleteDraftPlans(orgId, projectId, userId string) error {
ids = append(ids, id)
}
- errCh := make(chan error)
+ errCh := make(chan error, len(ids))
for _, planId := range ids {
go func(planId string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in DeleteDraftPlans: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in DeleteDraftPlans: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
errCh <- DeletePlanDir(orgId, planId)
}(planId)
}
@@ -378,9 +417,16 @@ func DeleteOwnerPlans(orgId, projectId, userId string) error {
ids = append(ids, id)
}
- errCh := make(chan error)
+ errCh := make(chan error, len(ids))
for _, planId := range ids {
go func(planId string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in DeleteOwnerPlans: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in DeleteOwnerPlans: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
errCh <- DeletePlanDir(orgId, planId)
}(planId)
}
diff --git a/app/server/db/result_helpers.go b/app/server/db/result_helpers.go
index bbc485da..5f61688b 100644
--- a/app/server/db/result_helpers.go
+++ b/app/server/db/result_helpers.go
@@ -7,6 +7,8 @@ import (
"log"
"os"
"path/filepath"
+ "runtime"
+ "runtime/debug"
"sort"
"strings"
"sync"
@@ -60,13 +62,21 @@ type CurrentPlanStateParams struct {
}
func GetFullCurrentPlanStateParams(orgId, planId string) (CurrentPlanStateParams, error) {
- errCh := make(chan error)
+ errCh := make(chan error, 3)
var results []*PlanFileResult
var convoMessageDescriptions []*ConvoMessageDescription
var contexts []*Context
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetFullCurrentPlanStateParams: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetFullCurrentPlanStateParams: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
res, err := GetPlanFileResults(orgId, planId)
if err != nil {
errCh <- fmt.Errorf("error getting plan file results: %v", err)
@@ -77,6 +87,13 @@ func GetFullCurrentPlanStateParams(orgId, planId string) (CurrentPlanStateParams
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetFullCurrentPlanStateParams: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetFullCurrentPlanStateParams: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
res, err := GetConvoMessageDescriptions(orgId, planId)
if err != nil {
errCh <- fmt.Errorf("error getting latest plan build description: %v", err)
@@ -87,6 +104,13 @@ func GetFullCurrentPlanStateParams(orgId, planId string) (CurrentPlanStateParams
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetFullCurrentPlanStateParams: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetFullCurrentPlanStateParams: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
res, err := GetPlanContexts(orgId, planId, true, false)
if err != nil {
errCh <- fmt.Errorf("error getting contexts: %v", err)
@@ -122,9 +146,16 @@ func GetCurrentPlanState(params CurrentPlanStateParams) (*shared.CurrentPlanStat
var convoMessageDescriptions []*shared.ConvoMessageDescription
contextsByPath := map[string]*Context{}
planApplies := []*shared.PlanApply{}
- errCh := make(chan error)
+ errCh := make(chan error, 4)
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
if params.PlanFileResults == nil {
res, err := GetPlanFileResults(orgId, planId)
dbPlanFileResults = res
@@ -141,6 +172,13 @@ func GetCurrentPlanState(params CurrentPlanStateParams) (*shared.CurrentPlanStat
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
if params.ConvoMessageDescriptions == nil {
res, err := GetConvoMessageDescriptions(orgId, planId)
if err != nil {
@@ -161,6 +199,13 @@ func GetCurrentPlanState(params CurrentPlanStateParams) (*shared.CurrentPlanStat
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
var contexts []*Context
if params.Contexts == nil {
res, err := GetPlanContexts(orgId, planId, true, false)
@@ -185,6 +230,13 @@ func GetCurrentPlanState(params CurrentPlanStateParams) (*shared.CurrentPlanStat
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetCurrentPlanState: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
res, err := GetPlanApplies(orgId, planId)
if err != nil {
errCh <- fmt.Errorf("error getting plan applies: %v", err)
@@ -273,6 +325,13 @@ func GetConvoMessageDescriptions(orgId, planId string) ([]*ConvoMessageDescripti
for _, file := range files {
go func(file os.DirEntry) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetConvoMessageDescriptions: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetConvoMessageDescriptions: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
path := filepath.Join(descriptionsDir, file.Name())
bytes, err := os.ReadFile(path)
@@ -338,6 +397,13 @@ func GetPlanFileResults(orgId, planId string) ([]*PlanFileResult, error) {
// log.Printf("Result file: %s", file.Name())
go func(file os.DirEntry) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetPlanFileResults: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetPlanFileResults: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
bytes, err := os.ReadFile(filepath.Join(resultsDir, file.Name()))
@@ -489,11 +555,28 @@ func ApplyPlan(repo *GitRepo, ctx context.Context, params ApplyPlanParams) error
var loadContextRes *shared.LoadContextResponse
var updateContextRes *shared.UpdateContextResponse
- errCh := make(chan error)
+ numRoutines := len(pendingDbResults) +
+ len(convoMessageDescriptions)
+
+ if len(pendingNewFilesSet) > 0 {
+ numRoutines++
+ }
+ if len(pendingUpdatedFilesSet) > 0 {
+ numRoutines++
+ }
+
+ errCh := make(chan error, numRoutines)
now := time.Now()
for _, result := range pendingDbResults {
go func(result *PlanFileResult) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
result.AppliedAt = &now
bytes, err := json.MarshalIndent(result, "", " ")
@@ -517,6 +600,13 @@ func ApplyPlan(repo *GitRepo, ctx context.Context, params ApplyPlanParams) error
for _, description := range convoMessageDescriptions {
go func(description *ConvoMessageDescription) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
description.AppliedAt = &now
err := StoreDescription(description)
@@ -532,6 +622,13 @@ func ApplyPlan(repo *GitRepo, ctx context.Context, params ApplyPlanParams) error
if len(pendingNewFilesSet) > 0 {
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
loadReq := shared.LoadContextRequest{}
for path := range pendingNewFilesSet {
loadReq = append(loadReq, &shared.LoadContextParams{
@@ -570,6 +667,13 @@ func ApplyPlan(repo *GitRepo, ctx context.Context, params ApplyPlanParams) error
if len(pendingUpdatedFilesSet) > 0 {
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in ApplyPlan: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
updateReq := shared.UpdateContextRequest{}
for path := range pendingUpdatedFilesSet {
context := contextsByPath[path]
@@ -602,16 +706,6 @@ func ApplyPlan(repo *GitRepo, ctx context.Context, params ApplyPlanParams) error
}
- numRoutines := len(pendingDbResults) +
- len(convoMessageDescriptions)
-
- if len(pendingNewFilesSet) > 0 {
- numRoutines++
- }
- if len(pendingUpdatedFilesSet) > 0 {
- numRoutines++
- }
-
for i := 0; i < numRoutines; i++ {
err := <-errCh
if err != nil {
@@ -713,6 +807,13 @@ func RejectAllResults(orgId, planId string) error {
resultId := strings.TrimSuffix(file.Name(), ".json")
go func(resultId string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in RejectAllResults: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in RejectAllResults: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
err := RejectPlanFile(orgId, planId, resultId, now)
if err != nil {
@@ -753,6 +854,13 @@ func DeletePendingResultsForPaths(orgId, planId string, paths map[string]bool) e
resultId := strings.TrimSuffix(file.Name(), ".json")
go func(resultId string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in DeletePendingResultsForPaths: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in DeletePendingResultsForPaths: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
bytes, err := os.ReadFile(filepath.Join(resultsDir, resultId+".json"))
if err != nil {
@@ -800,6 +908,13 @@ func RejectPlanFiles(orgId, planId string, files []string, now time.Time) error
for _, file := range files {
go func(file string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in RejectPlanFiles: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in RejectPlanFiles: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
err := RejectPlanFile(orgId, planId, file, now)
if err != nil {
@@ -833,6 +948,13 @@ func RejectPlanFile(orgId, planId, filePathOrResultId string, now time.Time) err
for _, result := range results {
go func(result *PlanFileResult) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in RejectPlanFile: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in RejectPlanFile: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
if (result.Path == filePathOrResultId || result.Id == filePathOrResultId) && result.AppliedAt == nil && result.RejectedAt == nil {
result.RejectedAt = &now
} else {
@@ -923,6 +1045,13 @@ func GetPlanApplies(orgId, planId string) ([]*PlanApply, error) {
for _, file := range files {
go func(file os.DirEntry) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GetPlanApplies: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GetPlanApplies: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
bytes, err := os.ReadFile(filepath.Join(appliesDir, file.Name()))
if err != nil {
diff --git a/app/server/db/stream_helpers.go b/app/server/db/stream_helpers.go
index c3a80a6d..19d5370d 100644
--- a/app/server/db/stream_helpers.go
+++ b/app/server/db/stream_helpers.go
@@ -5,6 +5,8 @@ import (
"database/sql"
"fmt"
"log"
+ "plandex-server/notify"
+ "runtime/debug"
"time"
shared "plandex-shared"
@@ -39,6 +41,14 @@ func StoreModelStream(stream *ModelStream, ctx context.Context, cancelFn context
// Start a goroutine to keep the lock alive
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in StoreModelStream: %v\n%s", r, debug.Stack())
+ cancelFn()
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("panic in StoreModelStream: %v\n%s", r, debug.Stack()))
+ }
+ }()
+
numErrors := 0
for {
select {
diff --git a/app/server/handlers/context_helper.go b/app/server/handlers/context_helper.go
index 1197fddb..38eab4e1 100644
--- a/app/server/handlers/context_helper.go
+++ b/app/server/handlers/context_helper.go
@@ -9,6 +9,8 @@ import (
"plandex-server/db"
"plandex-server/model"
"plandex-server/types"
+ "runtime"
+ "runtime/debug"
shared "plandex-shared"
)
@@ -133,6 +135,14 @@ func loadContexts(
num++
go func(context *shared.LoadContextParams) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GenPipedDataName: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GenPipedDataName: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
name, err := model.GenPipedDataName(r.Context(), auth, plan, settings, clients, context.Body, context.SessionId)
if err != nil {
@@ -147,6 +157,14 @@ func loadContexts(
num++
go func(context *shared.LoadContextParams) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in GenNoteName: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in GenNoteName: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
name, err := model.GenNoteName(r.Context(), auth, plan, settings, clients, context.Body, context.SessionId)
if err != nil {
diff --git a/app/server/handlers/file_maps.go b/app/server/handlers/file_maps.go
index c0c0bd46..c07601e6 100644
--- a/app/server/handlers/file_maps.go
+++ b/app/server/handlers/file_maps.go
@@ -6,6 +6,8 @@ import (
"log"
"net/http"
"plandex-server/db"
+ "runtime"
+ "runtime/debug"
"sync"
shared "plandex-shared"
@@ -132,6 +134,14 @@ func LoadCachedFileMapHandler(w http.ResponseWriter, r *http.Request) {
for _, path := range req.FilePaths {
go func(path string) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in LoadCachedFileMapHandler: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in LoadCachedFileMapHandler: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
cachedContext, err := db.GetCachedMap(plan.OrgId, plan.ProjectId, path)
if err != nil {
errCh <- fmt.Errorf("error getting cached map: %v", err)
@@ -155,6 +165,7 @@ func LoadCachedFileMapHandler(w http.ResponseWriter, r *http.Request) {
for range req.FilePaths {
err := <-errCh
if err != nil {
+ log.Printf("Error getting cached map: %v", err)
http.Error(w, fmt.Sprintf("Error getting cached map: %v", err), http.StatusInternalServerError)
return
}
diff --git a/app/server/handlers/plans_crud.go b/app/server/handlers/plans_crud.go
index 32774b63..a3e52919 100644
--- a/app/server/handlers/plans_crud.go
+++ b/app/server/handlers/plans_crud.go
@@ -8,6 +8,8 @@ import (
"net/http"
"plandex-server/db"
"plandex-server/hooks"
+ "runtime"
+ "runtime/debug"
"sort"
"strings"
"time"
@@ -435,11 +437,19 @@ func ListPlansRunningHandler(w http.ResponseWriter, r *http.Request) {
planIds = append(planIds, plan.Id)
}
- errCh := make(chan error)
+ errCh := make(chan error, 2)
var streams []*db.ModelStream
var branches []*db.Branch
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in ListPlansRunningHandler: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in ListPlansRunningHandler: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
var err error
if includeRecent {
streams, err = db.GetActiveOrRecentModelStreams(planIds)
@@ -454,6 +464,13 @@ func ListPlansRunningHandler(w http.ResponseWriter, r *http.Request) {
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in ListPlansRunningHandler: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("panic in ListPlansRunningHandler: %v\n%s", r, debug.Stack())
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
var err error
branches, err = db.ListBranchesForPlans(auth.OrgId, planIds)
if err != nil {
diff --git a/app/server/model/model_request.go b/app/server/model/model_request.go
index c483d38c..7242dea0 100644
--- a/app/server/model/model_request.go
+++ b/app/server/model/model_request.go
@@ -6,8 +6,10 @@ import (
"log"
"plandex-server/db"
"plandex-server/hooks"
+ "plandex-server/notify"
"plandex-server/types"
shared "plandex-shared"
+ "runtime/debug"
"strings"
"time"
@@ -196,6 +198,13 @@ func ModelRequest(
}
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in DidSendModelRequest hook: %v\n%s", r, debug.Stack())
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("panic in DidSendModelRequest hook: %v\n%s", r, debug.Stack()))
+ }
+ }()
+
_, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{
Auth: auth,
Plan: plan,
@@ -227,6 +236,7 @@ func ModelRequest(
if apiErr != nil {
log.Printf("buildWholeFile - error executing DidSendModelRequest hook: %v", apiErr)
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("error executing DidSendModelRequest hook: %v", apiErr))
}
}()
diff --git a/app/server/model/plan/build_finish.go b/app/server/model/plan/build_finish.go
index 312e999f..2f61eba0 100644
--- a/app/server/model/plan/build_finish.go
+++ b/app/server/model/plan/build_finish.go
@@ -98,7 +98,7 @@ func (state *activeBuildStreamFileState) onFinishBuild() {
return fmt.Errorf("error getting current plan state: %v", err)
}
- descErrCh := make(chan error)
+ descErrCh := make(chan error, len(unbuiltDescs))
for _, desc := range unbuiltDescs {
if len(desc.Operations) > 0 {
desc.DidBuild = true
diff --git a/app/server/model/plan/build_load.go b/app/server/model/plan/build_load.go
index a93e49d0..0ea2dd27 100644
--- a/app/server/model/plan/build_load.go
+++ b/app/server/model/plan/build_load.go
@@ -8,6 +8,8 @@ import (
"plandex-server/notify"
"plandex-server/syntax"
"plandex-server/types"
+ "runtime"
+ "runtime/debug"
shared "plandex-shared"
)
@@ -41,9 +43,16 @@ func (state *activeBuildStreamState) loadPendingBuilds(sessionId string) (map[st
CancelFn: active.CancelFn,
Reason: "load pending builds",
}, func(repo *db.GitRepo) error {
- errCh := make(chan error)
+ errCh := make(chan error, 3)
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanContexts: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan modelContext: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
res, err := db.GetPlanContexts(auth.OrgId, plan.Id, true, false)
if err != nil {
log.Printf("Error getting plan modelContext: %v\n", err)
@@ -56,6 +65,13 @@ func (state *activeBuildStreamState) loadPendingBuilds(sessionId string) (map[st
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanSettings: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan settings: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
res, err := active.PendingBuildsByPath(auth.OrgId, auth.User.Id, nil)
if err != nil {
@@ -70,6 +86,13 @@ func (state *activeBuildStreamState) loadPendingBuilds(sessionId string) (map[st
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanSettings: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan settings: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
res, err := db.GetPlanSettings(plan, true)
if err != nil {
log.Printf("Error getting plan settings: %v\n", err)
@@ -184,9 +207,16 @@ func (state *activeBuildStreamFileState) loadBuildFile(activeBuild *types.Active
CancelFn: activePlan.CancelFn,
Reason: "load build file",
}, func(repo *db.GitRepo) error {
- errCh := make(chan error)
+ errCh := make(chan error, 2)
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getCurrentPlanState: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting current plan state: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
log.Println("loadBuildFile - Getting current plan state")
res, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{
OrgId: currentOrgId,
@@ -214,6 +244,13 @@ func (state *activeBuildStreamFileState) loadBuildFile(activeBuild *types.Active
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanConvo: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan convo: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
res, err := db.GetPlanConvo(currentOrgId, planId)
if err != nil {
log.Printf("Error getting plan convo: %v\n", err)
diff --git a/app/server/model/plan/build_race.go b/app/server/model/plan/build_race.go
index 4be6dadc..408d2dd2 100644
--- a/app/server/model/plan/build_race.go
+++ b/app/server/model/plan/build_race.go
@@ -7,6 +7,8 @@ import (
"log"
"plandex-server/syntax"
"plandex-server/utils"
+ "runtime"
+ "runtime/debug"
"strings"
"time"
)
@@ -78,6 +80,13 @@ func (fileState *activeBuildStreamFileState) buildRace(
startWholeFileBuild := func(comments string) {
log.Printf("buildRace - starting whole file fallback build")
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in startWholeFileBuild: %v\n%s", r, debug.Stack())
+ sendErr(fmt.Errorf("error starting whole file build: %v", r))
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
select {
case <-buildCtx.Done():
log.Printf("buildRace - context already canceled, skipping whole file build")
@@ -112,6 +121,14 @@ func (fileState *activeBuildStreamFileState) buildRace(
}
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in maybeStartFastApply: %v\n%s", r, debug.Stack())
+ sendErr(fmt.Errorf("error starting fast apply: %v", r))
+ onFail()
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
var fastApplyRes string
select {
@@ -208,6 +225,14 @@ func (fileState *activeBuildStreamFileState) buildRace(
fileState.builderRun.AutoApplyValidationStartedAt = time.Now()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in buildRace validation loop: %v\n%s", r, debug.Stack())
+ sendErr(fmt.Errorf("error building validate loop: %v", r))
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
log.Printf("buildRace - starting validation loop")
validateResult, err := fileState.buildValidateLoop(buildCtx, buildValidateLoopParams{
originalFile: originalFile,
diff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.go
index 9e35a835..1a066524 100644
--- a/app/server/model/plan/build_structured_edits.go
+++ b/app/server/model/plan/build_structured_edits.go
@@ -9,6 +9,8 @@ import (
"plandex-server/hooks"
"plandex-server/syntax"
"plandex-server/utils"
+ "runtime"
+ "runtime/debug"
"strings"
"time"
@@ -56,6 +58,14 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
calledFastApply = true
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in callFastApply: %v\n%s", r, debug.Stack())
+ fastApplyCh <- ""
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
res, err := hooks.ExecHook(hooks.CallFastApply, hooks.HookParams{
FastApplyParams: &hooks.FastApplyParams{
InitialCode: originalFile,
diff --git a/app/server/model/plan/exec_status.go b/app/server/model/plan/exec_status.go
index 6e80a1e0..f222e229 100644
--- a/app/server/model/plan/exec_status.go
+++ b/app/server/model/plan/exec_status.go
@@ -34,76 +34,83 @@ func (state *activeTellStreamState) execStatusShouldContinue(currentMessage stri
clients := state.clients
config := settings.ModelPack.ExecStatus
+ currentSubtask := state.currentSubtask
+
+ if currentSubtask == nil {
+ log.Printf("[ExecStatus] No current subtask")
+ return execStatusShouldContinueResult{
+ subtaskFinished: true,
+ }, nil
+ }
+
// Check subtask completion
- if state.currentSubtask != nil {
- completionMarker := fmt.Sprintf("**%s** has been completed", state.currentSubtask.Title)
- log.Printf("[ExecStatus] Checking for subtask completion marker: %q", completionMarker)
- log.Printf("[ExecStatus] Current subtask: %q", state.currentSubtask.Title)
-
- if strings.Contains(currentMessage, completionMarker) {
- log.Printf("[ExecStatus] ✓ Subtask completion marker found")
- return execStatusShouldContinueResult{
- subtaskFinished: true,
- }, nil
-
- // NOTE: tried using an LLM to verify "suspicious" subtask completions, but in practice led to too many extra LLM calls and disagreement cycles between agent roles (it's finished. no it's note! etc.)
- // now just going back to trusting the completion marker... basically it's better to err on the side of marking tasks done.
-
- // var potentialProblem bool
-
- // if len(state.chunkProcessor.replyOperations) == 0 {
- // log.Printf("[ExecStatus] ✗ Subtask completion marker found, but there are no operations to execute")
- // potentialProblem = true
- // } else {
- // wroteToPaths := map[string]bool{}
- // for _, op := range state.chunkProcessor.replyOperations {
- // if op.Type == shared.OperationTypeFile {
- // wroteToPaths[op.Path] = true
- // }
- // }
-
- // for _, path := range state.currentSubtask.UsesFiles {
- // if !wroteToPaths[path] {
- // log.Printf("[ExecStatus] ✗ Subtask completion marker found, but the operations did not write to the file %q from the 'Uses' list", path)
- // potentialProblem = true
- // break
- // }
- // }
- // }
-
- // if !potentialProblem {
- // log.Printf("[ExecStatus] ✓ Subtask completion marker found and no potential problem - will mark as completed")
-
- // return execStatusShouldContinueResult{
- // subtaskFinished: true,
- // }, nil
- // } else if state.currentSubtask.NumTries >= 1 {
- // log.Printf("[ExecStatus] ✓ Subtask completion marker found, but the operations are questionable -- marking it done anyway since it's the second try and we can't risk an infinite loop")
-
- // return execStatusShouldContinueResult{
- // subtaskFinished: true,
- // }, nil
- // } else {
- // log.Printf("[ExecStatus] ✗ Subtask completion marker found, but the operations are questionable -- will verify with LLM call")
- // }
- } else {
- log.Printf("[ExecStatus] ✗ No subtask completion marker found in message")
- }
+ completionMarker := fmt.Sprintf("**%s** has been completed", currentSubtask.Title)
+ log.Printf("[ExecStatus] Checking for subtask completion marker: %q", completionMarker)
+ log.Printf("[ExecStatus] Current subtask: %q", currentSubtask.Title)
- log.Println("[ExecStatus] Current subtasks state:")
- for i, task := range state.subtasks {
- log.Printf("[ExecStatus] Task %d: %q (finished=%v)", i+1, task.Title, task.IsFinished)
- }
+ if strings.Contains(currentMessage, completionMarker) {
+ log.Printf("[ExecStatus] ✓ Subtask completion marker found")
+ return execStatusShouldContinueResult{
+ subtaskFinished: true,
+ }, nil
+
+ // NOTE: tried using an LLM to verify "suspicious" subtask completions, but in practice led to too many extra LLM calls and disagreement cycles between agent roles (it's finished. no it's note! etc.)
+ // now just going back to trusting the completion marker... basically it's better to err on the side of marking tasks done.
+
+ // var potentialProblem bool
+
+ // if len(state.chunkProcessor.replyOperations) == 0 {
+ // log.Printf("[ExecStatus] ✗ Subtask completion marker found, but there are no operations to execute")
+ // potentialProblem = true
+ // } else {
+ // wroteToPaths := map[string]bool{}
+ // for _, op := range state.chunkProcessor.replyOperations {
+ // if op.Type == shared.OperationTypeFile {
+ // wroteToPaths[op.Path] = true
+ // }
+ // }
+
+ // for _, path := range currentSubtask.UsesFiles {
+ // if !wroteToPaths[path] {
+ // log.Printf("[ExecStatus] ✗ Subtask completion marker found, but the operations did not write to the file %q from the 'Uses' list", path)
+ // potentialProblem = true
+ // break
+ // }
+ // }
+ // }
+
+ // if !potentialProblem {
+ // log.Printf("[ExecStatus] ✓ Subtask completion marker found and no potential problem - will mark as completed")
+
+ // return execStatusShouldContinueResult{
+ // subtaskFinished: true,
+ // }, nil
+ // } else if currentSubtask.NumTries >= 1 {
+ // log.Printf("[ExecStatus] ✓ Subtask completion marker found, but the operations are questionable -- marking it done anyway since it's the second try and we can't risk an infinite loop")
+
+ // return execStatusShouldContinueResult{
+ // subtaskFinished: true,
+ // }, nil
+ // } else {
+ // log.Printf("[ExecStatus] ✗ Subtask completion marker found, but the operations are questionable -- will verify with LLM call")
+ // }
+ } else {
+ log.Printf("[ExecStatus] ✗ No subtask completion marker found in message")
+ }
+
+ log.Println("[ExecStatus] Current subtasks state:")
+ for i, task := range state.subtasks {
+ log.Printf("[ExecStatus] Task %d: %q (finished=%v)", i+1, task.Title, task.IsFinished)
}
log.Println("Checking if plan should continue based on exec status")
- fullSubtask := state.currentSubtask.Title
- fullSubtask += "\n\n" + state.currentSubtask.Description
+ fullSubtask := currentSubtask.Title
+ fullSubtask += "\n\n" + currentSubtask.Description
previousMessages := []string{}
for _, msg := range state.convo {
- if msg.Subtask != nil && msg.Subtask.Title == state.currentSubtask.Title {
+ if msg.Subtask != nil && msg.Subtask.Title == currentSubtask.Title {
previousMessages = append(previousMessages, msg.Message)
}
}
diff --git a/app/server/model/plan/state.go b/app/server/model/plan/state.go
index ff2799f4..f888f334 100644
--- a/app/server/model/plan/state.go
+++ b/app/server/model/plan/state.go
@@ -2,8 +2,10 @@ package plan
import (
"context"
+ "fmt"
"log"
"plandex-server/db"
+ "plandex-server/notify"
"plandex-server/shutdown"
"plandex-server/types"
"strings"
@@ -60,6 +62,8 @@ func CreateActivePlan(orgId, userId, planId, branch, prompt string, buildOnly, a
} else {
log.Printf("Error streaming plan %s: %v\n", planId, apiErr)
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("error streaming plan %s: %v", planId, apiErr))
+
err := db.SetPlanStatus(planId, branch, shared.PlanStatusError, apiErr.Msg)
if err != nil {
log.Printf("Error setting plan %s status to error: %v\n", planId, err)
diff --git a/app/server/model/plan/tell_build_pending.go b/app/server/model/plan/tell_build_pending.go
index e8037ce7..b492d446 100644
--- a/app/server/model/plan/tell_build_pending.go
+++ b/app/server/model/plan/tell_build_pending.go
@@ -5,6 +5,7 @@ import (
"log"
"net/http"
"plandex-server/notify"
+ "runtime/debug"
shared "plandex-shared"
)
@@ -24,6 +25,18 @@ func (state *activeTellStreamState) queuePendingBuilds() {
return
}
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in queuePendingBuilds: %v\n%s", r, debug.Stack())
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("error getting pending builds by path: %v", r))
+ active.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: fmt.Sprintf("Error getting pending builds by path: %v", r),
+ }
+ }
+ }()
+
pendingBuildsByPath, err := active.PendingBuildsByPath(auth.OrgId, auth.User.Id, state.convo)
if err != nil {
diff --git a/app/server/model/plan/tell_load.go b/app/server/model/plan/tell_load.go
index 28d50101..b76cc46a 100644
--- a/app/server/model/plan/tell_load.go
+++ b/app/server/model/plan/tell_load.go
@@ -8,6 +8,8 @@ import (
"plandex-server/model"
"plandex-server/notify"
"plandex-server/types"
+ "runtime"
+ "runtime/debug"
shared "plandex-shared"
@@ -59,10 +61,18 @@ func (state *activeTellStreamState) loadTellPlan() error {
CancelFn: active.CancelFn,
Reason: "load tell plan",
}, func(repo *db.GitRepo) error {
- errCh := make(chan error)
+ errCh := make(chan error, 4)
// get name for plan and rename if it's a draft
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanSettings: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan settings: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
res, err := db.GetPlanSettings(plan, true)
if err != nil {
log.Printf("Error getting plan settings: %v\n", err)
@@ -117,6 +127,14 @@ func (state *activeTellStreamState) loadTellPlan() error {
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanContexts: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan modelContext: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
if iteration > 0 || missingFileResponse != "" {
modelContext = active.Contexts
} else {
@@ -139,6 +157,14 @@ func (state *activeTellStreamState) loadTellPlan() error {
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanConvo: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan convo: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
res, err := db.GetPlanConvo(currentOrgId, planId)
if err != nil {
log.Printf("Error getting plan convo: %v\n", err)
@@ -151,9 +177,17 @@ func (state *activeTellStreamState) loadTellPlan() error {
})
promptTokens := shared.GetNumTokensEstimate(req.Prompt)
- innerErrCh := make(chan error)
+ innerErrCh := make(chan error, 2)
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in storeUserMessage: %v\n%s", r, debug.Stack())
+ innerErrCh <- fmt.Errorf("error storing user message: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
if iteration == 0 && missingFileResponse == "" && !req.IsUserContinue {
num := len(convo) + 1
@@ -194,6 +228,14 @@ func (state *activeTellStreamState) loadTellPlan() error {
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanSummaries: %v\n%s", r, debug.Stack())
+ innerErrCh <- fmt.Errorf("error getting plan summaries: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
var convoMessageIds []string
for _, convoMessage := range convo {
@@ -236,6 +278,14 @@ func (state *activeTellStreamState) loadTellPlan() error {
}()
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in getPlanSubtasks: %v\n%s", r, debug.Stack())
+ errCh <- fmt.Errorf("error getting plan subtasks: %v", r)
+ runtime.Goexit() // don't allow outer function to continue and double-send to channel
+ }
+ }()
+
res, err := db.GetPlanSubtasks(auth.OrgId, planId)
if err != nil {
log.Printf("Error getting plan subtasks: %v\n", err)
diff --git a/app/server/model/plan/tell_stream_finish.go b/app/server/model/plan/tell_stream_finish.go
index 6654fa35..08e77ae7 100644
--- a/app/server/model/plan/tell_stream_finish.go
+++ b/app/server/model/plan/tell_stream_finish.go
@@ -7,6 +7,7 @@ import (
"plandex-server/db"
"plandex-server/notify"
"plandex-server/types"
+ "runtime/debug"
"time"
shared "plandex-shared"
@@ -106,6 +107,17 @@ func (state *activeTellStreamState) handleStreamFinished() handleStreamFinishedR
log.Println("summarizing convo in background")
// summarize in the background
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in summarizeConvo: %v\n%s", r, debug.Stack())
+ active.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: fmt.Sprintf("Error summarizing convo: %v", r),
+ }
+ }
+ }()
+
err := summarizeConvo(clients, settings.ModelPack.PlanSummary, summarizeConvoParams{
auth: auth,
plan: plan,
@@ -142,6 +154,13 @@ func (state *activeTellStreamState) handleStreamFinished() handleStreamFinishedR
log.Println("Sending stream message to load context files")
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic streaming auto-load context: %v\n%s", r, debug.Stack())
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("panic streaming auto-load context: %v\n%s", r, debug.Stack()))
+ }
+ }()
+
active.Stream(shared.StreamMessage{
Type: shared.StreamMessageLoadContext,
LoadContextFiles: autoLoadPaths,
diff --git a/app/server/model/plan/tell_stream_status.go b/app/server/model/plan/tell_stream_status.go
index 509e0dfd..25d4d604 100644
--- a/app/server/model/plan/tell_stream_status.go
+++ b/app/server/model/plan/tell_stream_status.go
@@ -1,9 +1,12 @@
package plan
import (
+ "fmt"
"log"
+ "net/http"
"plandex-server/db"
shared "plandex-shared"
+ "runtime/debug"
)
type handleDescAndExecStatusResult struct {
@@ -36,6 +39,17 @@ func (state *activeTellStreamState) handleDescAndExecStatus() handleDescAndExecS
var errCh = make(chan *shared.ApiError, 2)
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in genPlanDescription: %v\n%s", r, debug.Stack())
+ errCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: fmt.Sprintf("Error generating plan description: %v", r),
+ }
+ }
+ }()
+
if len(replyOperations) > 0 {
log.Println("Generating plan description")
@@ -58,6 +72,17 @@ func (state *activeTellStreamState) handleDescAndExecStatus() handleDescAndExecS
if state.currentStage.TellStage == shared.TellStageImplementation {
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in execStatusShouldContinue: %v\n%s", r, debug.Stack())
+ errCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: fmt.Sprintf("Error getting exec status: %v", r),
+ }
+ }
+ }()
+
log.Println("Getting exec status")
var err *shared.ApiError
res, err := state.execStatusShouldContinue(active.CurrentReplyContent, active.SessionId, active.Ctx)
@@ -115,6 +140,7 @@ func (state *activeTellStreamState) willContinuePlan(params willContinuePlanPara
removedSubtasks := params.removedSubtasks
allSubtasksFinished := params.allSubtasksFinished
activatePaths := params.activatePaths
+ currentSubtask := state.currentSubtask
log.Printf("[willContinuePlan] currentStage: %v", state.currentStage)
@@ -169,9 +195,9 @@ func (state *activeTellStreamState) willContinuePlan(params willContinuePlanPara
log.Printf("[willContinuePlan] Checking subtasks finished - allSubtasksFinished: %v, will continue: %v",
allSubtasksFinished, !allSubtasksFinished)
- log.Printf("[willContinuePlan] currentSubtask: %v", state.currentSubtask)
+ log.Printf("[willContinuePlan] currentSubtask: %v", currentSubtask)
- return !allSubtasksFinished && state.currentSubtask != nil
+ return !allSubtasksFinished && currentSubtask != nil
} else if state.currentStage.TellStage == shared.TellStageImplementation {
log.Println("[willContinuePlan] In implementation stage")
diff --git a/app/server/model/plan/tell_stream_usage.go b/app/server/model/plan/tell_stream_usage.go
index 9b5f4615..49c1fc7a 100644
--- a/app/server/model/plan/tell_stream_usage.go
+++ b/app/server/model/plan/tell_stream_usage.go
@@ -1,8 +1,11 @@
package plan
import (
+ "fmt"
"log"
"plandex-server/hooks"
+ "plandex-server/notify"
+ "runtime/debug"
"github.com/davecgh/go-spew/spew"
"github.com/sashabaranov/go-openai"
@@ -26,6 +29,13 @@ func (state *activeTellStreamState) handleUsageChunk(usage *openai.Usage) {
modelConfig := state.modelConfig
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in handleUsageChunk: %v\n%s", r, debug.Stack())
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("panic in handleUsageChunk: %v\n%s", r, debug.Stack()))
+ }
+ }()
+
_, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{
Auth: auth,
Plan: plan,
@@ -80,6 +90,13 @@ func (state *activeTellStreamState) execHookOnStop(sendStreamErr bool) {
modelConfig := state.modelConfig
go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in execHookOnStop: %v\n%s", r, debug.Stack())
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("panic in execHookOnStop: %v\n%s", r, debug.Stack()))
+ }
+ }()
+
_, apiErr := hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{
Auth: auth,
Plan: plan,
diff --git a/app/server/model/plan/tell_summary.go b/app/server/model/plan/tell_summary.go
index 5e425d07..eed3c3d4 100644
--- a/app/server/model/plan/tell_summary.go
+++ b/app/server/model/plan/tell_summary.go
@@ -39,10 +39,12 @@ func (state *activeTellStreamState) addConversationMessages() bool {
conversationTokens := 0
tokensUpToTimestamp := make(map[int64]int)
+ convoMessagesById := make(map[string]*db.ConvoMessage)
for _, convoMessage := range convo {
conversationTokens += convoMessage.Tokens + model.TokensPerMessage + model.TokensPerName
timestamp := convoMessage.CreatedAt.UnixNano() / int64(time.Millisecond)
tokensUpToTimestamp[timestamp] = conversationTokens
+ convoMessagesById[convoMessage.Id] = convoMessage
// log.Printf("Timestamp: %s | Tokens: %d | Total: %d | conversationTokens\n", convoMessage.Timestamp, convoMessage.Tokens, conversationTokens)
}
@@ -76,25 +78,32 @@ func (state *activeTellStreamState) addConversationMessages() bool {
log.Printf("Tokens up to timestamp: %d\n", tokens)
if !ok {
- err := fmt.Errorf("conversation summary timestamp not found in conversation")
- log.Printf("Error: %v\n", err)
+ // try a fallback by id instead of timestamp, in case timestamp rounding caused it to be missing
+ convoMessage, ok := convoMessagesById[s.LatestConvoMessageId]
- log.Println("timestamp:", timestamp)
+ if ok {
+ timestamp = convoMessage.CreatedAt.UnixNano() / int64(time.Millisecond)
+ tokens, ok = tokensUpToTimestamp[timestamp]
+ }
+
+ if !ok {
+ // instead of erroring here as we did previously, we'll just log and continue
+ // if no summary is found, we still handle it as an error below
+ // but this way we don't error out completely for a single detached summary
+
+ log.Println("conversation summary timestamp not found in conversation")
+ log.Println("timestamp:", timestamp)
- // log.Println("Conversation summary:")
- // spew.Dump(s)
+ // log.Println("Conversation summary:")
+ // spew.Dump(s)
- log.Println("tokensUpToTimestamp:")
- log.Println(spew.Sdump(tokensUpToTimestamp))
+ log.Println("tokensUpToTimestamp:")
+ log.Println(spew.Sdump(tokensUpToTimestamp))
- go notify.NotifyErr(notify.SeverityError, fmt.Errorf("conversation summary timestamp not found in conversation"))
+ go notify.NotifyErr(notify.SeverityInfo, fmt.Errorf("conversation summary timestamp not found in conversation"))
- active.StreamDoneCh <- &shared.ApiError{
- Type: shared.ApiErrorTypeOther,
- Status: http.StatusInternalServerError,
- Msg: "Conversation summary timestamp not found in conversation",
+ continue
}
- return false
}
updatedConversationTokens := (conversationTokens - tokens) + s.Tokens
diff --git a/app/server/notify/errors.go b/app/server/notify/errors.go
index dfd7fadf..c8a3b30a 100644
--- a/app/server/notify/errors.go
+++ b/app/server/notify/errors.go
@@ -1,5 +1,10 @@
package notify
+import (
+ "log"
+ "runtime/debug"
+)
+
// this allows Plandex Cloud to inject error monitoring
// all non-streaming handlers are already wrapped with different logic, so this is only needed for errors in streaming handlers
@@ -17,6 +22,12 @@ func RegisterNotifyErrFn(fn func(severity Severity, data ...interface{})) {
}
func NotifyErr(severity Severity, data ...interface{}) {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in NotifyErr: %v\n%s", r, debug.Stack())
+ }
+ }()
+
if NotifyErrFn != nil {
NotifyErrFn(severity, data...)
}
diff --git a/app/server/setup/setup.go b/app/server/setup/setup.go
index 676e314e..0f8b3054 100644
--- a/app/server/setup/setup.go
+++ b/app/server/setup/setup.go
@@ -2,6 +2,7 @@ package setup
import (
"context"
+ "fmt"
"log"
"net/http"
"os"
@@ -9,7 +10,9 @@ import (
"plandex-server/db"
"plandex-server/host"
"plandex-server/model/plan"
+ "plandex-server/notify"
"plandex-server/shutdown"
+ "runtime/debug"
"syscall"
"time"
)
@@ -126,7 +129,13 @@ func StartServer(handler http.Handler, configureFn func(handler http.Handler) ht
// Start goroutine to monitor active plans
go func() {
- defer close(plansDone)
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("panic in waitForActivePlans: %v\n%s", r, debug.Stack())
+ go notify.NotifyErr(notify.SeverityError, fmt.Errorf("panic in waitForActivePlans: %v\n%s", r, debug.Stack()))
+ }
+ close(plansDone)
+ }()
// First wait for active plans to complete or timeout
log.Println("Waiting for active plans to complete...")
diff --git a/releases/server/versions/2.1.7.md b/releases/server/versions/2.1.7.md
new file mode 100644
index 00000000..74afcc69
--- /dev/null
+++ b/releases/server/versions/2.1.7.md
@@ -0,0 +1,3 @@
+- Fix for "conversation summary timestamp not found in conversation" error (https://github.com/plandex-ai/plandex/issues/274)
+- Fix for potential panic/crash during plan stream (https://github.com/plandex-ai/plandex/issues/275)
+- Better protection against panics/crashes in server goroutines across the board
\ No newline at end of file