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
a lot of work on execution, basis for file operations
Dane Schneidereacef5ce07fa
diff --git a/app/cli/fs/fs.go b/app/cli/fs/fs.goindex a723b515..46ef38b1 100644--- a/app/cli/fs/fs.go+++ b/app/cli/fs/fs.go@@ -122,14 +122,7 @@ func IsGitRepo(dir string) bool {return isGitRepo}-type ProjectPaths struct {- ActivePaths map[string]bool- AllPaths map[string]bool- PlandexIgnored *ignore.GitIgnore- IgnoredPaths map[string]string-}--func GetProjectPaths(baseDir string) (*ProjectPaths, error) {+func GetProjectPaths(baseDir string) (*types.ProjectPaths, error) {if ProjectRoot == "" {return nil, fmt.Errorf("no project root found")}@@ -137,7 +130,7 @@ func GetProjectPaths(baseDir string) (*ProjectPaths, error) {return GetPaths(baseDir, ProjectRoot)}-func GetPaths(baseDir, currentDir string) (*ProjectPaths, error) {+func GetPaths(baseDir, currentDir string) (*types.ProjectPaths, error) {ignored, err := GetPlandexIgnore(currentDir)if err != nil {@@ -376,7 +369,7 @@ func GetPaths(baseDir, currentDir string) (*ProjectPaths, error) {}}- return &ProjectPaths{+ return &types.ProjectPaths{ActivePaths: activePaths,AllPaths: allPaths,PlandexIgnored: ignored,diff --git a/app/cli/lib/apply.go b/app/cli/lib/apply.goindex 31912ad2..5a95c6ea 100644--- a/app/cli/lib/apply.go+++ b/app/cli/lib/apply.go@@ -12,7 +12,9 @@ import ("plandex/auth""plandex/fs""plandex/term"+ "plandex/types""strings"+ "sync""syscall""github.com/fatih/color"@@ -28,13 +30,11 @@ type ApplyFlags struct {AutoDebug int}-type onErrFn func(errMsg string, errArgs ...interface{})-func MustApplyPlan(planId,branch string,flags ApplyFlags,- onExecFail func(status int, output string, attempt int),+ onExecFail types.OnApplyExecFailFn,) {MustApplyPlanAttempt(planId, branch, flags, onExecFail, 0)}@@ -43,7 +43,7 @@ func MustApplyPlanAttempt(planId,branch string,flags ApplyFlags,- onExecFail func(status int, output string, attempt int),+ onExecFail types.OnApplyExecFailFn,attempt int,) {log.Println("Applying plan")@@ -122,7 +122,7 @@ func MustApplyPlanAttempt(log.Printf("Files to apply: %d, Has exec script: %v", len(toApply), hasExec)- if len(toApply) == 0 {+ if len(toApply) == 0 && !hasExec {term.StopSpinner()fmt.Println("🤷♂️ No changes to apply")return@@ -130,8 +130,14 @@ func MustApplyPlanAttempt(hasFileChanges := !hasExec || len(toApply) > 1+ var toRollback *types.ApplyRollbackPlan+ var updatedFiles []string+onErr := func(errMsg string, errArgs ...interface{}) {term.StopSpinner()+ if toRollback != nil && toRollback.HasChanges() {+ toRollback.Rollback(true)+ }term.OutputErrorAndExit(errMsg, errArgs...)}@@ -140,13 +146,6 @@ func MustApplyPlanAttempt(term.OutputSimpleError(errMsg, unformattedErrMsg)}- log.Println("Getting API keys")-- var apiKeys map[string]string- if !auth.Current.IntegratedModelsMode {- apiKeys = MustVerifyApiKeysSilent()- }-log.Println("Has file changes:", hasFileChanges)if hasFileChanges {@@ -171,151 +170,76 @@ func MustApplyPlanAttempt(term.ResumeSpinner()}- var commitSummary string+ log.Println("Applying plan files")- openAIBase := os.Getenv("OPENAI_API_BASE")- if openAIBase == "" {- openAIBase = os.Getenv("OPENAI_ENDPOINT")+ if hasExec {+ term.StopSpinner()+ fmt.Println("🔄 Tentatively applying changes")+ term.ResumeSpinner()}- log.Println("Applying plan with API call")+ updatedFiles, toRollback, err = applyFiles(toApply)- commitSummary, apiErr = api.Client.ApplyPlan(planId, branch, shared.ApplyPlanRequest{- ApiKeys: apiKeys,- OpenAIBase: openAIBase,- OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),- })-- if apiErr != nil {- onErr("failed to set pending results applied: %s", apiErr.Msg)- return+ if err != nil {+ onErr("failed to apply files: %s", err)}- log.Println("Applying plan files")-- var updatedFiles []string- for path, content := range toApply {- if path == "_apply.sh" {- continue- }-- // Compute destination path- dstPath := filepath.Join(fs.ProjectRoot, path)-- content = strings.ReplaceAll(content, "\\`\\`\\`", "```")-- // Check if the file exists- var exists bool- _, err := os.Stat(dstPath)- if err == nil {- exists = true- } else {- if os.IsNotExist(err) {- exists = false- } else {- onErr("failed to check if %s exists:", dstPath)- return- }- }-- if exists {- // read file content- bytes, err := os.ReadFile(dstPath)-- if err != nil {- onErr("failed to read %s:", dstPath)- return- }-- // Check if the file has changed- if string(bytes) == content {- // log.Println("File is unchanged, skipping")- continue- } else {- updatedFiles = append(updatedFiles, path)- }- } else {- updatedFiles = append(updatedFiles, path)+ log.Println("Applying plan files complete")+ }- // Create the directory if it doesn't exist- err := os.MkdirAll(filepath.Dir(dstPath), 0755)- if err != nil {- onErr("failed to create directory %s:", filepath.Dir(dstPath))- return- }- }+ onExecSuccess := func() {+ commitSummary, err := apiApplyPlan(planId, branch)- // Write the file- err = os.WriteFile(dstPath, []byte(content), 0644)- if err != nil {- onErr("failed to write %s:", dstPath)- return- }+ if err != nil {+ onErr("apply plan server error: %s", err)}- term.StopSpinner()-- log.Println("Applying plan files complete")-if len(updatedFiles) == 0 {+ term.StopSpinner()fmt.Println("✅ Applied changes, but no files were updated")- return} else {- if isRepo && !noCommit {- confirmed := autoCommit- if !autoCommit {- fmt.Println("✏️ Plandex can commit these updates with an automatically generated message.")- fmt.Println()- // fmt.Println("ℹ️ Only the files that Plandex is updating will be included the commit. Any other changes, staged or unstaged, will remain exactly as they are.")- // fmt.Println()-- confirmed, err = term.ConfirmYesNo("Commit Plandex updates now?")-- if err != nil {- onErr("failed to get confirmation user input: %s", err)- }- }-- if confirmed {- // Commit the changes- msg := currentPlanState.PendingChangesSummaryForApply(commitSummary)-- // log.Println("Committing changes with message:")- // log.Println(msg)-- // spew.Dump(currentPlanState)-- err := GitAddAndCommitPaths(fs.ProjectRoot, msg, updatedFiles, true)- if err != nil {- onGitErr("Failed to commit changes:", err.Error())- }+ appliedMsgFn := func() {+ suffix := ""+ if len(updatedFiles) > 1 {+ suffix = "s"}+ fmt.Printf("✅ Applied changes, %d file%s updated\n", len(updatedFiles), suffix)}- suffix := ""- if len(updatedFiles) > 1 {- suffix = "s"+ if isRepo && !noCommit {+ gitErr := commitApplied(autoCommit, commitSummary, updatedFiles, currentPlanState)+ term.StopSpinner()+ appliedMsgFn()+ if gitErr != nil {+ onGitErr("Failed to commit changes:", gitErr.Error())+ }+ } else {+ term.StopSpinner()+ appliedMsgFn()}- fmt.Printf("✅ Applied changes, %d file%s updated\n", len(updatedFiles), suffix)}}- term.StopSpinner()-if _, ok := toApply["_apply.sh"]; ok && !noExec {- handleApplyScript(flags, toApply, onErr, onExecFail, attempt)+ handleApplyScript(flags, toApply, onErr, toRollback, onExecFail, attempt, onExecSuccess)+ } else {+ onExecSuccess()}}func handleApplyScript(flags ApplyFlags,toApply map[string]string,- onErr onErrFn,- onExecFail func(status int, output string, attempt int),+ onErr types.OnErrFn,+ toRollback *types.ApplyRollbackPlan,+ onExecFail types.OnApplyExecFailFn,attempt int,+ onSuccess func(),) {log.Println("Handling apply script")+ term.StopSpinner()+color.New(term.ColorHiCyan, color.Bold).Println("🚀 Commands to execute 👇")content := toApply["_apply.sh"]@@ -343,7 +267,21 @@ func handleApplyScript(if confirmed {log.Println("Executing apply script")- execApplyScript(toApply, onErr, onExecFail, attempt)+ execApplyScript(flags, toApply, onErr, toRollback, onExecFail, attempt, onSuccess)+ } else {+ if toRollback != nil && toRollback.HasChanges() {+ res, err := term.SelectFromList("Skipping execution. Still apply other changes or roll back all changes?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})++ if err != nil {+ onErr("failed to get rollback confirmation user input: %s", err)+ }++ if res == string(types.ApplyRollbackOptionRollback) {+ toRollback.Rollback(true)+ } else {+ onSuccess()+ }+ }}}@@ -364,10 +302,13 @@ trap 'echo "Error on line $LINENO: ${funcfiletrace[0]#*:}"' ERR}func execApplyScript(+ flags ApplyFlags,toApply map[string]string,- onErr onErrFn,- onExecFail func(status int, output string, attempt int),+ onErr types.OnErrFn,+ toRollback *types.ApplyRollbackPlan,+ onExecFail types.OnApplyExecFailFn,attempt int,+ onSuccess func(),) {log.Println("Executing apply script")@@ -444,6 +385,21 @@ func execApplyScript(go func() {sig := <-sigChanos.Remove(dstPath)++ if toRollback != nil && toRollback.HasChanges() {+ color.New(term.ColorHiRed, color.Bold).Println("🚨 Execution interrupted")+ res, err := term.SelectFromList("Still apply other changes or roll back all changes?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})+ if err != nil {+ onErr("failed to get rollback confirmation user input: %s", err)+ }++ if res == string(types.ApplyRollbackOptionRollback) {+ toRollback.Rollback(true)+ } else {+ onSuccess()+ }+ }+execCmd.Process.Signal(sig)}()defer signal.Stop(sigChan)@@ -476,10 +432,167 @@ func execApplyScript(if ok {status = exitErr.ExitCode()}- onExecFail(status, outputBuilder.String(), attempt)+ onExecFail(status, outputBuilder.String(), attempt, toRollback, onErr, onSuccess)} else {fmt.Println()fmt.Println("✅ Commands succeeded")+ onSuccess()+ }++}++func apiApplyPlan(planId, branch string) (string, error) {+ log.Println("Getting API keys")++ var apiKeys map[string]string+ if !auth.Current.IntegratedModelsMode {+ apiKeys = MustVerifyApiKeysSilent()+ }++ var commitSummary string++ openAIBase := os.Getenv("OPENAI_API_BASE")+ if openAIBase == "" {+ openAIBase = os.Getenv("OPENAI_ENDPOINT")+ }++ log.Println("Applying plan with API call")++ commitSummary, apiErr := api.Client.ApplyPlan(planId, branch, shared.ApplyPlanRequest{+ ApiKeys: apiKeys,+ OpenAIBase: openAIBase,+ OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),+ })++ if apiErr != nil {+ return "", fmt.Errorf("failed to set pending results applied: %s", apiErr.Msg)+ }++ return commitSummary, nil+}++func commitApplied(autoCommit bool, commitSummary string, updatedFiles []string, currentPlanState *shared.CurrentPlanState) (err error) {+ confirmed := autoCommit+ if !autoCommit {+ fmt.Println("✏️ Plandex can commit these updates with an automatically generated message.")+ fmt.Println()+ // fmt.Println("ℹ️ Only the files that Plandex is updating will be included the commit. Any other changes, staged or unstaged, will remain exactly as they are.")+ // fmt.Println()++ confirmed, err = term.ConfirmYesNo("Commit Plandex updates now?")++ if err != nil {+ return fmt.Errorf("failed to get confirmation user input: %s", err)+ }+ }++ if confirmed {+ // Commit the changes+ msg := currentPlanState.PendingChangesSummaryForApply(commitSummary)++ // log.Println("Committing changes with message:")+ // log.Println(msg)++ // spew.Dump(currentPlanState)++ err = GitAddAndCommitPaths(fs.ProjectRoot, msg, updatedFiles, true)+ if err != nil {+ return fmt.Errorf("failed to commit changes: %s", err.Error())+ }+ }++ return nil+}++func applyFiles(toApply map[string]string) ([]string, *types.ApplyRollbackPlan, error) {+ var updatedFiles []string+ var toRevert map[string]types.ApplyReversion+ var toRemove []string++ var mu sync.Mutex+ errCh := make(chan error, len(toApply))++ for path, content := range toApply {+ if path == "_apply.sh" {+ errCh <- nil+ continue+ }++ go func(path, content string) {+ // Compute destination path+ dstPath := filepath.Join(fs.ProjectRoot, path)+ content = strings.ReplaceAll(content, "\\`\\`\\`", "```")++ // Check if the file exists+ var exists bool+ var mode os.FileMode+ info, err := os.Stat(dstPath)+ if err == nil {+ exists = true+ mode = info.Mode()+ } else {+ if os.IsNotExist(err) {+ exists = false+ } else {+ errCh <- fmt.Errorf("failed to check if %s exists: %s", dstPath, err.Error())+ return+ }+ }++ if exists {+ // read file content+ bytes, err := os.ReadFile(dstPath)++ if err != nil {+ errCh <- fmt.Errorf("failed to read %s: %s", dstPath, err.Error())+ return+ }++ // Check if the file has changed+ if string(bytes) == content {+ // log.Println("File is unchanged, skipping")+ errCh <- nil+ return+ } else {+ mu.Lock()+ updatedFiles = append(updatedFiles, path)+ toRevert[dstPath] = types.ApplyReversion{Content: string(bytes), Mode: mode}+ mu.Unlock()+ }+ } else {+ mu.Lock()+ updatedFiles = append(updatedFiles, path)+ toRemove = append(toRemove, dstPath)+ mu.Unlock()++ // Create the directory if it doesn't exist+ err := os.MkdirAll(filepath.Dir(dstPath), 0755)+ if err != nil {+ errCh <- fmt.Errorf("failed to create directory %s: %s", filepath.Dir(dstPath), err.Error())+ return+ }+ }++ // Write the file+ err = os.WriteFile(dstPath, []byte(content), 0644)+ if err != nil {+ errCh <- fmt.Errorf("failed to write %s: %s", dstPath, err.Error())+ return+ }++ errCh <- nil+ }(path, content)+ }++ for range toApply {+ err := <-errCh+ if err != nil {+ return nil, nil, err+ }}+ return updatedFiles, &types.ApplyRollbackPlan{+ ToRevert: toRevert,+ ToRemove: toRemove,+ }, nil}diff --git a/app/cli/lib/context_update.go b/app/cli/lib/context_update.goindex 9dd44b13..a94ddf88 100644--- a/app/cli/lib/context_update.go+++ b/app/cli/lib/context_update.go@@ -223,7 +223,7 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool, maybeContexts []*shared.CcontextsById := map[string]*shared.Context{}deleteIds := map[string]bool{}- var paths *fs.ProjectPaths+ var paths *types.ProjectPathsvar hasDirectoryTreeWithIgnoredPaths boolfor _, context := range contexts {diff --git a/app/cli/plan_exec/exec.go b/app/cli/plan_exec/apply_exec.gosimilarity index 60%rename from app/cli/plan_exec/exec.gorename to app/cli/plan_exec/apply_exec.goindex 061d0dc6..21efaa97 100644--- a/app/cli/plan_exec/exec.go+++ b/app/cli/plan_exec/apply_exec.go@@ -6,19 +6,24 @@ import ("plandex/auth""plandex/lib""plandex/term"+ "plandex/types""github.com/fatih/color""github.com/plandex/plandex/shared")-func GetOnApplyExecFail(flags lib.ApplyFlags) func(status int, output string, attempt int) {- var onExecFail func(status int, output string, attempt int)- onExecFail = func(status int, output string, attempt int) {+func GetOnApplyExecFail(flags lib.ApplyFlags) types.OnApplyExecFailFn {+ var onExecFail types.OnApplyExecFailFn+ onExecFail = func(status int, output string, attempt int, toRollback *types.ApplyRollbackPlan, onErr types.OnErrFn, onSuccess func()) {var proceed boolif flags.AutoDebug > 0 {if attempt >= flags.AutoDebug {- color.New(term.ColorHiRed, color.Bold).Printf("Commands failed %d times.\n", attempt)+ timesLbl := "times"+ if attempt == 1 {+ timesLbl = "time"+ }+ color.New(term.ColorHiRed, color.Bold).Printf("Commands failed %d %s.\n", attempt, timesLbl)} else {proceed = true}@@ -35,6 +40,10 @@ func GetOnApplyExecFail(flags lib.ApplyFlags) func(status int, output string, at}if proceed {+ if toRollback != nil && toRollback.HasChanges() {+ toRollback.Rollback(true)+ }+var apiKeys map[string]stringif !auth.Current.IntegratedModelsMode {apiKeys = lib.MustVerifyApiKeysSilent()@@ -54,6 +63,18 @@ func GetOnApplyExecFail(flags lib.ApplyFlags) func(status int, output string, atlog.Printf("Applying plan after tell")lib.MustApplyPlanAttempt(lib.CurrentPlanId, lib.CurrentBranch, flags, onExecFail, attempt+1)+ } else {+ res, err := term.SelectFromList("Still apply other changes or roll back all changes?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})++ if err != nil {+ onErr("failed to get rollback confirmation user input: %s", err)+ }++ if res == string(types.ApplyRollbackOptionRollback) {+ toRollback.Rollback(true)+ } else {+ onSuccess()+ }}}diff --git a/app/cli/types/apply.go b/app/cli/types/apply.gonew file mode 100644index 00000000..8d1a783d--- /dev/null+++ b/app/cli/types/apply.go@@ -0,0 +1,75 @@+package types++import (+ "fmt"+ "os"+)++type ApplyRollbackOption string++const (+ ApplyRollbackOptionKeep ApplyRollbackOption = "Still apply other changes"+ ApplyRollbackOptionRollback ApplyRollbackOption = "Roll back all changes"+)++type OnApplyExecFailFn func(status int, output string, attempt int, toRollback *ApplyRollbackPlan, onErr OnErrFn, onSuccess func())++type ApplyReversion struct {+ Content string+ Mode os.FileMode+}++type ApplyRollbackPlan struct {+ ToRevert map[string]ApplyReversion+ ToRemove []string+ PreviousProjectPaths *ProjectPaths+}++func (r *ApplyRollbackPlan) HasChanges() bool {+ return len(r.ToRevert) > 0 || len(r.ToRemove) > 0+}++func (r *ApplyRollbackPlan) Rollback(msg bool) error {+ errCh := make(chan error, len(r.ToRevert)+len(r.ToRemove))++ for path, revert := range r.ToRevert {+ go func(path string, revert ApplyReversion) {+ err := os.WriteFile(path, []byte(revert.Content), revert.Mode)+ if err != nil {+ errCh <- fmt.Errorf("failed to write %s: %s", path, err.Error())+ return+ }+ errCh <- nil+ }(path, revert)+ }++ for _, path := range r.ToRemove {+ go func(path string) {+ err := os.RemoveAll(path)+ if err != nil {+ errCh <- fmt.Errorf("failed to remove %s: %s", path, err.Error())+ return+ }+ errCh <- nil+ }(path)+ }++ errs := []error{}++ for i := 0; i < len(r.ToRevert)+len(r.ToRemove); i++ {+ err := <-errCh+ if err != nil {+ errs = append(errs, err)+ }+ }++ if len(errs) > 0 {+ return fmt.Errorf("failed to rollback: %s", errs)+ }++ if msg {+ fmt.Println("🚫 Rolled back all changes")+ }++ return nil+}diff --git a/app/cli/types/fs.go b/app/cli/types/fs.gonew file mode 100644index 00000000..e7fc8e08--- /dev/null+++ b/app/cli/types/fs.go@@ -0,0 +1,10 @@+package types++import ignore "github.com/sabhiram/go-gitignore"++type ProjectPaths struct {+ ActivePaths map[string]bool+ AllPaths map[string]bool+ PlandexIgnored *ignore.GitIgnore+ IgnoredPaths map[string]string+}diff --git a/app/cli/types/types.go b/app/cli/types/types.goindex 700811dc..c8f776ca 100644--- a/app/cli/types/types.go+++ b/app/cli/types/types.go@@ -60,3 +60,5 @@ type ChangesUIScrollReplacement struct {type ChangesUIViewportsUpdate struct {ScrollReplacement *ChangesUIScrollReplacement}++type OnErrFn func(errMsg string, errArgs ...interface{})diff --git a/app/server/db/result_helpers.go b/app/server/db/result_helpers.goindex e866277a..8d4cffb0 100644--- a/app/server/db/result_helpers.go+++ b/app/server/db/result_helpers.go@@ -376,14 +376,7 @@ func ApplyPlan(ctx context.Context, orgId, userId, branchName string, plan *PlanerrCh <- fmt.Errorf("error getting plan file results: %v", err)return}- filtered := []*PlanFileResult{}- for _, result := range res {- if result.Path != "_apply.sh" {- filtered = append(filtered, result)- }- }-- results = filtered+ results = reserrCh <- nil}()@@ -435,6 +428,10 @@ func ApplyPlan(ctx context.Context, orgId, userId, branchName string, plan *PlanpendingNewFilesSet := make(map[string]bool)pendingUpdatedFilesSet := make(map[string]bool)for _, result := range pendingDbResults {+ if result.Path == "_apply.sh" {+ continue+ }+if len(result.Replacements) == 0 && result.Content != "" {pendingNewFilesSet[result.Path] = true} else if !pendingNewFilesSet[result.Path] {diff --git a/app/server/model/plan/tell_context.go b/app/server/model/plan/tell_context.goindex 410afc6c..b5e7c586 100644--- a/app/server/model/plan/tell_context.go+++ b/app/server/model/plan/tell_context.go@@ -7,7 +7,7 @@ import ("github.com/plandex/plandex/shared")-func (state *activeTellStreamState) formatModelContext(includeMaps, includeTrees, isImplementationStage bool) (string, int, error) {+func (state *activeTellStreamState) formatModelContext(includeMaps, includeTrees, isImplementationStage, execEnabled bool) (string, int, error) {var contextMessages []string = []string{"### LATEST PLAN CONTEXT ###",}@@ -92,9 +92,25 @@ func (state *activeTellStreamState) formatModelContext(includeMaps, includeTreescontinue}+ if filePath == "_apply.sh" {+ continue+ }+contextMessages = append(contextMessages, fmt.Sprintf("\n\n- %s:\n\n```\n%s\n```", filePath, body))}}+ if execEnabled {+ contextMessages = append(contextMessages, state.currentPlanState.ExecHistory())++ scriptContent, ok := state.currentPlanState.CurrentPlanFiles.Files["_apply.sh"]+ if !ok || scriptContent == "" {+ scriptContent = "[empty]"+ }++ contextMessages = append(contextMessages, "*Current* state of _apply.sh script:")+ contextMessages = append(contextMessages, fmt.Sprintf("\n\n- _apply.sh:\n\n```\n%s\n```", scriptContent))+ }+return strings.Join(contextMessages, "\n### END OF CONTEXT ###\n"), numTokens, nil}diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex b7a44ffd..9d0abd82 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -174,7 +174,7 @@ func execTellPlan(includeTrees = false}- modelContextText, modelContextTokens, err := state.formatModelContext(includeMaps, includeTrees, isImplementationStage)+ modelContextText, modelContextTokens, err := state.formatModelContext(includeMaps, includeTrees, isImplementationStage, req.ExecEnabled)if err != nil {err = fmt.Errorf("error formatting model modelContext: %v", err)log.Println(err)diff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.goindex 19f2494a..61822f0e 100644--- a/app/server/model/plan/tell_stream.go+++ b/app/server/model/plan/tell_stream.go@@ -361,11 +361,17 @@ mainLoop:log.Println("Description stored")if hasNewSubtasks || subtaskFinished {- if subtaskFinished {+ if subtaskFinished && state.currentSubtask != nil {log.Println("Subtask finished")+ log.Println("Current subtask:")+ log.Println(state.currentSubtask.Title)state.currentSubtask.IsFinished = true++ log.Println("Updated state. Current subtask:")+ log.Println(state.currentSubtask)}+ log.Println("Storing plan subtasks")err = db.StorePlanSubtasks(currentOrgId, planId, state.subtasks)if err != nil {log.Printf("Error storing plan subtasks: %v\n", err)@@ -383,18 +389,24 @@ mainLoop:}}+ log.Println("Set new current subtask. Current subtask:")+ log.Println(state.currentSubtask)+ log.Println("All subtasks finished:", allSubtasksFinished)++ log.Println("Update state of subtasks")+ spew.Dump(state.subtasks)}// spew.Dump(description)- log.Println("Comitting reply message and description")+ log.Println("Comitting reply message, description, and subtasks")err = db.GitAddAndCommit(currentOrgId, planId, branch, convoCommitMsg)if err != nil {state.onError(fmt.Errorf("failed to commit: %v", err), false, assistantMsg.Id, convoCommitMsg)return err}- log.Println("Assistant reply and description committed")+ log.Println("Assistant reply, description, and subtasks committed")return nil}()diff --git a/app/server/model/plan/tell_subtasks.go b/app/server/model/plan/tell_subtasks.goindex ee369438..71452011 100644--- a/app/server/model/plan/tell_subtasks.go+++ b/app/server/model/plan/tell_subtasks.go@@ -31,9 +31,10 @@ func (state *activeTellStreamState) formatSubtasks() (string, int, error) {} else {subtasksText += "no"}+ subtasksText += "\n"if state.currentSubtask != nil && subtask.Title == state.currentSubtask.Title {- subtasksText += "\n\n**CURRENT SUBTASK**\n\n"+ subtasksText += "Current subtask: yes"}subtasksText += "\n"diff --git a/app/server/model/prompts/apply_exec.go b/app/server/model/prompts/apply_exec.goindex aa5af407..d9ea401f 100644--- a/app/server/model/prompts/apply_exec.go+++ b/app/server/model/prompts/apply_exec.go@@ -24,70 +24,6 @@ DO NOT update the _apply.sh script unless it is necessary to fix the problem. Ifvar ApplyDebugPromptTokens int-const PendingScriptPrompt = `- ## _pending.sh file-- You can write to a *special path*: ` + "_pending.sh" + `-- This file allows you to execute commands in the context of the *files in context* and the *pending files* (the files that will be created or updated by the plan). This script will be executed on the *Plandex server*, not on the user's machine. You can ONLY use this file to manipulate files that are in context or *pending*. You cannot use this file to execute commands outside the context of the plan or files with pending updates.-- The ` + "_pending.sh" + ` can take the following actions and use the following special commands:-- - Move or rename a file, directory, or pattern of files that are in context or pending with the special` + "`move`" + ` command (it works just like the 'mv' command and takes arguments in the same way):- - move 'components/page.tsx' 'pages/page.tsx'- - move 'pages/' 'components/'- - move 'components/*.page.ts' 'pages/'- - Copy a file, directory, or pattern of files that are in context or pending with the special ` + "`copy`" + ` command (it works just like the 'cp' command and takes arguments in the same way):- - copy 'components/page.tsx' 'pages/page.tsx'- - copy 'pages/' 'components/'- - copy 'components/*.page.ts' 'pages/'- - Clear any pending changes to files, directories, or patterns of files that are in context or pending with the special` + "`reject`" + ` command (it works just like the 'git reset --hard' command and takes arguments in the same way):- - reject 'pages/page.tsx'- - reject 'components/'- - reject 'components/*.page.ts'- - Remove a file, directory, or pattern of files that are in context or pending with the ` + "`remove`" + ` command (it works just like the 'rm -rf' command and takes arguments in the same way):- - remove 'pages/'- - remove 'components/page.tsx'- - remove 'components/*.page.ts'-- You CANNOT AND MUST NOT use *any other commands* in the _pending.sh script. Only the commands listed above are allowed. All the above commands (move, reject, remove) will be available to you when the script is executed. Again, NO OTHER commands are allowed or available to you—**this is absolutely critical.**-- You MUST NOT create directories in the _pending.sh script. They will be created as needed by the special commands.-- You MUST NOT include comments in the _pending.sh script. There MUST NOT be *anything* at all apart from the special commands and their arguments.-- Do NOT use 'mv', 'cp', or 'rm' commands in the _pending.sh script. Use the special commands (` + "`move`" + `, ` + "`copy`" + `, ` + "`reject`" + `, ` + "`remove`" + `) instead.-- The _pending.sh script is executed at the *root directory* that contains all context files and pending files. You can only reference files and directories that are listed in context or in the pending files.-- Each _pending.sh script file block is *independent*. It will be executed independently of any others. You can output multiple _pending.sh scripts in the same response if needed. _pending.sh scripts are *not* persisted. Each one is executed once at the end of the response and then discarded. You must treat each _pending.sh block as if you are *creating a new file* which is independent of any other files.-- Do NOT include the` + "`#!/bin/bash`" + ` line at the top of a _pending.sh script. Every _pending.sh script will be executed with ` + "`#!/bin/bash`" + ` already included.-- You also must not include error handling or logging in a _pending.sh script. This will be handled outside the script.-- Wrap paths in single quotes when using the move, reject, or remove commands.-- You do not need to give _pending.sh scripts execution privileges or any other permissions. This is handled outside the script.-- Example:-- - _pending.sh:- ` + "```bash" + `- move 'components/page.tsx' 'pages/page.tsx'- ` + "```" + `-- You ABSOLUTEY MUST use the _pending.sh script when moving, renaming, copying, rejecting, or removing files or directories that are in context or pending. Do NOT UNDER ANY CIRCUMSTANCES use a file block to do any of these actions; ALWAYS use the _pending.sh script instead.-- If the user asks you to move or change the path of a file that is in context or pending, you MUST use the _pending.sh script with a 'move' command to do this. Do NOT use a file block to do this.-- If the user asks you to copy a file that is in context or pending, you MUST use the _pending.sh script with a 'copy' command to do this. Do NOT use a file block to do this.-- If the user asks you to revert all the changes you've made to a file that is in context or pending, you MUST use the _pending.sh script with a 'reject' command to do this. Do NOT use a file block to do this.-- If the user asks you to remove a file that is in context or pending, you MUST use the _pending.sh script with a 'remove' command to do this. Do NOT use a file block to do this.-`-const ApplyScriptPrompt = `## _apply.sh file and command execution@@ -99,10 +35,12 @@ This file allows you to execute commands in the context of the *user's machine*,Use this to run any necessary commands *after* all the pending files from the plan have been created or updated on the user's machine.-DO NOT use the _apply.sh script to move, copy, reject, or remove files that are in the context of the plan or the pending files—use the _pending.sh script for those actions.+DO NOT use the _apply.sh script to move, remove, or reset changes to files that are in context or have pending changes—use one of the special file operation sections instead: '### Move Files', '### Remove Files', or '### Reset Changes' if you need to do that, and follow the instructions for those sections.DO NOT use the _apply.sh script to create directory paths for files that are in context or pending. Any required directories will be created automatically when the plan is applied.+You can use the _apply.sh script for file operations on files that are *not* in context or pending, but be careful and conservative—only do so when it is strictly necessary to implement the plan. Do NOT use it for file operations on files that are in context or pending, and do NOT use it to create directories or files—any required directories will be created automatically when the plan is applied, and files must be created by code blocks within the plan, not in _apply.sh.+Use the appropriate commands for the user's operating system and shell, which will be supplied to you in the prompt.When using third party tools, do not assume the user has them installed. The _apply.sh script should always first check if the tool is installed. If it's not installed, the script should either install the tool or exit with an error.@@ -129,6 +67,8 @@ DO NOT give the user any additional commands to run—include them all in the _aIf appropriate, also include a command to run the actual program in _apply.sh. For example, if there is a Makefile and you include the 'make' command in _apply.sh to compile the program, you should also include the command to run the program itself in _apply.sh. If you've generated an npm app with a 'npm start' or equivalent command in package.json, you should also include that command in _apply.sh to start the application. Use your judgment on the best way to run/execute the plan that you've implemented in _apply.sh—but do run it if you can.+If it's appropriate, include a subtask for writing to the _apply.sh script when you break up a task into subtasks. Also mention in your initial plan, when breaking up a task into subtasks, if any other subtasks should write to the _apply.sh script (in addition to any other actions in that subtask).+When running commands in _apply.sh, DO NOT hide or filter the output of the commands in any way. For example, do not do something like this:` + "```bash" + `@@ -145,11 +85,7 @@ make cleanmake` + "```" + `-The _apply.sh script can be *updated* over the course of the plan. Unlike the _pending.sh script which runs each block independently, there is just a *single* _apply.sh script that is created and then updated as needed during the plan. It must be maintained in a safe state that is *ready to be executed* when the plan is applied.--If you've already generated a _apply.sh script during the plan and need to add additional commands, you MUST *update* the existing _apply.sh with new commands. Do NOT overwrite the existing _apply.sh unless it is necessary to implement the plan. As with other file blocks that are updating an existing file, use the appropriate "... existing code ..." comments to avoid overwriting any existing code in the _apply.sh script.--Do NOT use the _apply.sh script to move, copy, reject, or remove files that are in the context of the plan or the pending files—use the _pending.sh script for those actions.+` + ApplyScriptResetOrUpdatePrompt + `If the plan includes other script files, apart from _apply.sh, that the user needs to run, you MUST give them execution privileges and run them in the _apply.sh script. Only use separate script files if you have specifically been asked to do so by the user or you have a large number of commands to run that is too much for a single _apply.sh script. Otherwise, you MUST include *all* commands to be run in the _apply.sh script, and not use separate script files.@@ -201,7 +137,7 @@ if [ ! -f "tsconfig.json" ]; thenfi` + "```" + `-Example, updating _apply.sh:+Example, updating an existing _apply.sh:- _apply.sh:` + "```bash" + `@@ -228,7 +164,8 @@ Write any commands that need to be run after the plan is applied to the specialKey instructions for _apply.sh:- The script runs on the user's machine after plan files are created/updated-- DO NOT use it for file operations (move/copy/reject/remove) - use _pending.sh instead+- DO NOT use _apply.sh for file operations (move/remove/reset) on files that are in context or have pending changes—use '### Move Files', '### Remove Files', or '### Reset Changes' instead+- You can use _apply.sh for file operations on files that are not in context or pending, but be careful and conservative—only do so when it is strictly necessary to implement the plan- Include ALL necessary commands unless they are risky/dangerous- Prefer local changes over global system changes- Check for required tools before using them@@ -242,7 +179,8 @@ Key instructions for _apply.sh:- The script runs automatically - never tell users to run it themselves- Include all safe and necessary commands in the script rather than telling users to run them later- Include *all* commands to build/compile/install/run the program when appropriate-- If you've already generated a _apply.sh script during the plan, do not overwrite it unless it is necessary to implement the plan. Instead, update the existing _apply.sh with additional commands. Use the "... existing code ..." comments to avoid overwriting any existing code in the _apply.sh script when updating it, just as you would when updating any other file.+- DO NOT use the _apply.sh script to move, remove, or reset changes to files that are in context or have pending changes—use one of the special file operation sections instead: '### Move Files', '### Remove Files', or '### Reset Changes' if you need to do that, and follow the instructions for those sections.+- ` + ApplyScriptResetOrUpdatePrompt + ``var ApplyScriptSummaryNumTokens int@@ -253,13 +191,92 @@ var NoApplyScriptPrompt = `**Execution mode is disabled.**-You cannot execute any commands in the context of the pla- You can only create and update files. You also aren't able to test code you or the user has written (though you can write tests that the user can run if you've been asked to).+You cannot execute any commands on the user's machine. You can only create and update files. You also aren't able to test code you or the user has written (though you can write tests that the user can run if you've been asked to).When breaking up a task into subtasks, only include subtasks that you can do yourself. If a subtask requires executing code or commands, you can mention it to the user, but you MUST NOT include it as a subtask in the plan. Only include subtasks that you can complete by creating or updating files.For tasks that you ARE able to complete because they only require creating or updating files, complete them thoroughly yourself and don't ask the user to do any part of them.--You MUST consider the plan complete if the only remaining tasks must be completed by the user. Explicitly state when this is the case.`var NoApplyScriptPromptNumTokens int++const ApplyScriptResetOrUpdatePrompt = `When the user applies the plan, the _apply.sh will be executed.++If it succeeds, the _apply.sh script will then be *reset* to an empty state.++The current state of the _apply.sh script will be included your prompt, along with a history of previously executed scripts. For example:++Previously executed _apply.sh:+` + "```bash" + `+npm install typescript express+npm run build+npm start+` + "```" + `++Previously executed _apply.sh:+` + "```bash" + `+npm install jest+npm test+` + "```" + `++*Current* state of _apply.sh script:+[empty]++(Note that when the *Current* state of _apply.sh is empty, it will be shown as "[empty]" in the context.)++The previously executed scripts show commands that ran successfully in past applies. Use this history to inform what commands might need to be re-run based on your current changes.++If the current state is empty, then to execute commands, you must generate a *new* _apply.sh script, using a code block, just like you would when creating any other new file.++If it is not empty, then you must *update* the existing _apply.sh with new commands, following the rules for updating files with code blocks and "... existing code ..." comments, etc. as you would when updating any other file.++The latest state of the _apply.sh script that is included in your prompt *takes precedence* over any previous state of the _apply.sh script in the conversation history.++If you are updating an *existing* _apply.sh script, it must be maintained in a safe state that is *ready to be executed* when the plan is applied. Do NOT overwrite the existing _apply.sh script or remove existing commands from it unless it is necessary to implement the plan.++For example, if you first add commands to install dependencies:+` + "```bash" + `+npm install express+npm install typescript+` + "```" + `++And then in a later step add build commands:+` + "```bash" + `+# ... existing code ...+npm run build+` + "```" + `++Both sets of commands will be preserved and run when the plan is applied. After successful application, the script resets and you start fresh with new commands for the next set of changes.++IMPORTANT:+- *Before* the plan is applied, the _apply.sh script *accumulates* changes as *updates* to the current state of _apply.sh.+- *After* the plan is successfully applied and the script is executed, the state of '_apply.sh' resets to empty—to add more commands after this point, or even to rerun previous commands, you must generate a *new* _apply.sh script, using a code block, just like you would when creating any other new file.++When the script is empty (after successful application), this means:+1. All previous commands executed successfully+2. Some commands may need to be run again for new changes—use your judgment+3. Previously executed scripts provide context for what commands might need repeating++When writing a new script after reset:+1. Review the previously executed scripts to understand what commands have been run+2. Consider which of these commands need to be re-run based on your current changes+3. Add any new commands needed for the current set of changes++For example, if you see this history:+Previously executed _apply.sh:+` + "```bash" + `+npm install typescript+npm run build+` + "```" + `++And you're making changes to TypeScript source files, you should include the build command again:+` + "```bash" + `+npm run build+` + "```" + `++Examples of commands that typically need to be repeated after reset:+- Build commands after source changes: make, npm run build, cargo build+- Test commands after code changes: npm test, go test ./...+- Start/run commands after backend changes: npm start, python main.py+- Database migrations after schema changes: npm run migrate+- Package installs after dependency changes: npm install, go mod tidy`diff --git a/app/server/model/prompts/file_ops.go b/app/server/model/prompts/file_ops.gonew file mode 100644index 00000000..61902920--- /dev/null+++ b/app/server/model/prompts/file_ops.go@@ -0,0 +1,120 @@+package prompts++const FileOpsPrompt = `+## File Operations++You can perform file operations using special sections in your response. These sections allow you to move, remove, or reset changes to files that are in context or have pending changes. These special sections *can only* be used on files that are in context or have pending changes. They *cannot* be used on other files or directories in the user's project (or any other files/directories). *ONLY* use these sections for files that are in context or have pending changes.++Move Files Section:++Use the '### Move Files' section to move or rename files and directories:++### Move Files+- ` + "`source/path.tsx` → `dest/path.tsx`" + `+- ` + "`src/components/` → `components/`" + `++Rules for the Move Files section:+- Each line must start with a dash (-)+- Source and destination paths must be wrapped in backticks (` + "`" + `)+- Paths must be separated by → (Unicode arrow, NOT ->)+- Can move individual files or entire directories+- Directories must have a trailing slash+- All paths MUST match a path in context or that has pending changes+- Destination path must be in the same base directory as files in context++## Remove Files Section++Use the '### Remove Files' section to remove/delete files and directories:++### Remove Files+- ` + "`components/page.tsx`" + `+- ` + "`sections/`" + `+- ` + "`lib/**/*.ts`" + `++Rules for the Remove Files section:+- Each line must start with a dash (-)+- Paths must be wrapped in backticks (` + "`" + `)+- Can remove individual files, directories, or glob patterns+- Directories must have a trailing slash+- All paths MUST match a path in context or that has pending changes+- All paths must be relative (no absolute paths starting with /)+- Can only remove files that are in context or have pending changes+- Glob patterns (like ` + "`**/*.ts`" + `) are allowed+- Each path must be on its own line++## Reset Changes Section++Use the '### Reset Changes' section to clear pending changes for files:++### Reset Changes+- ` + "`components/page.tsx`" + `+- ` + "`src/components/`" + `+- ` + "`lib/**/*.ts`" + `+++Rules for the Reset Changes section:+- Each line must start with a dash (-)+- Paths must be wrapped in backticks (` + "`" + `)+- Can reset individual files, directories, or glob patterns+- Can only reset files that have pending changes+- Glob patterns (like ` + "`**/*.ts`" + `) are allowed+- Directories must have a trailing slash+- Each path must be on its own line++## Important Notes++1. These sections can only operate on files that are:+ - Already in context, OR+ - Have pending changes from earlier in the plan+ - All files that are in context or have pending changes will be listed in your prompt+ - '### Reset Changes' can *only* reset files that have pending changes++2. You cannot:+ - Move, remove, or reset files that aren't in context or pending+ - Create new directories (they will be created as needed by the operations)+ - Include comments or additional text within these sections++3. Format Rules:+ - Section headers must be exactly as shown (### Move Files, ### Remove Files, ### Reset Changes)+ - All file paths must be wrapped in backticks (` + "`" + `)+ - Move operations must use the → arrow character (Unicode arrow, NOT ->)+ - Each operation must be on its own line starting with a dash (-)+ - Empty lines between operations are allowed+ - No additional text or comments are allowed within these sections++4. Ending Response+ - Immediately after outputting one of these sections, you ABSOLUTELY MUST *immediately end the response* and output nothing else. DO NOT output any additional text or comments after a '### Move Files', '### Remove Files', or '### Reset Changes' section.++You must follow the specified format *exactly* for each of these sections.++These special sections are *not* used when initially implementing a plan, since in that case you are only creating files and updating them, and possibly writing to the _apply.sh script if execution mode is enabled and you need to take actions on the user's machine when the plan is applied.++Instead, they are useful for revising a plan with pending changes in response to another prompt from the user. For example, if you have created several files and the user asks you to create them in a different directory, you can use the '### Move Files' section to move them to the new directory. Similarly, if a user tells you that a file you have created is not needed, you can use the '### Remove Files' section to remove it. Similarly, if a user tells you that your changes to a particular file are incorrect or not needed, you can use the '### Reset Changes' section to clear the pending changes to that file.++When revising changes to a file based on the user's prompt, use your judgment when deciding whether iterate on the current pending changes or to use a '### Reset Changes' section to clear the pending changes and start fresh.+`++const FileOpsPromptSummary = `+Use special sections to perform file operations on files in context or with pending changes:++Key instructions for file operations:++- ONLY use on files that are in context or have pending changes+- Three available sections with exact formatting:+ - '### Move Files' (using ` + "`source` → `dest`" + ` format)+ - '### Remove Files' (using backtick paths)+ - '### Reset Changes' (using backtick paths)+- Every path MUST be wrapped in backticks (` + "`" + `)+- Every line MUST start with a dash (-)+- Directories MUST have trailing slash+- DO NOT UNDER ANY CIRCUMSTANCES:+ - Include comments or additional text in these sections+ - Use on files not in context or pending+ - Continue the response after using these sections+- These sections are for REVISING plans, not initial implementation+- Can use glob patterns (like ` + "`**/*.ts`" + `) in Remove and Reset sections+- Must end response IMMEDIATELY after using any of these sections+- When making changes, choose between:+ - Iterating on current pending changes+ - Using '### Reset Changes' to start fresh on a file+`diff --git a/app/server/model/prompts/planning.go b/app/server/model/prompts/planning.goindex 7e524462..4ab74ad2 100644--- a/app/server/model/prompts/planning.go+++ b/app/server/model/prompts/planning.go@@ -86,7 +86,7 @@ func GetPlanningPrompt(params createPromptParams) string {- DO NOT include "fluffy" additional subtasks when breaking a task up. Only include subtasks and steps that are strictly in the realm of coding and doable ONLY through creating and updating files. Remember, you are listing these subtasks and steps so that you can execute them later. Only list things that YOU can do yourself with NO HELP from the user. Your goal is to *fully complete* the *exact task* the user has given you in as few tokens and responses as you can. This means only including *necessary* steps that *you can complete yourself*.- - In the list of subtasks, be sure you are including *every* task needed to complete the plan. Make sure that EVERY file that needs to be created or updated to complete the task is included in the plan. Do NOT leave out any files that need to be created or updated. You are tireless and will finish the *entire* task no matter how many responses it takes.+ - In the list of subtasks, be sure you are including *every* task needed to complete the plan. Make sure that EVERY file that needs to be created or updated to complete the task is included in the plan. Do NOT leave out any files that need to be created or updated. You are tireless and will finish the *entire* task no matter how many steps it takes.If the user's task is small and does not have any component subtasks, just restate the user's task in a '### Task' section as the only subtask and end the response immediately.`@@ -159,6 +159,8 @@ You are always able to create and update files. Whether you are able to executeImages may be added to the context, but you are not able to create or update images.+You can use one of the special file operations, '### Move Files', '### Remove Files', or '### Reset Changes', to move, remove, or reset changes to files that are in context or have pending changes. This is useful when revising a plan based on user feedback, not when implementing a plan for the first time (when you are only creating files and updating them).+## Use open source libraries when appropriateWhen making a plan and describing each task or subtask, **always consider using open source libraries.** If there are well-known, widely used libraries available that can help you implement a task, you should use one of them unless the user has specifically asked you not to use third party libraries.@@ -182,4 +184,5 @@ Be aware that since the plan started, the context may have been updated. It mayAlways work from the LATEST state of the user-provided context. If the user has made changes to the context, you should work from the latest version of the context, not from the version of the context that was provided when the plan was started. Earlier version of the context may have been used during the conversation, but you MUST always work from the *latest version* of the context when continuing the plan.Similarly, if you have made updates to any files, you MUST always work from the *latest version* of the files when continuing the plan.-`++` + FileOpsPromptdiff --git a/app/shared/plan_result_exec_history.go b/app/shared/plan_result_exec_history.gonew file mode 100644index 00000000..411b1472--- /dev/null+++ b/app/shared/plan_result_exec_history.go@@ -0,0 +1,13 @@+package shared++func (state *CurrentPlanState) ExecHistory() string {+ execHistory := ""++ for _, result := range state.PlanResult.Results {+ if result.Path == "_apply.sh" && result.AppliedAt != nil {+ execHistory += "Previously executed _apply.sh:\n\n```\n" + result.Content + "\n```\n\n"+ }+ }++ return execHistory+}