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
lots of work on execution, auto-debug loops, and race condition fixes
Dane Schneider0569366f7a0c
diff --git a/app/cli/changes_tui/run.go b/app/cli/changes_tui/run.goindex 337f20e4..8a1b0e0d 100644--- a/app/cli/changes_tui/run.go+++ b/app/cli/changes_tui/run.go@@ -3,6 +3,7 @@ package changes_tuiimport ("fmt""plandex/lib"+ "plandex/plan_exec""plandex/term"tea "github.com/charmbracelet/bubbletea"@@ -38,7 +39,15 @@ func StartChangesUI(currentPlan *shared.CurrentPlanState) error {}if mod.shouldApplyAll {- lib.MustApplyPlan(lib.CurrentPlanId, lib.CurrentBranch, true, false, false)+ flags := lib.ApplyFlags{+ AutoConfirm: true,+ }+ lib.MustApplyPlan(+ lib.CurrentPlanId,+ lib.CurrentBranch,+ flags,+ plan_exec.GetOnApplyExecFail(flags),+ )}if mod.rejectFileErr != nil {diff --git a/app/cli/cmd/apply.go b/app/cli/cmd/apply.goindex c97bfc3f..51ff46d6 100644--- a/app/cli/cmd/apply.go+++ b/app/cli/cmd/apply.go@@ -3,16 +3,22 @@ package cmdimport ("plandex/auth""plandex/lib"+ "plandex/plan_exec""plandex/term"+ "strconv""github.com/spf13/cobra")-var autoCommit, noCommit bool+var autoCommit, noCommit, autoExec boolfunc init() {applyCmd.Flags().BoolVarP(&autoCommit, "commit", "c", false, "Commit changes to git")- applyCmd.Flags().BoolVarP(&noCommit, "no-commit", "n", false, "Do not commit changes to git")+ applyCmd.Flags().BoolVar(&noCommit, "no-commit", false, "Do not commit changes to git")+ applyCmd.Flags().BoolVar(&noExec, "no-exec", false, "Disable _apply.sh execution")+ applyCmd.Flags().BoolVar(&autoExec, "auto-exec", false, "Automatically execute commands without confirmation")+ applyCmd.Flags().Var(newAutoDebugValue(&autoDebug), "debug", "Automatically execute and debug failing commands (optionally specify number of tries—default is 5)")+ applyCmd.Flag("debug").NoOptDefVal = strconv.Itoa(defaultAutoDebugTries)RootCmd.AddCommand(applyCmd)}@@ -31,5 +37,19 @@ func apply(cmd *cobra.Command, args []string) {term.OutputNoCurrentPlanErrorAndExit()}- lib.MustApplyPlan(lib.CurrentPlanId, lib.CurrentBranch, true, autoCommit, noCommit)+ flags := lib.ApplyFlags{+ AutoConfirm: true,+ AutoCommit: autoCommit,+ NoCommit: noCommit,+ AutoExec: autoExec,+ NoExec: noExec,+ AutoDebug: autoDebug,+ }++ lib.MustApplyPlan(+ lib.CurrentPlanId,+ lib.CurrentBranch,+ flags,+ plan_exec.GetOnApplyExecFail(flags),+ )}diff --git a/app/cli/cmd/build.go b/app/cli/cmd/build.goindex 5d24856c..644e5ccd 100644--- a/app/cli/cmd/build.go+++ b/app/cli/cmd/build.go@@ -27,6 +27,9 @@ func init() {buildCmd.Flags().BoolVarP(&autoConfirm, "yes", "y", false, "Automatically confirm context updates")buildCmd.Flags().BoolVarP(&tellAutoApply, "apply", "a", false, "Automatically apply changes (and confirm context updates)")buildCmd.Flags().BoolVarP(&autoCommit, "commit", "c", false, "Commit changes to git when --apply/-a is passed")+ buildCmd.Flags().BoolVar(&noExec, "no-exec", false, "Disable command execution")+ buildCmd.Flags().BoolVar(&autoExec, "auto-exec", false, "Automatically execute commands without confirmation when --apply is passed")+ buildCmd.Flags().Var(newAutoDebugValue(&autoDebug), "debug", "Automatically execute and debug failing commands (optionally specify number of tries—default is 5)")}func build(cmd *cobra.Command, args []string) {@@ -68,7 +71,21 @@ func build(cmd *cobra.Command, args []string) {fmt.Println()term.PrintCmds("", "ps", "connect", "stop")} else if tellAutoApply {- lib.MustApplyPlan(lib.CurrentPlanId, lib.CurrentBranch, true, autoCommit, !autoCommit)+ flags := lib.ApplyFlags{+ AutoConfirm: true,+ AutoCommit: autoCommit,+ NoCommit: !autoCommit,+ NoExec: noExec,+ AutoExec: autoExec,+ AutoDebug: autoDebug,+ }++ lib.MustApplyPlan(+ lib.CurrentPlanId,+ lib.CurrentBranch,+ flags,+ plan_exec.GetOnApplyExecFail(flags),+ )} else {fmt.Println()term.PrintCmds("", "diff", "diff --ui", "apply", "reject", "log")diff --git a/app/cli/cmd/continue.go b/app/cli/cmd/continue.goindex 789b0e2c..f95dc85f 100644--- a/app/cli/cmd/continue.go+++ b/app/cli/cmd/continue.go@@ -27,6 +27,9 @@ func init() {continueCmd.Flags().BoolVarP(&autoConfirm, "yes", "y", false, "Automatically confirm context updates")continueCmd.Flags().BoolVarP(&tellAutoApply, "apply", "a", false, "Automatically apply changes (and confirm context updates)")continueCmd.Flags().BoolVarP(&autoCommit, "commit", "c", false, "Commit changes to git when --apply/-a is passed")+ continueCmd.Flags().BoolVar(&noExec, "no-exec", false, "Disable command execution")+ continueCmd.Flags().BoolVar(&autoExec, "auto-exec", false, "Automatically execute commands without confirmation when --apply is passed")+ continueCmd.Flags().Var(newAutoDebugValue(&autoDebug), "debug", "Automatically execute and debug failing commands (optionally specify number of tries—default is 5)")}func doContinue(cmd *cobra.Command, args []string) {@@ -56,9 +59,24 @@ func doContinue(cmd *cobra.Command, args []string) {TellStop: tellStop,TellNoBuild: tellNoBuild,IsUserContinue: true,+ ExecEnabled: !noExec,})if tellAutoApply {- lib.MustApplyPlan(lib.CurrentPlanId, lib.CurrentBranch, true, autoCommit, !autoCommit)+ flags := lib.ApplyFlags{+ AutoConfirm: true,+ AutoCommit: autoCommit,+ NoCommit: !autoCommit,+ AutoExec: autoExec,+ NoExec: noExec,+ AutoDebug: autoDebug,+ }++ lib.MustApplyPlan(+ lib.CurrentPlanId,+ lib.CurrentBranch,+ flags,+ plan_exec.GetOnApplyExecFail(flags),+ )}}diff --git a/app/cli/cmd/debug.go b/app/cli/cmd/debug.goindex fa42548c..0277adbb 100644--- a/app/cli/cmd/debug.go+++ b/app/cli/cmd/debug.go@@ -127,6 +127,18 @@ func doDebug(cmd *cobra.Command, args []string) {},}, prompt, plan_exec.TellFlags{IsUserDebug: true})- lib.MustApplyPlan(lib.CurrentPlanId, lib.CurrentBranch, true, autoCommit, !autoCommit)+ flags := lib.ApplyFlags{+ AutoConfirm: true,+ AutoCommit: autoCommit,+ NoCommit: !autoCommit,+ NoExec: true,+ }++ lib.MustApplyPlan(+ lib.CurrentPlanId,+ lib.CurrentBranch,+ flags,+ plan_exec.GetOnApplyExecFail(flags),+ )}}diff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.goindex 57e4ffb8..98dce5ef 100644--- a/app/cli/cmd/tell.go+++ b/app/cli/cmd/tell.go@@ -11,6 +11,7 @@ import ("plandex/lib""plandex/plan_exec""plandex/term"+ "strconv""strings""time"@@ -24,6 +25,7 @@ import ()const defaultEditor = "vim"+const defaultAutoDebugTries = 5var autoConfirm bool@@ -35,6 +37,8 @@ var tellStop boolvar tellNoBuild boolvar tellAutoApply boolvar tellAutoContext bool+var noExec bool+var autoDebug int// tellCmd represents the prompt commandvar tellCmd = &cobra.Command{@@ -56,9 +60,15 @@ func init() {tellCmd.Flags().BoolVarP(&autoConfirm, "yes", "y", false, "Automatically confirm context updates")tellCmd.Flags().BoolVar(&tellAutoApply, "apply", false, "Automatically apply changes (and confirm context updates)")- tellCmd.Flags().BoolVarP(&autoCommit, "commit", "c", false, "Commit changes to git when --apply/-a is passed")+ tellCmd.Flags().BoolVarP(&autoCommit, "commit", "c", false, "Commit changes to git when --apply is passed")tellCmd.Flags().BoolVar(&tellAutoContext, "auto-context", false, "Load and manage context automatically")++ tellCmd.Flags().BoolVar(&noExec, "no-exec", false, "Disable command execution")+ tellCmd.Flags().BoolVar(&autoExec, "auto-exec", false, "Automatically execute commands without confirmation when --apply is passed")++ tellCmd.Flags().Var(newAutoDebugValue(&autoDebug), "debug", "Automatically execute and debug failing commands (optionally specify number of tries—default is 5)")+ tellCmd.Flag("debug").NoOptDefVal = strconv.Itoa(defaultAutoDebugTries)}func doTell(cmd *cobra.Command, args []string) {@@ -95,10 +105,25 @@ func doTell(cmd *cobra.Command, args []string) {TellStop: tellStop,TellNoBuild: tellNoBuild,AutoContext: tellAutoContext,+ ExecEnabled: !noExec,})if tellAutoApply {- lib.MustApplyPlan(lib.CurrentPlanId, lib.CurrentBranch, true, autoCommit, !autoCommit)+ flags := lib.ApplyFlags{+ AutoConfirm: true,+ AutoCommit: autoCommit,+ NoCommit: !autoCommit,+ NoExec: noExec,+ AutoExec: autoExec || autoDebug > 0,+ AutoDebug: autoDebug,+ }++ lib.MustApplyPlan(+ lib.CurrentPlanId,+ lib.CurrentBranch,+ flags,+ plan_exec.GetOnApplyExecFail(flags),+ )}}@@ -210,13 +235,22 @@ func getEditorPrompt() string {func validateTellFlags() {if tellAutoApply && tellNoBuild {- term.OutputErrorAndExit("🚨 --apply/-a can't be used with --no-build/-n")+ term.OutputErrorAndExit("🚨 --apply can't be used with --no-build/-n")}if tellAutoApply && tellBg {- term.OutputErrorAndExit("🚨 --apply/-a can't be used with --bg")+ term.OutputErrorAndExit("🚨 --apply can't be used with --bg")}if autoCommit && !tellAutoApply {- term.OutputErrorAndExit("🚨 --commit/-c can only be used with --apply/-a")+ term.OutputErrorAndExit("🚨 --commit/-c can only be used with --apply")+ }+ if autoExec && !tellAutoApply {+ term.OutputErrorAndExit("🚨 --auto-exec can only be used with --apply")+ }+ if autoDebug > 0 && !tellAutoApply {+ term.OutputErrorAndExit("🚨 --debug can only be used with --apply")+ }+ if autoDebug > 0 && noExec {+ term.OutputErrorAndExit("🚨 --debug can't be used with --no-exec")}if tellAutoContext && tellBg {@@ -274,3 +308,40 @@ func maybeShowDiffs() {time.Sleep(100 * time.Millisecond)}}++// AutoDebugValue implements the flag.Value interface+type autoDebugValue struct {+ value *int+}++func newAutoDebugValue(p *int) *autoDebugValue {+ *p = 0 // Default to 0 (disabled)+ return &autoDebugValue{p}+}++func (f *autoDebugValue) Set(s string) error {+ if s == "" {+ *f.value = defaultAutoDebugTries+ return nil+ }+ v, err := strconv.Atoi(s)+ if err != nil {+ return fmt.Errorf("invalid value for --debug: %v", err)+ }+ if v <= 0 {+ return fmt.Errorf("--debug value must be greater than 0")+ }+ *f.value = v+ return nil+}++func (f *autoDebugValue) String() string {+ if f.value == nil {+ return "0"+ }+ return strconv.Itoa(*f.value)+}++func (f *autoDebugValue) Type() string {+ return "int"+}diff --git a/app/cli/lib/apply.go b/app/cli/lib/apply.goindex 3b989a6d..31912ad2 100644--- a/app/cli/lib/apply.go+++ b/app/cli/lib/apply.go@@ -1,19 +1,58 @@package libimport (+ "bufio""fmt"+ "log""os"+ "os/exec"+ "os/signal""path/filepath""plandex/api""plandex/auth""plandex/fs""plandex/term""strings"+ "syscall"+ "github.com/fatih/color""github.com/plandex/plandex/shared")-func MustApplyPlan(planId, branch string, autoConfirm, autoCommit, noCommit bool) {+type ApplyFlags struct {+ AutoConfirm bool+ AutoCommit bool+ NoCommit bool+ AutoExec bool+ NoExec bool+ AutoDebug int+}++type onErrFn func(errMsg string, errArgs ...interface{})++func MustApplyPlan(+ planId,+ branch string,+ flags ApplyFlags,+ onExecFail func(status int, output string, attempt int),+) {+ MustApplyPlanAttempt(planId, branch, flags, onExecFail, 0)+}++func MustApplyPlanAttempt(+ planId,+ branch string,+ flags ApplyFlags,+ onExecFail func(status int, output string, attempt int),+ attempt int,+) {+ log.Println("Applying plan")++ autoConfirm := flags.AutoConfirm+ autoCommit := flags.AutoCommit+ noCommit := flags.NoCommit+ noExec := flags.NoExec+term.StartSpinner("")currentPlanState, apiErr := api.Client.GetCurrentPlanState(planId, branch)@@ -79,6 +118,9 @@ func MustApplyPlan(planId, branch string, autoConfirm, autoCommit, noCommit boolisRepo := fs.ProjectRootIsGitRepo()toApply := currentPlanFiles.Files+ hasExec := currentPlanFiles.Files["_apply.sh"] != ""++ log.Printf("Files to apply: %d, Has exec script: %v", len(toApply), hasExec)if len(toApply) == 0 {term.StopSpinner()@@ -86,24 +128,7 @@ func MustApplyPlan(planId, branch string, autoConfirm, autoCommit, noCommit boolreturn}- if !autoConfirm {- term.StopSpinner()- numToApply := len(toApply)- suffix := ""- if numToApply > 1 {- suffix = "s"- }- shouldContinue, err := term.ConfirmYesNo("Apply changes to %d file%s?", numToApply, suffix)-- if err != nil {- term.OutputErrorAndExit("failed to get confirmation user input: %s", err)- }-- if !shouldContinue {- os.Exit(0)- }- term.ResumeSpinner()- }+ hasFileChanges := !hasExec || len(toApply) > 1onErr := func(errMsg string, errArgs ...interface{}) {term.StopSpinner()@@ -115,127 +140,346 @@ func MustApplyPlan(planId, branch string, autoConfirm, autoCommit, noCommit boolterm.OutputSimpleError(errMsg, unformattedErrMsg)}+ log.Println("Getting API keys")+var apiKeys map[string]stringif !auth.Current.IntegratedModelsMode {apiKeys = MustVerifyApiKeysSilent()}- var commitSummary string+ log.Println("Has file changes:", hasFileChanges)- openAIBase := os.Getenv("OPENAI_API_BASE")- if openAIBase == "" {- openAIBase = os.Getenv("OPENAI_ENDPOINT")- }+ if hasFileChanges {+ if !autoConfirm {+ log.Println("Asking user to confirm applying changes")- commitSummary, apiErr = api.Client.ApplyPlan(planId, branch, shared.ApplyPlanRequest{- ApiKeys: apiKeys,- OpenAIBase: openAIBase,- OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),- })+ term.StopSpinner()+ numToApply := len(toApply)+ suffix := ""+ if numToApply > 1 {+ suffix = "s"+ }+ shouldContinue, err := term.ConfirmYesNo("Apply changes to %d file%s?", numToApply, suffix)- if apiErr != nil {- onErr("failed to set pending results applied: %s", apiErr.Msg)- return- }+ if err != nil {+ term.OutputErrorAndExit("failed to get confirmation user input: %s", err)+ }- var updatedFiles []string- for path, content := range toApply {- // Compute destination path- dstPath := filepath.Join(fs.ProjectRoot, path)+ if !shouldContinue {+ os.Exit(0)+ }+ term.ResumeSpinner()+ }- content = strings.ReplaceAll(content, "\\`\\`\\`", "```")+ var commitSummary string- // 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- }+ openAIBase := os.Getenv("OPENAI_API_BASE")+ if openAIBase == "" {+ openAIBase = os.Getenv("OPENAI_ENDPOINT")}- if exists {- // read file content- bytes, err := os.ReadFile(dstPath)+ log.Println("Applying plan with API call")- if err != nil {- onErr("failed to read %s:", dstPath)- return- }+ commitSummary, apiErr = api.Client.ApplyPlan(planId, branch, shared.ApplyPlanRequest{+ ApiKeys: apiKeys,+ OpenAIBase: openAIBase,+ OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),+ })- // Check if the file has changed- if string(bytes) == content {- // log.Println("File is unchanged, skipping")+ if apiErr != nil {+ onErr("failed to set pending results applied: %s", apiErr.Msg)+ return+ }++ 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)++ // 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+ }}- } else {- updatedFiles = append(updatedFiles, path)- // Create the directory if it doesn't exist- err := os.MkdirAll(filepath.Dir(dstPath), 0755)+ // Write the file+ err = os.WriteFile(dstPath, []byte(content), 0644)if err != nil {- onErr("failed to create directory %s:", filepath.Dir(dstPath))+ onErr("failed to write %s:", dstPath)return}}- // Write the file- err = os.WriteFile(dstPath, []byte(content), 0644)- if err != nil {- onErr("failed to write %s:", dstPath)+ term.StopSpinner()++ log.Println("Applying plan files complete")++ if len(updatedFiles) == 0 {+ 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())+ }+ }+ }++ suffix := ""+ if len(updatedFiles) > 1 {+ suffix = "s"+ }+ fmt.Printf("✅ Applied changes, %d file%s updated\n", len(updatedFiles), suffix)}}term.StopSpinner()- if len(updatedFiles) == 0 {- fmt.Println("✅ Applied changes, but no files were updated")- return+ if _, ok := toApply["_apply.sh"]; ok && !noExec {+ handleApplyScript(flags, toApply, onErr, onExecFail, attempt)+ }+}++func handleApplyScript(+ flags ApplyFlags,+ toApply map[string]string,+ onErr onErrFn,+ onExecFail func(status int, output string, attempt int),+ attempt int,+) {+ log.Println("Handling apply script")++ color.New(term.ColorHiCyan, color.Bold).Println("🚀 Commands to execute 👇")++ content := toApply["_apply.sh"]++ md, err := term.GetMarkdown("```bash\n" + content + "\n```")++ if err != nil {+ onErr("failed to get markdown representation: %s", err)+ }++ fmt.Println(strings.TrimSpace(md))++ log.Println("Asking user to confirm executing apply script")++ var confirmed bool+ if flags.AutoExec {+ confirmed = true} 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("Execute now?")- confirmed, err = term.ConfirmYesNo("Commit Plandex updates now?")+ if err != nil {+ onErr("failed to get confirmation user input: %s", err)+ }+ }- if err != nil {- onErr("failed to get confirmation user input: %s", err)- }- }+ if confirmed {+ log.Println("Executing apply script")+ execApplyScript(toApply, onErr, onExecFail, attempt)+ }+}- if confirmed {- // Commit the changes- msg := currentPlanState.PendingChangesSummaryForApply(commitSummary)+var shellShebangs = map[string]string{+ "/bin/bash": `#!/bin/bash+`,+ "/bin/zsh": `#!/bin/zsh+`,+}- // log.Println("Committing changes with message:")- // log.Println(msg)+var applyScriptErrorHandling = map[string]string{+ "/bin/bash": `set -euo pipefail+trap 'echo "Error on line $LINENO: $BASH_COMMAND"' ERR+`,+ "/bin/zsh": `set -euo pipefail+trap 'echo "Error on line $LINENO: ${funcfiletrace[0]#*:}"' ERR+`,+}- // spew.Dump(currentPlanState)+func execApplyScript(+ toApply map[string]string,+ onErr onErrFn,+ onExecFail func(status int, output string, attempt int),+ attempt int,+) {+ log.Println("Executing apply script")- err := GitAddAndCommitPaths(fs.ProjectRoot, msg, updatedFiles, true)- if err != nil {- onGitErr("Failed to commit changes:", err.Error())- }- }+ color.New(term.ColorHiCyan, color.Bold).Println("🚀 Executing... Output below:")+ fmt.Println()++ dstPath := filepath.Join(fs.ProjectRoot, "_apply.sh")++ content := toApply["_apply.sh"]+ lines := strings.Split(content, "\n")+ filteredLines := []string{}++ for _, line := range lines {+ trimmed := strings.TrimSpace(line)+ if strings.HasPrefix(trimmed, "#!/") {+ continue+ }+ if strings.HasPrefix(trimmed, "set -") || strings.HasSuffix(trimmed, "pipefail") {+ continue+ }+ if strings.HasPrefix(trimmed, "trap") {+ continue}+ filteredLines = append(filteredLines, line)+ }++ // Detect shell+ shell := os.Getenv("SHELL")+ if shell == "" {+ shell = "/bin/bash" // fallback+ }++ // Get appropriate header+ shebang := shellShebangs[shell]+ if shebang == "" {+ shebang = shellShebangs["/bin/bash"] // fallback if shell not supported+ }+ errorHandling := applyScriptErrorHandling[shell]++ if errorHandling == "" {+ errorHandling = applyScriptErrorHandling["/bin/bash"] // fallback if shell not supported+ }++ header := shebang + "\n" + errorHandling+ content = header + "\n" + strings.Join(filteredLines, "\n")+ err := os.WriteFile(dstPath, []byte(content), 0755)++ if err != nil {+ onErr("failed to write _apply.sh: %s", err)+ }++ execCmd := exec.Command(shell, "-l", dstPath)+ execCmd.Dir = fs.ProjectRoot+ execCmd.Env = os.Environ()+ execCmd.Stdin = os.Stdin- suffix := ""- if len(updatedFiles) > 1 {- suffix = "s"+ // Create a pipe for both stdout and stderr+ pipe, err := execCmd.StdoutPipe()+ if err != nil {+ os.Remove(dstPath)+ onErr("failed to create stdout pipe: %s", err)+ }+ execCmd.Stderr = execCmd.Stdout++ if err := execCmd.Start(); err != nil {+ os.Remove(dstPath)+ onErr("failed to start command: %s", err)+ }++ // Handle SIGINT and SIGTERM -- delete _apply.sh and kill process+ sigChan := make(chan os.Signal, 1)+ signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)++ go func() {+ sig := <-sigChan+ os.Remove(dstPath)+ execCmd.Process.Signal(sig)+ }()+ defer signal.Stop(sigChan)++ // Read and display output in real-time+ scanner := bufio.NewScanner(pipe)+ var outputBuilder strings.Builder+ for scanner.Scan() {+ line := scanner.Text()+ fmt.Println(line)+ outputBuilder.WriteString(line + "\n")+ }++ err = execCmd.Wait()++ // remove _apply.sh without overwriting err val+ {+ err := os.Remove(dstPath)+ if err != nil && !os.IsNotExist(err) {+ onErr("failed to remove _apply.sh: %s", err)}- fmt.Printf("✅ Applied changes, %d file%s updated\n", len(updatedFiles), suffix)+ }++ if err != nil {+ fmt.Println()+ color.New(term.ColorHiRed, color.Bold).Println("🚨 Commands failed")++ exitErr, ok := err.(*exec.ExitError)+ status := -1+ if ok {+ status = exitErr.ExitCode()+ }+ onExecFail(status, outputBuilder.String(), attempt)+ } else {+ fmt.Println()+ fmt.Println("✅ Commands succeeded")}}diff --git a/app/cli/lib/context_update.go b/app/cli/lib/context_update.goindex 1927f80b..d8b645ab 100644--- a/app/cli/lib/context_update.go+++ b/app/cli/lib/context_update.go@@ -31,7 +31,9 @@ func CheckOutdatedContextWithOutput(quiet, autoConfirm bool, maybeContexts []*shreturn false, false, fmt.Errorf("failed to check outdated context: %s", err)}- term.StopSpinner()+ if !quiet {+ term.StopSpinner()+ }if len(outdatedRes.UpdatedContexts) == 0 && len(outdatedRes.RemovedContexts) == 0 {if !quiet {@@ -92,6 +94,8 @@ func CheckOutdatedContextWithOutput(quiet, autoConfirm bool, maybeContexts []*shphrase = "has been"}+ term.StopSpinner()+color.New(term.ColorHiCyan, color.Bold).Printf("%s in context %s modified 👇\n\n", msg, phrase)tableString := tableForContextOutdated(outdatedRes.UpdatedContexts, outdatedRes.TokenDiffsById)@@ -135,6 +139,8 @@ func CheckOutdatedContextWithOutput(quiet, autoConfirm bool, maybeContexts []*shphrase = "has been"}+ term.StopSpinner()+color.New(term.ColorHiCyan, color.Bold).Printf("%s in context %s removed 👇\n\n", msg, phrase)tableString := tableForContextOutdated(outdatedRes.RemovedContexts, outdatedRes.TokenDiffsById)diff --git a/app/cli/plan_exec/exec.go b/app/cli/plan_exec/exec.gonew file mode 100644index 00000000..061d0dc6--- /dev/null+++ b/app/cli/plan_exec/exec.go@@ -0,0 +1,61 @@+package plan_exec++import (+ "fmt"+ "log"+ "plandex/auth"+ "plandex/lib"+ "plandex/term"++ "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) {+ var proceed bool++ if flags.AutoDebug > 0 {+ if attempt >= flags.AutoDebug {+ color.New(term.ColorHiRed, color.Bold).Printf("Commands failed %d times.\n", attempt)+ } else {+ proceed = true+ }+ }++ if !proceed {+ confirm, err := term.ConfirmYesNo("Auto-debug?")++ if err != nil {+ term.OutputErrorAndExit("failed to get confirmation user input: %s", err)+ }++ proceed = confirm+ }++ if proceed {+ var apiKeys map[string]string+ if !auth.Current.IntegratedModelsMode {+ apiKeys = lib.MustVerifyApiKeysSilent()+ }+ prompt := fmt.Sprintf("Execution of '_apply.sh' failed with exit status %d. Output:\n\n%s\n\n--\n\n",+ status, output)++ TellPlan(ExecParams{+ CurrentPlanId: lib.CurrentPlanId,+ CurrentBranch: lib.CurrentBranch,+ ApiKeys: apiKeys,+ CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {+ return lib.CheckOutdatedContextWithOutput(false, true, maybeContexts)+ },+ }, prompt, TellFlags{IsApplyDebug: true, ExecEnabled: true})++ log.Printf("Applying plan after tell")++ lib.MustApplyPlanAttempt(lib.CurrentPlanId, lib.CurrentBranch, flags, onExecFail, attempt+1)+ }+ }++ return onExecFail+}diff --git a/app/cli/plan_exec/params.go b/app/cli/plan_exec/params.goindex ea2bfd51..3cd4412d 100644--- a/app/cli/plan_exec/params.go+++ b/app/cli/plan_exec/params.go@@ -15,7 +15,9 @@ type TellFlags struct {TellNoBuild boolIsUserContinue boolIsUserDebug bool+ IsApplyDebug boolIsChatOnly boolAutoContext boolContinuedAfterAction bool+ ExecEnabled bool}diff --git a/app/cli/plan_exec/tell.go b/app/cli/plan_exec/tell.goindex 60de5a1a..0763907e 100644--- a/app/cli/plan_exec/tell.go+++ b/app/cli/plan_exec/tell.go@@ -27,6 +27,7 @@ func TellPlan(isDebugCmd := flags.IsUserDebugisChatOnly := flags.IsChatOnlyautoContext := flags.AutoContext+ execEnabled := flags.ExecEnableddone := make(chan struct{})@@ -109,6 +110,11 @@ func TellPlan(openAIOrgId = params.ApiKeys["OPENAI_ORG_ID"]}+ var osDetails string+ if execEnabled {+ osDetails = term.GetOsDetails()+ }+apiErr := api.Client.TellPlan(params.CurrentPlanId, params.CurrentBranch, shared.TellPlanRequest{Prompt: prompt,ConnectStream: !tellBg,@@ -119,6 +125,8 @@ func TellPlan(IsUserDebug: isDebugCmd,IsChatOnly: isChatOnly,AutoContext: autoContext,+ ExecEnabled: execEnabled,+ OsDetails: osDetails,ApiKey: legacyApiKey, // deprecatedEndpoint: openAIBase, // deprecatedApiKeys: params.ApiKeys,diff --git a/app/cli/stream_tui/view.go b/app/cli/stream_tui/view.goindex 376c4cc7..157c5731 100644--- a/app/cli/stream_tui/view.go+++ b/app/cli/stream_tui/view.go@@ -96,11 +96,19 @@ func (m streamUIModel) doRenderBuild(outputStatic bool) string {func (m streamUIModel) getRows(static bool) []string {filePaths := make([]string, 0, len(m.tokensByPath))for filePath := range m.tokensByPath {+ // _apply.sh script goes last+ if filePath == "_apply.sh" {+ continue+ }filePaths = append(filePaths, filePath)}sort.Strings(filePaths)+ if _, ok := m.tokensByPath["_apply.sh"]; ok {+ filePaths = append(filePaths, "_apply.sh")+ }+lbl := "Building plan "bgColor := color.BgGreenbuilt := false@@ -127,7 +135,12 @@ func (m streamUIModel) getRows(static bool) []string {for _, filePath := range filePaths {tokens := m.tokensByPath[filePath]finished := m.finished || m.finishedByPath[filePath] || built- block := fmt.Sprintf("📄 %s", filePath)+ icon := "📄"+ label := filePath+ if filePath == "_apply.sh" {+ icon = "🚀"+ }+ block := fmt.Sprintf("%s %s", icon, label)if finished {block += " ✅"diff --git a/app/cli/term/os.go b/app/cli/term/os.gonew file mode 100644index 00000000..b7272aaa--- /dev/null+++ b/app/cli/term/os.go@@ -0,0 +1,17 @@+package term++import (+ "fmt"+ "os"+ "runtime"+)++func GetOsDetails() string {+ return fmt.Sprintf(+ "OS: %s\nArchitecture: %s\nCPUs: %d\nShell: %s",+ runtime.GOOS,+ runtime.GOARCH,+ runtime.NumCPU(),+ os.Getenv("SHELL"),+ )+}diff --git a/app/server/db/git.go b/app/server/db/git.goindex 414fb4c9..07abb8be 100644--- a/app/server/db/git.go+++ b/app/server/db/git.go@@ -394,21 +394,48 @@ func gitCommit(repoDir, commitMsg string) error {func gitRemoveIndexLockFileIfExists(repoDir string) error {// Remove the lock file if it existslockFilePath := filepath.Join(repoDir, ".git", "index.lock")+_, err := os.Stat(lockFilePath)+ exists := err == nil+ log.Println("index.lock file exists:", exists)+ if err != nil && !os.IsNotExist(err) {+ return fmt.Errorf("error checking lock file: %v", err)+ }- // log.Println("lockFilePath:", lockFilePath)- // log.Println("exists:", err == nil)+ attempts := 0+ for exists {+ if attempts > 10 {+ return fmt.Errorf("error removing index.lock file: %v after %d attempts", err, attempts)+ }++ log.Println("removing index.lock file:", lockFilePath, "attempt:", attempts)- if err == nil {if err := os.Remove(lockFilePath); err != nil {if os.IsNotExist(err) {+ log.Println("index.lock file not found, skipping removal")return nil}return fmt.Errorf("error removing lock file: %v", err)}- } else if !os.IsNotExist(err) {- return fmt.Errorf("error checking lock file: %v", err)++ _, err = os.Stat(lockFilePath)+ exists = err == nil++ if err != nil && !os.IsNotExist(err) {+ return fmt.Errorf("error checking lock file: %v", err)+ }++ log.Println("after removal, index.lock file exists:", exists)+ if exists {+ log.Println("index.lock file still exists, retrying after delay")+ } else {+ log.Println("index.lock file removed successfully")+ return nil+ }++ attempts+++ time.Sleep(20 * time.Millisecond)}return nil@@ -435,7 +462,8 @@ func gitWriteOperation(operation func() error) error {log.Printf("Retry attempt %d failed. Error: %v", attempt+1, err)isIndexLockError := strings.Contains(err.Error(), "new_index file") ||- strings.Contains(err.Error(), "index.lock")+ strings.Contains(err.Error(), "index.lock") ||+ strings.Contains(err.Error(), "index file")if isIndexLockError {log.Printf("Retry attempt %d failed due to 'unable to write git index file error'. Waiting %v before retrying. Error: %v", attempt+1, retryInterval, err)diff --git a/app/server/db/locks.go b/app/server/db/locks.goindex a775d76a..9cf10ace 100644--- a/app/server/db/locks.go+++ b/app/server/db/locks.go@@ -36,7 +36,7 @@ func LockRepo(params LockRepoParams) (string, error) {}func lockRepo(params LockRepoParams, numRetry int) (string, error) {- log.Printf("locking repo. orgId: %s | planId: %s | scope: %s, \n", params.OrgId, params.PlanId, params.Scope)+ log.Printf("locking repo. orgId: %s | planId: %s | scope: %s | branch %s | numRetry %d \n", params.OrgId, params.PlanId, params.Scope, params.Branch, numRetry)// spew.Dump(params)orgId := params.OrgId@@ -130,14 +130,15 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {return "", err}- log.Printf("Serialization or deadlock error, retrying transaction: %v\n", err)+ wait := initialRetryInterval * time.Duration(1<<numRetry) * time.Duration(rand.Intn(200)*int(time.Millisecond))- wait := initialRetryInterval * time.Duration(1<<numRetry) * time.Duration(rand.Intn(500)*int(time.Millisecond))+ log.Printf("Serialization or deadlock error, retrying transaction after %v: %v\n", wait, err)select {case <-ctx.Done():return "", fmt.Errorf("context finished during retry transaction")case <-time.After(wait):+ log.Printf("Retrying transaction after %v\n", wait)return lockRepo(params, numRetry+1)}}diff --git a/app/server/db/result_helpers.go b/app/server/db/result_helpers.goindex 99ca061d..39cf6212 100644--- a/app/server/db/result_helpers.go+++ b/app/server/db/result_helpers.go@@ -376,7 +376,14 @@ func ApplyPlan(ctx context.Context, orgId, userId, branchName string, plan *PlanerrCh <- fmt.Errorf("error getting plan file results: %v", err)return}- results = res+ filtered := []*PlanFileResult{}+ for _, result := range res {+ if result.Path != "_apply.sh" {+ filtered = append(filtered, result)+ }+ }++ results = filterederrCh <- nil}()@@ -423,6 +430,8 @@ func ApplyPlan(ctx context.Context, orgId, userId, branchName string, plan *Plan}}+ log.Printf("Pending db results: %d", len(pendingDbResults))+pendingNewFilesSet := make(map[string]bool)pendingUpdatedFilesSet := make(map[string]bool)for _, result := range pendingDbResults {@@ -505,24 +514,27 @@ func ApplyPlan(ctx context.Context, orgId, userId, branchName string, plan *Plan})}- res, _, err := LoadContexts(- ctx,- LoadContextsParams{- OrgId: orgId,- UserId: userId,- Plan: plan,- BranchName: branchName,- Req: &loadReq,- SkipConflictInvalidation: true, // no need to invalidate conflicts when applying plan--and fixes race condition since invalidation check loads description- },- )+ if len(loadReq) > 0 {+ res, _, err := LoadContexts(+ ctx,+ LoadContextsParams{+ OrgId: orgId,+ UserId: userId,+ Plan: plan,+ BranchName: branchName,+ Req: &loadReq,+ SkipConflictInvalidation: true, // no need to invalidate conflicts when applying plan--and fixes race condition since invalidation check loads description+ },+ )- if err != nil {- errCh <- fmt.Errorf("error loading context: %v", err)- return+ if err != nil {+ errCh <- fmt.Errorf("error loading context: %v", err)+ return+ }++ loadContextRes = res}- loadContextRes = reserrCh <- nil}()}@@ -537,22 +549,24 @@ func ApplyPlan(ctx context.Context, orgId, userId, branchName string, plan *Plan}}- res, err := UpdateContexts(- UpdateContextsParams{- OrgId: orgId,- Plan: plan,- BranchName: branchName,- Req: &updateReq,- SkipConflictInvalidation: true, // no need to invalidate conflicts when applying plan--and fixes race condition since invalidation check loads description- },- )+ if len(updateReq) > 0 {+ res, err := UpdateContexts(+ UpdateContextsParams{+ OrgId: orgId,+ Plan: plan,+ BranchName: branchName,+ Req: &updateReq,+ SkipConflictInvalidation: true, // no need to invalidate conflicts when applying plan--and fixes race condition since invalidation check loads description+ },+ )- if err != nil {- errCh <- fmt.Errorf("error updating context: %v", err)- return- }+ if err != nil {+ errCh <- fmt.Errorf("error updating context: %v", err)+ return+ }- updateContextRes = res+ updateContextRes = res+ }errCh <- nil}()diff --git a/app/server/handlers/plans_changes.go b/app/server/handlers/plans_changes.goindex 1c230e8f..4d14bbee 100644--- a/app/server/handlers/plans_changes.go+++ b/app/server/handlers/plans_changes.go@@ -32,6 +32,9 @@ func CurrentPlanHandler(w http.ResponseWriter, r *http.Request) {return}+ // Just in case this was sent immediately after a stream finished, wait a little before locking to allow for cleanup+ time.Sleep(100 * time.Millisecond)+var err errorctx, cancel := context.WithCancel(context.Background())@@ -104,6 +107,9 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {return}+ // Just in case this was sent immediately after a stream finished, wait a little before locking to allow for cleanup+ time.Sleep(100 * time.Millisecond)+ctx, cancel := context.WithCancel(r.Context())unlockFn := LockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)if unlockFn == nil {diff --git a/app/server/model/plan/activate.go b/app/server/model/plan/activate.goindex 5fff91ef..92cb6624 100644--- a/app/server/model/plan/activate.go+++ b/app/server/model/plan/activate.go@@ -6,6 +6,7 @@ import ("plandex-server/db""plandex-server/host""plandex-server/types"+ "time""github.com/plandex/plandex/shared""github.com/sashabaranov/go-openai"@@ -20,6 +21,13 @@ func activatePlan(buildOnly,autoContext bool,) (*types.ActivePlan, error) {+ log.Printf("Activate plan: plan ID %s on branch %s\n", plan.Id, branch)++ // Just in case this request was made immediately after another stream finished, wait a little to allow for cleanup+ log.Println("Waiting 100ms before checking for active plan")+ time.Sleep(100 * time.Millisecond)+ log.Println("Done waiting, checking for active plan")+active := GetActivePlan(plan.Id, branch)if active != nil {log.Printf("Tell: Active plan found for plan ID %s on branch %s\n", plan.Id, branch) // Log if an active plan is founddiff --git a/app/server/model/plan/build_exec.go b/app/server/model/plan/build_exec.goindex ef024a2c..6d911486 100644--- a/app/server/model/plan/build_exec.go+++ b/app/server/model/plan/build_exec.go@@ -205,6 +205,9 @@ func (fileState *activeBuildStreamFileState) buildFile() {var currentState stringcurrentPlanFile, fileInCurrentPlan := currentPlan.CurrentPlanFiles.Files[filePath]+ log.Println("plan files:")+ spew.Dump(currentPlan.CurrentPlanFiles.Files)+if fileInCurrentPlan {log.Printf("File %s found in current plan.\n", filePath)currentState = currentPlanFilediff --git a/app/server/model/plan/build_finish.go b/app/server/model/plan/build_finish.goindex be9d30bf..ef4e6c36 100644--- a/app/server/model/plan/build_finish.go+++ b/app/server/model/plan/build_finish.go@@ -8,6 +8,7 @@ import ("plandex-server/types""strings"+ "github.com/davecgh/go-spew/spew""github.com/plandex/plandex/shared")@@ -257,6 +258,7 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanF}()log.Println("Storing plan result")+ spew.Dump(planRes)err = db.StorePlanResult(planRes)if err != nil {diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex 88a578fd..80324c39 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -197,6 +197,13 @@ func execTellPlan(} else {sysCreate = prompts.SysCreateBasic}++ if req.ExecEnabled {+ sysCreate += prompts.ApplyScriptPrompt+ } else {+ sysCreate += prompts.NoApplyScriptPrompt+ }+systemMessageText := sysCreate + modelContextTextsystemMessage := openai.ChatCompletionMessage{Role: openai.ChatMessageRoleSystem,@@ -248,6 +255,16 @@ func execTellPlan(}}+ osDetailsTokens, err := shared.GetNumTokens(req.OsDetails)+ if err != nil {+ log.Printf("Error getting num tokens for os details: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error getting num tokens for os details",+ }+ }+var (numPromptTokens intpromptTokens int@@ -264,14 +281,18 @@ func execTellPlan(}return}- promptTokens = prompts.PromptWrapperTokens + numPromptTokens+ promptTokens = prompts.PromptWrapperTokens + numPromptTokens + osDetailsTokens} else if iteration > 0 && missingFileResponse == "" {if verifyDiffs != "" {numPromptTokens = prompts.VerifyDiffsPromptTokens} else {numPromptTokens = prompts.AutoContinuePromptTokens}- promptTokens = prompts.PromptWrapperTokens + numPromptTokens+ promptTokens = prompts.PromptWrapperTokens + numPromptTokens + osDetailsTokens+ }++ if req.ExecEnabled {+ promptTokens += prompts.ApplyScriptSummaryNumTokens}var sysCreateTokens int@@ -285,6 +306,12 @@ func execTellPlan(sysCreateTokens = prompts.SysCreateBasicNumTokens}+ if req.ExecEnabled {+ sysCreateTokens += prompts.ApplyScriptPromptNumTokens+ } else {+ sysCreateTokens += prompts.NoApplyScriptPromptNumTokens+ }+state.tokensBeforeConvo = sysCreateTokens + modelContextTokens + state.latestSummaryTokens + promptTokens// print out breakdown of token usage@@ -310,6 +337,11 @@ func execTellPlan(return}+ var applyScriptSummary string+ if req.ExecEnabled {+ applyScriptSummary = prompts.ApplyScriptPromptSummary+ }+state.replyId = uuid.New().String()state.replyParser = types.NewReplyParser()@@ -339,7 +371,7 @@ func execTellPlan(state.messages = state.messages[:len(state.messages)-1]promptMessage = &openai.ChatCompletionMessage{Role: openai.ChatMessageRoleUser,- Content: prompts.GetWrappedPrompt(lastMessage.Content),+ Content: prompts.GetWrappedPrompt(lastMessage.Content, req.OsDetails, applyScriptSummary),}state.userPrompt = lastMessage.Content@@ -351,7 +383,7 @@ func execTellPlan(// otherwise we'll use the continue promptpromptMessage = &openai.ChatCompletionMessage{Role: openai.ChatMessageRoleUser,- Content: prompts.GetWrappedPrompt(prompts.UserContinuePrompt),+ Content: prompts.GetWrappedPrompt(prompts.UserContinuePrompt, req.OsDetails, applyScriptSummary),}}@@ -365,6 +397,9 @@ func execTellPlan(} else if req.IsUserDebug {prompt = req.Prompt + prompts.DebugPromptstate.totalRequestTokens += prompts.DebugPromptTokens+ } else if req.IsApplyDebug {+ prompt = req.Prompt + prompts.ApplyDebugPrompt+ state.totalRequestTokens += prompts.ApplyDebugPromptTokens} else {prompt = req.Prompt}@@ -420,7 +455,7 @@ func execTellPlan(promptMessage = &openai.ChatCompletionMessage{Role: openai.ChatMessageRoleUser,- Content: prompts.GetWrappedPrompt(prompt),+ Content: prompts.GetWrappedPrompt(prompt, req.OsDetails, applyScriptSummary),}}@@ -496,6 +531,9 @@ func execTellPlan(// log.Printf("%s: %s\n", message.Role, message.Content)// }+ // ts := time.Now().Format("2006-01-02-150405")+ // os.WriteFile(fmt.Sprintf("generations/messages-%s.txt", ts), []byte(spew.Sdump(state.messages)), 0644)+_, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{Auth: auth,Plan: plan,diff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.goindex 47120a22..4335cf2e 100644--- a/app/server/model/plan/tell_stream.go+++ b/app/server/model/plan/tell_stream.go@@ -524,6 +524,7 @@ mainLoop:}// log.Printf("Adding chunk to parser: %s\n", content)+ // log.Printf("fileOpen: %v\n", fileOpen)replyParser.AddChunk(content, true)parserRes := replyParser.Read()@@ -535,9 +536,11 @@ mainLoop:if fileOpen && strings.HasSuffix(content, "```") {log.Println("FinishAndRead because of closing backticks")parserRes = replyParser.FinishAndRead()+ fileOpen = false}if fileOpen && parserRes.CurrentFilePath == "" {+ // log.Println("File open but current file path is empty, closing file")fileOpen = false}@@ -663,6 +666,12 @@ mainLoop:}log.Printf("Detected file: %s\n", file)++ if file == "_pending.sh" {+ // TODO: extract and queue commands from _pending.sh+ continue+ }+if req.BuildMode == shared.BuildModeAuto {log.Printf("Queuing build for %s\n", file)buildState := &activeBuildStreamState{diff --git a/app/server/model/prompts/create.go b/app/server/model/prompts/create.goindex a4cc89c9..b3f89417 100644--- a/app/server/model/prompts/create.go+++ b/app/server/model/prompts/create.go@@ -28,7 +28,7 @@ func init() {panic(fmt.Sprintf("Error getting number of tokens for sys msg: %v", err))}- PromptWrapperTokens, err = shared.GetNumTokens(fmt.Sprintf(promptWrapperFormatStr, ""))+ PromptWrapperTokens, err = shared.GetNumTokens(fmt.Sprintf(promptWrapperFormatStr, "", "", "", ""))if err != nil {panic(fmt.Sprintf("Error getting number of tokens for prompt wrapper: %v", err))@@ -57,6 +57,24 @@ func init() {if err != nil {panic(fmt.Sprintf("Error getting number of tokens for chat only prompt: %v", err))}++ ApplyScriptPromptNumTokens, err = shared.GetNumTokens(ApplyScriptPrompt)++ if err != nil {+ panic(fmt.Sprintf("Error getting number of tokens for apply script prompt: %v", err))+ }++ ApplyScriptSummaryNumTokens, err = shared.GetNumTokens(ApplyScriptPromptSummary)++ if err != nil {+ panic(fmt.Sprintf("Error getting number of tokens for apply script prompt summary: %v", err))+ }++ NoApplyScriptPromptNumTokens, err = shared.GetNumTokens(NoApplyScriptPrompt)++ if err != nil {+ panic(fmt.Sprintf("Error getting number of tokens for no execute script prompt: %v", err))+ }}type CreatePromptParams struct {@@ -146,7 +164,7 @@ func GetCreatePrompt(params CreatePromptParams) string {`} else {prompt += `- 1. Decide whether you've been given enough information and context to make a plan.+ 1. Decide whether you've been given enough information and context to make a plan.- Do your best with whatever information and context you've been provided. Choose sensible values and defaults where appropriate. Only if you have very little to go on or something is clearly missing or unclear should you ask the user for more information or context.a. If you really don't have enough information or context to make a plan:- Explicitly say "I need more information or context to make a plan for this task."@@ -274,15 +292,9 @@ func GetCreatePrompt(params CreatePromptParams) string {You MUST ALWAYS work on subtasks IN ORDER. You must not skip a subtask or work on subtasks out of order. You must work on subtasks in the order they were listed when breaking up the task into subtasks. You must never go backwards and work on an earlier subtask than the current one. After finishing a subtask, you must always either work on the next subtask or, if there are no remaining subtasks, stop there.".- ## Things you can't do+ ## Things you can and can't do- You are able to create and update files, but you are not able to execute code or commands. 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.+ You are always able to create and update files. Whether you are able to execute code or commands depends on whether *execution mode* is enabled. This will be specified later in the prompt.Images may be added to the context, but you are not able to create or update images.@@ -319,8 +331,6 @@ func GetCreatePrompt(params CreatePromptParams) string {- If you think the plan is done, say so, but remember that you don't have the final word on the matter. Explain why you think the plan is done.- - If you think the plan is done and any files in context have been *updated* during the plan: unless the diffs for the plan have been supplied to you in the prompt for you to verify, state that you think the plan is done (as described above), but that you will proceed to verify the generated files before the plan is fully complete. You ABSOLUTELY MUST NOT state that the plan is complete after updating any files in context—if you believe all the steps in the plan have been completed, you must instead state that all steps have been implemented, and that next you will proceed to check over the changes and fix any problems.-## EXTREMELY IMPORTANT Rules for responsesYou *must never respond with just a single paragraph.* Every response should include at least a few paragraphs that try to move a plan forward. You *especially must never* reply with just a single paragraph that begins with "Next,".@@ -386,9 +396,7 @@ Always use triple backticks to start and end code blocks.Only list out subtasks once for the plan--after that, do not list or describe a subtask that can be implemented in code without including a code block that implements the subtask.-Do not ask the user to do anything that you can do yourself with a code block. Do not say a task is too large or complex for you to complete--do your best to break down the task and complete it even if it's very large or complex.--You can ONLY create or update files. You cannot execute code or commands. If a task or subtask requires executing code or commands, mention that the user should do so and move on. You MUST consider the plan complete if the only remaining tasks must be completed by the user. Explicitly state when this is the case.+Do not ask the user to do anything that you can do yourself. Do not say a task is too large or complex for you to complete--do your best to break down the task and complete it even if it's very large or complex.Do not implement a task partially and then give up even if it's very large or complex--do your best to implement each task and subtask **fully**.@@ -402,13 +410,29 @@ ALWAYS complete subtasks in order and never go backwards in the list of subtasksIf you break up a task into subtasks, only include subtasks that can be implemented directly in code by creating or updating files. Do not include subtasks that require executing code or commands. Do not include subtasks that require user testing, deployment, or other tasks that go beyond coding.+If you need to *move* or *rename* a file that is in context or pending, use the special ` + "`move`" + ` command in the _pending.sh script. Do NOT use a file block to move a file to another path.++If you need to *copy* a file that is in context or pending, use the special ` + "`copy`" + ` command in the _pending.sh script. Do NOT use a file block to copy a file to another path.++If you need to reject/revert all the changes you've made to a file that is in context or pending, use the special ` + "`reject`" + ` command in the _pending.sh script. Do NOT use a file block to reject/revert changes to a file.++If you need to *remove* a file that is in context or pending, use the special ` + "`remove`" + ` command in the _pending.sh script. Do NOT use a file block to remove a file.++DO NOT include comments or any other commands in the _pending.sh script. It MUST ONLY include special commands and their arguments. Do NOT create directories in the _pending.sh script—the special commands will create directories as needed.+Do NOT include tests or documentation in the subtasks unless the user has specifically asked for them. Do not include extra code or features beyond what the user has asked for. Focus on the user's request and implement only what is necessary to fulfill it.+%s+The current UTC timestamp is: %s — this can be useful if you need to create a new file that includes the current date in the file name—database migrations, for example, often follow this pattern.++User's operating system details:+%s`-func GetWrappedPrompt(prompt string) string {- return fmt.Sprintf(promptWrapperFormatStr, prompt, time.Now().Format(time.RFC3339))+func GetWrappedPrompt(prompt, osDetails, applyScriptSummary string) string {+ ts := time.Now().Format(time.RFC3339)+ return fmt.Sprintf(promptWrapperFormatStr, prompt, applyScriptSummary, ts, osDetails)}var PromptWrapperTokens int@@ -468,6 +492,19 @@ Command details:var DebugPromptTokens int+const ApplyDebugPrompt = `The _apply.sh script failed and you must debug. Focus only on fixing this issue so that the command runs successfully; don't make other changes.++Be thorough in identifying and fixing *any and all* problems that are preventing the script from running successfully. If there are multiple problems, identify and fix all of them.++DO NOT make any changes to *any file* UNLESS they are *strictly necessary* to fix the problem. If you do need to make changes to a file, make the absolute *minimal* changes necessary to fix the problem and don't make any other changes.++DO NOT update the _apply.sh script unless it is necessary to fix the problem. If you do need to update the _apply.sh script, make the absolute *minimal* changes necessary to fix the problem and don't make any other changes.++**Follow all other instructions you've been given for the _apply.sh script.**+`++var ApplyDebugPromptTokens int+const ChatOnlyPrompt = `**CHAT MODE IS ENABLED.**@@ -1176,6 +1213,107 @@ Even if it's unimportant where in the original file the new code should be added*+When inserting new code between two existing blocks of code in the original file, you MUST include "... existing code ..." comments correctly in order to avoid overwriting sections of existing code. For example, if the original file looks like this:++---++func main() {+ console.log("main")+}++func fooBar() {+ console.log("fooBar")+}++func baz() {+ console.log("baz")+}++func qux() {+ console.log("qux")+}++func quix() {+ console.log("quix")+}++func qwoo() {+ console.log("qwoo")+}++func last() {+ console.log("last")+}++---++DO NOT output a file block like this to demonstrate that new code will be inserted somewhere between the 'fooBar' and 'last' functions:++---+// ... existing code ...++func fooBar() {+ console.log("fooBar")+}++func newCode() {+ console.log("newCode")+}++func last() {+ console.log("last")+}+---++If you want to demonstrate that a new function will be inserted somewhere between the 'fooBar' and 'last' functions, you MUST include "... existing code ..." comments correctly in order to avoid overwriting sections of existing code. Instead, do this to show exactly where the new function will be inserted:++---++// ... existing code ...++func baz() {+ // ... existing code ...+}++func newCode() {+ console.log("newCode")+}++func qux() {+ // ... existing code ...+}++// ... existing code ...+++Or this to show that the new function will be inserted *somehwere* between the 'fooBar' and 'last' functions:++---++// ... existing code ...++func fooBar() {+ console.log("fooBar")+}++// ... existing code ...++func newCode() {+ console.log("newCode")+}++// ... existing code ...++func last() {+ console.log("last")+}++---++Either way, you MUST NOT leave out the "... existing code ..." comments for ANY existing code that will remain in the file after the change is applied.++*+When writing an "... existing code ..." comment, you MUST use the correct comment symbol for the programming language. For example, if you are writing a plan in Python, Ruby, or Bash, you MUST use '# ... existing code ...' instead of '// ... existing code ...'. If you're writing HTML, you MUST use '<!-- ... existing code ... -->'. If you're writing jsx, tsx, svelte, or another language where the correct comment symbol(s) depend on where in the code you are, use the appropriate comment symbol(s) for where that comment is placed in the file. If you're in a javascript block of a jsx file, use '// ... existing code ...'. If you're in a markup block of a jsx file, use '{/* ... existing code ... */}'.Again, if you are writing a plan in a language that does not use '//' for comments, you absolutely must always use the appropriate comment symbol or symbols for that language instead of '//'. It is critically important that comments are ALWAYS written correctly for the language you are writing in.@@ -1256,3 +1394,243 @@ func (a *Api) LoadContext(planId, branch string, reqNow the final line is an "... existing code ..." comment, which is correct.`++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++**Execution mode is enabled.**++In addition to creating and updating files, you can also execute commands on the user's machine by writing to a another *special path*: ` + "_apply.sh" + `++This file allows you to execute commands in the context of the *user's machine*, not the Plandex server, when the user applies the changes from the plan to their project. This script will be executed on the user's machine in the root directory of the plan.++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 create directory paths for files that are in context or pending. Any required directories will be created automatically when the plan is applied.++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.++When determining whether to install a tool or exit with an error if a necessary tool or dependency is missing, you can make some assumptions about what is likely installed based on the user's operating system, the files and paths in the context of the plan, and the conversation history.++The _apply.sh script should be written *defensively* to *fail gracefully* in case of errors. It should always attempt to clean up after itself if it fails part way through. As much as possible, it should be *idempotent*.++Unless the user has specifically directed you otherwise, the _apply.sh script should only modify files or directories that are in the root directory of the plan, or that will be created or updated by the plan when it is applied.++In general, the _apply.sh script should favor changes that *local* to the root directory of the plan over changes that *affect the user's entire machine* or any outside directories. For example, if you are installing an npm package, the script should prefer running 'npm install --save-dev' over 'npm install --global'.++You can include logging for key steps in the _apply.sh script but don't overdo it. Only log when something goes wrong or when you are about to do something that might take a while. Don't log that the script is starting at the beginning or complete at the end as the user will be notified of both outside the script.++Include comments for key sections in the _apply.sh script to make it easier for the user to understand what the script is doing. But again, don't overdo it.++BE CAREFUL AND CONSERVATIVE WHEN MAKING CHANGES TO THE USER'S MACHINE. Only make changes that are necessary in the context of the plan. Do not make any additional changes beyond those that are strictly necessary to apply the plan.++If a command is risky in terms of potentially harming the user's system, or it has security implications, tell the user to take these actions themselves after the plan is applied. Do NOT include such commands in the _apply.sh script. Apart from risky or dangerous commands, or commands that aren't strictly necessary to apply the plan, you should include *all* commands to be run in the _apply.sh script. Do NOT give the user commands to run themselves after the plan is applied, *unless* they are risky or dangerous or optional and not strictly necessary to apply the plan; instead, the commands that are safe to run and are strictly necessary to apply the plan in the _apply.sh script.++Unless some required commands are potentially risky or dangerous, you MUST include *all* commands needed to implement the plan in the _apply.sh script. Do NOT leave out any commands or leave commands for the user to run themselves after the plan is applied——include them all in the _apply.sh script.++DO NOT give the user any additional commands to run—include them all in the _apply.sh script. For example, if you have created or updated a Makefile, you must include the 'make' command in the _apply.sh script instead of telling the user to run 'make' after the plan is applied. Similarly, if you have created or updated a package.json file, you must include the 'npm install' command in the _apply.sh script instead of telling the user to run 'npm install' after the plan is applied. If you have written or updated tests, you must include the command to run the tests in the _apply.sh script instead of telling the user to run the tests after the plan is applied.++If 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.++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" + `+if ! make clean && make; then+ echo "Error: Compilation failed"+ exit 1+fi+` + "```" + `++because the output of the 'make clean' and 'make' commands won't be shown to the user. Instead, run each command separately and show the output:++` + "```bash" + `+make clean+make+` + "```" + `++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.++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.++Running the _apply.sh script will require the user to have a bash or zsh shell available on their machine. You can assume that the user has bash or zsh installed. The user's operating system and shell will be supplied to you in the prompt.++You MUST NOT include the shebang line ` + "(`#!/bin/bash` or `#!/bin/zsh`)" + ` line at the top of the _apply.sh script. Every _apply.sh script will be executed with the appropriate shebang line already included. DO NOT include the shebang line in the _apply.sh script.++Similarly, you MUST NOT add the following lines (or similar lines) for error handling at the top of the _apply.sh script:++` + "```bash" + `+set -euo pipefail+trap 'echo "Error on line $LINENO: $BASH_COMMAND"' ERR+` + "```" + `++The _apply.sh script will be executed with the above error handling already included.++You DO NOT need to give the _apply.sh script execution privileges or any other permissions. This is handled outside the script.++You ABSOLUTELY MUST NOT tell the user to run the _apply.sh script or that you are waiting for them to run it. It will be run automatically when the user applies the plan.++You MUST NOT tell the user to do anything themselves that's included in the _apply.sh script. It will be run automatically when the user applies the plan.++Example, creating initial _apply.sh:++- _apply.sh:+` + "```bash" + `+# Check for node/npm+if ! command -v node > /dev/null; then+ echo "Error: node is not installed"+ exit 1+fi++if ! command -v npm > /dev/null; then+ echo "Error: npm is not installed"+ exit 1+fi++# Install dependencies+echo "Installing project dependencies..."+npm install --save-dev \+ "@types/react@^18.0.0" \+ "typescript@^4.9.0" \+ "prettier@^2.8.0"++# Generate tsconfig if it doesn't exist+if [ ! -f "tsconfig.json" ]; then+ echo "Generating TypeScript configuration..."+ npx tsc --init --jsx react+fi+` + "```" + `++Example, updating _apply.sh:++- _apply.sh:+` + "```bash" + `+# ... existing code ...++# Install dependencies+echo "Installing project dependencies..."+npm install --save-dev \+ "@types/react@^18.0.0" \+ "typescript@^4.9.0" \+ "prettier@^2.8.0" \+ "eslint@^9.0.0" \+ "jest@^29.0.0"++# ... existing code ...+` + "```" + `+`++var ApplyScriptPromptNumTokens int++const ApplyScriptPromptSummary = `+Write any commands that need to be run after the plan is applied to the special _apply.sh file.++Key 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+- Include ALL necessary commands unless they are risky/dangerous+- Prefer local changes over global system changes+- Check for required tools before using them+- Can be updated during the plan but must always be in executable state+- Should be idempotent and fail gracefully when possible+- DO NOT hide or filter the output of commands—output of all commands must be shown to the user+- DO NOT UNDER ANY CIRCUMSTANCES:+ - Add shebang or error handling (handled externally)+ - Give the _apply.sh script execution privileges or any other permissions (handled externally)+ - Create directories for plan files+- 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.+`++var ApplyScriptSummaryNumTokens int++var NoApplyScriptPrompt = `++## No execution of commands++**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).++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 intdiff --git a/app/server/model/prompts/summary.go b/app/server/model/prompts/summary.goindex e9b999c3..07b635e9 100644--- a/app/server/model/prompts/summary.go+++ b/app/server/model/prompts/summary.go@@ -36,7 +36,9 @@ You are an AI summarizer that summarizes the conversation so far. The conversatiIf the plan is just starting, there will be no existing summary, so you should just summarize the conversation between the user and yourself prior to this message. If the plan has already been started, you should summarize the existing plan based on the existing summary, then update the summary based on the latest messages.-Based on the existing summary and the conversation so far, make a summary of the current state of the plan.+Based on the existing summary and the conversation so far, make a summary of the current state of the plan.++Do not include any heading or title for the summary. Just start with the summary of the plan.- Begin with a summary of the user's messages, with particular focus any tasks they have given you. Your summary of the tasks should reflect the latest version of each task--if they have changed over time, summarize the latest state of each task that was given and omit anything that is now obsolete. Condense this information as much as possible while still being clear and retaining the meaning of the original messages.diff --git a/app/server/model/summarize.go b/app/server/model/summarize.goindex f7c40590..62fc2381 100644--- a/app/server/model/summarize.go+++ b/app/server/model/summarize.go@@ -8,6 +8,7 @@ import ("plandex-server/hooks""plandex-server/model/prompts""plandex-server/types"+ "strings""time""github.com/plandex/plandex/shared"@@ -121,10 +122,15 @@ func PlanSummary(client *openai.Client, config shared.ModelRoleConfig, params Pl// log.Println("Plan summary content:")// log.Println(content)+ summary := content+ if !strings.HasPrefix(summary, "## Summary of the plan so far:") {+ summary = "## Summary of the plan so far:\n\n" + summary+ }+return &db.ConvoSummary{OrgId: params.Auth.OrgId,PlanId: params.Plan.Id,- Summary: "## Summary of the plan so far:\n\n" + content,+ Summary: summary,Tokens: resp.Usage.CompletionTokens,LatestConvoMessageId: params.LatestConvoMessageId,LatestConvoMessageCreatedAt: params.LatestConvoMessageCreatedAt,diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.goindex cc0b909f..20cbab2a 100644--- a/app/server/types/active_plan.go+++ b/app/server/types/active_plan.go@@ -224,13 +224,21 @@ func (ap *ActivePlan) Stream(msg shared.StreamMessage) {ap.streamMessageBuffer = []shared.StreamMessage{}ap.streamMu.Unlock()+ log.Println("Flushing buffered messages before finishing")ap.Stream(shared.StreamMessage{Type: shared.StreamMessageMulti,StreamMessages: bufferToFlush,})+ log.Println("Finished flushing buffered messages, waiting 50ms before sending finish message")+ time.Sleep(50 * time.Millisecond)+ log.Println("Sending finish message")+ ap.Stream(msg) // send the finish message+ // wait for the finish message to be sent then send the done signal+ log.Println("Waiting 50ms before sending done signal")time.Sleep(50 * time.Millisecond)- ap.Stream(msg) // Resend the finish message+ log.Println("Sending done signal")+ ap.StreamDoneCh <- nilreturn}@@ -243,7 +251,9 @@ func (ap *ActivePlan) Stream(msg shared.StreamMessage) {ap.streamMu.Unlock()if msg.Type == shared.StreamMessageFinished {- time.Sleep(100 * time.Millisecond)+ log.Println("Waiting 50ms before sending done signal")+ time.Sleep(50 * time.Millisecond)+ log.Println("Sending done signal")ap.StreamDoneCh <- nil}}diff --git a/app/server/types/reply.go b/app/server/types/reply.goindex 4b83f509..76a4469a 100644--- a/app/server/types/reply.go+++ b/app/server/types/reply.go@@ -1,9 +1,7 @@package typesimport (- "log""os"- "strconv""strings")@@ -49,7 +47,7 @@ func NewReplyParser() *ReplyParser {}func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {- log.Println("Adding chunk:", strconv.Quote(chunk)) // Logging the chunk that's being processed+ // log.Println("Adding chunk:", strconv.Quote(chunk)) // Logging the chunk that's being processedhasNewLine := falsenextChunk := ""@@ -66,7 +64,7 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {}if chunk == "\n" {- log.Println("Chunk is \\n, adding new line")+ // log.Println("Chunk is \\n, adding new line")r.lines = append(r.lines, "")hasNewLine = truer.lineIndex++@@ -124,7 +122,7 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {}prevFullLine := r.lines[r.lineIndex-1]- log.Println("Previous full line:", strconv.Quote(prevFullLine)) // Logging the full line that's being checked+ // log.Println("Previous full line:", strconv.Quote(prevFullLine)) // Logging the full line that's being checkedprevFullLineTrimmed := strings.TrimSpace(prevFullLine)@@ -160,7 +158,7 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {}if r.currentFilePath == "" {- log.Println("Current file path is empty--checking for possible file path...")+ // log.Println("Current file path is empty--checking for possible file path...")var gotPath stringif lineMaybeHasFilePath(prevFullLineTrimmed) {@@ -178,7 +176,7 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {}}} else {- log.Println("Current file path is not empty--adding to current file...")+ // log.Println("Current file path is not empty--adding to current file...")if strings.HasPrefix(prevFullLineTrimmed, "```") {// log.Println("Found closing ticks--adding file to files and resetting current file...")r.files = append(r.files, r.currentFilePath)diff --git a/app/server/types/reply_test.go b/app/server/types/reply_test.goindex 478de555..d2a753dc 100644--- a/app/server/types/reply_test.go+++ b/app/server/types/reply_test.go@@ -60,6 +60,13 @@ var examples = []TestExample{"file_map/map.go": 239,},},+ {+ N: 8,+ TokensByFilePath: map[string]int{+ "Makefile": 100,+ "_apply.sh": 100,+ },+ },}func TestReplyTokenCounter(t *testing.T) {@@ -113,6 +120,11 @@ func TestReplyTokenCounter(t *testing.T) {}}+ // fmt.Println("file contents:")+ // spew.Dump(fileContents)++ // t.Errorf("output")+// if totalCounted != totalTokens {// t.Errorf("Expected %d tokens, got %d", totalTokens, totalCounted)// }diff --git a/app/server/types/reply_test_examples/8.md b/app/server/types/reply_test_examples/8.mdnew file mode 100644index 00000000..ace48b14--- /dev/null+++ b/app/server/types/reply_test_examples/8.md@@ -0,0 +1,61 @@+**Creating the project structure and Makefile**++First, I'll create a basic project structure with a Makefile that will compile our game.++- Makefile:+```makefile+CC = gcc+CFLAGS = -Wall -Wextra -I./include+LDFLAGS = -framework OpenGL -framework GLUT+SRC_DIR = src+OBJ_DIR = obj+BIN_DIR = bin++SRCS = $(wildcard $(SRC_DIR)/*.c)+OBJS = $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)+EXECUTABLE = $(BIN_DIR)/pong++.PHONY: all clean directories++all: directories $(EXECUTABLE)++directories:+ @mkdir -p $(OBJ_DIR) $(BIN_DIR)++$(EXECUTABLE): $(OBJS)+ $(CC) $(OBJS) -o $@ $(LDFLAGS)++$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c+ $(CC) $(CFLAGS) -c $< -o $@++clean:+ rm -rf $(OBJ_DIR) $(BIN_DIR)+```++- _apply.sh:+```bash+# Create necessary directories if they don't exist+mkdir -p src include obj bin++# Check if Homebrew is installed+if ! command -v brew &> /dev/null; then+ echo "Homebrew is not installed. Please install Homebrew first."+ exit 1+fi++# Install GLUT if not already installed+if ! brew list | grep -q freeglut; then+ echo "Installing GLUT..."+ brew install freeglut+fi++# Compile the project+make clean && make++# Run the game+./bin/pong+```++**Creating the project structure and Makefile** has been completed.++Next, I'll implement the header files for game structures and constants.\ No newline at end of filediff --git a/app/shared/req_res.go b/app/shared/req_res.goindex 22211a3a..e75d4a1d 100644--- a/app/shared/req_res.go+++ b/app/shared/req_res.go@@ -121,8 +121,11 @@ type TellPlanRequest struct {AutoContinue bool `json:"autoContinue"`IsUserContinue bool `json:"isUserContinue"`IsUserDebug bool `json:"isUserDebug"`+ IsApplyDebug bool `json:"isApplyDebug"`IsChatOnly bool `json:"isChatOnly"`AutoContext bool `json:"autoContext"`+ ExecEnabled bool `json:"execEnabled"`+ OsDetails string `json:"osDetails"`ApiKey string `json:"apiKey"` // deprecatedEndpoint string `json:"endpoint"` // deprecatedApiKeys map[string]string `json:"apiKeys"`@@ -211,7 +214,6 @@ type GetContextBodyResponse struct {type UpdateContextRequest map[string]*UpdateContextParams-type UpdateContextResponse = LoadContextResponsetype DeleteContextRequest struct {