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
Enhance plan execution with better context management
Dane Schneider2aeb82662006
diff --git a/app/server/model/plan/tell_context.go b/app/server/model/plan/tell_context.goindex 089c02ba..41373f4b 100644--- a/app/server/model/plan/tell_context.go+++ b/app/server/model/plan/tell_context.go@@ -138,8 +138,10 @@ func (state *activeTellStreamState) formatModelContext(params formatModelContext}// Add any current pendingFiles in plan that weren't added to the context+ var currentPlanFiles *shared.CurrentPlanFilesvar pendingFiles map[string]string = map[string]string{}if state.currentPlanState != nil && state.currentPlanState.CurrentPlanFiles != nil && state.currentPlanState.CurrentPlanFiles.Files != nil {+ currentPlanFiles = state.currentPlanState.CurrentPlanFilespendingFiles = state.currentPlanState.CurrentPlanFiles.Files}@@ -270,9 +272,9 @@ func (state *activeTellStreamState) formatModelContext(params formatModelContext}}- if len(state.currentPlanState.CurrentPlanFiles.Removed) > 0 {+ if currentPlanFiles != nil && len(currentPlanFiles.Removed) > 0 {contextBodies = append(contextBodies, "*Removed files:*\n")- for path := range state.currentPlanState.CurrentPlanFiles.Removed {+ for path := range currentPlanFiles.Removed {contextBodies = append(contextBodies, fmt.Sprintf("- %s", path))}contextBodies = append(contextBodies, "These files have been *removed* and are no longer in the plan. If you want to re-add them to the plan, you must explicitly create them again.")diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex 1ea3f43a..acc753f1 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -323,7 +323,7 @@ func execTellPlan(params execTellPlanParams) {log.Printf("\n\nMessages: %d\n", len(state.messages))// for _, message := range state.messages {- // log.Printf("%s: %s\n", message.Role, message.Content)+ // log.Printf("%s: %v\n", message.Role, message.Content)// }requestTokens := model.GetMessagesTokenEstimate(state.messages...) + model.TokensPerRequest@@ -331,17 +331,28 @@ func execTellPlan(params execTellPlanParams) {stop := []string{"<PlandexFinish/>"}modelConfig := tentativeModelConfig++ log.Println("Tell plan - setting modelConfig")+ log.Println("Tell plan - requestTokens:", requestTokens)+ log.Println("Tell plan - state.currentStage.TellStage:", state.currentStage.TellStage)+ log.Println("Tell plan - state.currentStage.PlanningPhase:", state.currentStage.PlanningPhase)+if state.currentStage.TellStage == shared.TellStagePlanning {if state.currentStage.PlanningPhase == shared.PlanningPhaseContext {log.Println("Tell plan - isContextStage - setting modelConfig to context loader")modelConfig = state.settings.ModelPack.GetArchitect().GetRoleForInputTokens(requestTokens)+ log.Println("Tell plan - got modelConfig for context phase")} else if state.currentStage.PlanningPhase == shared.PlanningPhaseTasks {- modelConfig = state.settings.ModelPack.Planner.GetRoleForInputTokens(requestTokens)+ modelConfig = state.settings.ModelPack.Planner.GetRoleForInputTokens(requestTokens).ModelRoleConfig+ log.Println("Tell plan - got modelConfig for tasks phase")}} else if state.currentStage.TellStage == shared.TellStageImplementation {modelConfig = state.settings.ModelPack.GetCoder().GetRoleForInputTokens(requestTokens)+ log.Println("Tell plan - got modelConfig for implementation stage")}+ log.Println("Tell plan - modelConfig:", spew.Sdump(modelConfig))+// if the model doesn't support cache control, remove the cache control spec from the messagesif !modelConfig.BaseModelConfig.SupportsCacheControl {for i := range state.messages {diff --git a/app/server/model/plan/tell_load.go b/app/server/model/plan/tell_load.goindex 99a3f3a5..a530d7bc 100644--- a/app/server/model/plan/tell_load.go+++ b/app/server/model/plan/tell_load.go@@ -46,6 +46,8 @@ func (state *activeTellStreamState) loadTellPlan() error {var latestSummaryTokens intvar currentPlan *shared.CurrentPlanState+ log.Printf("[TellLoad] Tell plan - loadTellPlan - iteration: %d, missingFileResponse: %s, req.IsUserContinue: %t, lockScope: %s\n", iteration, missingFileResponse, req.IsUserContinue, lockScope)+db.ExecRepoOperation(db.ExecRepoOperationParams{OrgId: auth.OrgId,UserId: auth.User.Id,@@ -124,10 +126,10 @@ func (state *activeTellStreamState) loadTellPlan() error {return}- log.Printf("Tell plan - loadTellPlan - modelContext: %v\n", len(modelContext))- for _, part := range modelContext {- log.Printf("Tell plan - loadTellPlan - part: %s - %s - %s - %d tokens\n", part.ContextType, part.Name, part.FilePath, part.NumTokens)- }+ log.Printf("[TellLoad] Tell plan - loadTellPlan - modelContext: %v\n", len(modelContext))+ // for _, part := range modelContext {+ // log.Printf("[TellLoad] Tell plan - loadTellPlan - part: %s - %s - %s - %d tokens\n", part.ContextType, part.Name, part.FilePath, part.NumTokens)+ // }modelContext = res}@@ -154,7 +156,7 @@ func (state *activeTellStreamState) loadTellPlan() error {if iteration == 0 && missingFileResponse == "" && !req.IsUserContinue {num := len(convo) + 1- log.Printf("storing user message | len(convo): %d | num: %d\n", len(convo), num)+ log.Printf("[TellLoad] storing user message | len(convo): %d | num: %d\n", len(convo), num)promptMsg = &db.ConvoMessage{OrgId: currentOrgId,@@ -171,10 +173,13 @@ func (state *activeTellStreamState) loadTellPlan() error {},}+ log.Println("[TellLoad] storing user message")+ // repo.LogGitRepoState()+_, err = db.StoreConvoMessage(repo, promptMsg, auth.User.Id, branch, true)if err != nil {- log.Printf("Error storing user message: %v\n", err)+ log.Printf("[TellLoad] Error storing user message: %v\n", err)innerErrCh <- fmt.Errorf("error storing user message: %v", err)return}diff --git a/app/server/model/plan/tell_stream_error.go b/app/server/model/plan/tell_stream_error.goindex d8416d66..cf5387ea 100644--- a/app/server/model/plan/tell_stream_error.go+++ b/app/server/model/plan/tell_stream_error.go@@ -6,6 +6,7 @@ import ("log""net/http""plandex-server/db"+ "plandex-server/shutdown""strconv""time"@@ -79,7 +80,7 @@ func (state *activeTellStreamState) onError(params onErrorParams) onErrorResult}storeDescAndReply := func() error {- ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)+ ctx, cancelFn := context.WithTimeout(shutdown.ShutdownCtx, 5*time.Second)err := db.ExecRepoOperation(db.ExecRepoOperationParams{OrgId: currentOrgId,diff --git a/app/server/model/plan/tell_stream_main.go b/app/server/model/plan/tell_stream_main.goindex 32af1919..5ce2e4d8 100644--- a/app/server/model/plan/tell_stream_main.go+++ b/app/server/model/plan/tell_stream_main.go@@ -7,6 +7,8 @@ import ("time"shared "plandex-shared"++ "github.com/davecgh/go-spew/spew")func (state *activeTellStreamState) listenStream(stream *model.ExtendedChatCompletionStream) {@@ -112,34 +114,40 @@ mainLoop:return}- res := state.onError(onErrorParams{- streamErr: fmt.Errorf("stream finished with no choices | The model failed to generate a valid response."),- storeDesc: true,- canRetry: true,- })- if res.shouldReturn {- return- }- if res.shouldContinueMainLoop {- // continue instead of returning so that context cancellation is handled- continue mainLoop- }+ log.Println("listenStream - stream finished with no choices", spew.Sdump(response))++ // Previously we'd return an error if there were no choices, but some models do this and then keep streaming, so we'll just log it and continue, waiting for an EOF if there's a problem+ // res := state.onError(onErrorParams{+ // streamErr: fmt.Errorf("stream finished with no choices | The model failed to generate a valid response."),+ // storeDesc: true,+ // canRetry: true,+ // })+ // if res.shouldReturn {+ // return+ // }+ // if res.shouldContinueMainLoop {+ // // continue instead of returning so that context cancellation is handled+ // continue mainLoop+ // }++ continue mainLoop}- if len(response.Choices) > 1 {- res := state.onError(onErrorParams{- streamErr: fmt.Errorf("stream finished with more than one choice | The model failed to generate a valid response."),- storeDesc: true,- canRetry: true,- })- if res.shouldReturn {- return- }- if res.shouldContinueMainLoop {- // continue instead of returning so that context cancellation is handled- continue mainLoop- }- }+ // We'll be more accepting of multiple choices and just take the first one+ // if len(response.Choices) > 1 {+ // res := state.onError(onErrorParams{+ // streamErr: fmt.Errorf("stream finished with more than one choice | The model failed to generate a valid response."),+ // storeDesc: true,+ // canRetry: true,+ // })+ // if res.shouldReturn {+ // return+ // }+ // if res.shouldContinueMainLoop {+ // // continue instead of returning so that context cancellation is handled+ // continue mainLoop+ // }+ // }choice := response.Choices[0]diff --git a/app/server/model/plan/tell_stream_store.go b/app/server/model/plan/tell_stream_store.goindex 7ed087a0..46911246 100644--- a/app/server/model/plan/tell_stream_store.go+++ b/app/server/model/plan/tell_stream_store.go@@ -227,18 +227,6 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParams}}- log.Println("[storeOnFinished] Locked repo for assistant reply and description")-- if err != nil {- return storeOnFinishedResult{- handleStreamFinishedResult: handleStreamFinishedResult{- shouldContinueMainLoop: true,- shouldReturn: false,- },- allSubtasksFinished: allSubtasksFinished,- }- }-return storeOnFinishedResult{handleStreamFinishedResult: handleStreamFinishedResult{},allSubtasksFinished: allSubtasksFinished,