Skip to content

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

feat: add full auto mode and improve interrupt handling

Dane Schneider3a77543bed68
diff --git a/app/cli/cmd/apply.go b/app/cli/cmd/apply.go
index 7d38289d..445bd876 100644
--- a/app/cli/cmd/apply.go
+++ b/app/cli/cmd/apply.go
@@ -1,11 +1,13 @@
package cmd
import (
+ "plandex-cli/api"
"plandex-cli/auth"
"plandex-cli/lib"
"plandex-cli/plan_exec"
"plandex-cli/term"
"plandex-cli/types"
+ shared "plandex-shared"
"github.com/spf13/cobra"
)
@@ -16,6 +18,8 @@ func init() {
initApplyFlags(applyCmd, false)
initExecScriptFlags(applyCmd)
RootCmd.AddCommand(applyCmd)
+
+ applyCmd.Flags().BoolVar(&fullAuto, "full", false, "Apply the plan and debug in full auto mode")
}
var applyCmd = &cobra.Command{
@@ -28,7 +32,22 @@ var applyCmd = &cobra.Command{
func apply(cmd *cobra.Command, args []string) {
auth.MustResolveAuthWithOrg()
lib.MustResolveProject()
- mustSetPlanExecFlags(cmd)
+
+ var config *shared.PlanConfig
+ if fullAuto {
+ term.StartSpinner("")
+ var apiErr *shared.ApiError
+ config, apiErr = api.Client.GetPlanConfig(lib.CurrentPlanId)
+ if apiErr != nil {
+ term.OutputErrorAndExit("Error getting plan config: %v", apiErr)
+ }
+ _, updatedConfig, printFn := resolveAutoModeSilent(config)
+ config = updatedConfig
+ term.StopSpinner()
+ printFn()
+ }
+
+ mustSetPlanExecFlagsWithConfig(cmd, config)
if lib.CurrentPlanId == "" {
term.OutputNoCurrentPlanErrorAndExit()
diff --git a/app/cli/cmd/debug.go b/app/cli/cmd/debug.go
index 69da71be..3914809b 100644
--- a/app/cli/cmd/debug.go
+++ b/app/cli/cmd/debug.go
@@ -1,9 +1,13 @@
package cmd
import (
+ "bufio"
+ "context"
"fmt"
+ "log"
"os"
"os/exec"
+ "os/signal"
"plandex-cli/auth"
"plandex-cli/lib"
"plandex-cli/plan_exec"
@@ -11,6 +15,10 @@ import (
"plandex-cli/types"
"strconv"
"strings"
+ "sync"
+ "sync/atomic"
+ "syscall"
+ "time"
shared "plandex-shared"
@@ -73,27 +81,120 @@ func doDebug(cmd *cobra.Command, args []string) {
// Execute command and handle retries
for attempt := 0; attempt < tries; attempt++ {
- term.StartSpinner("")
// Use shell to handle operators like && and |
- execCmd := exec.Command("sh", "-c", cmdStr)
+ shellCmdStr := "set -euo pipefail; " + cmdStr
+ execCmd := exec.Command("sh", "-c", shellCmdStr)
execCmd.Dir = cwd
execCmd.Env = os.Environ()
+ execCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
- output, err := execCmd.CombinedOutput()
+ pipe, err := execCmd.StdoutPipe()
+ if err != nil {
+ term.StopSpinner()
+ term.OutputErrorAndExit("Failed to create pipe: %v", err)
+ }
+ execCmd.Stderr = execCmd.Stdout
+
+ if err := execCmd.Start(); err != nil {
+ term.StopSpinner()
+ term.OutputErrorAndExit("Failed to start command: %v", err)
+ }
+
+ ctx, cancel := context.WithCancel(context.Background())
+ var interrupted atomic.Bool
+ var interruptHandled atomic.Bool
+ var interruptWG sync.WaitGroup
+
+ sigChan := make(chan os.Signal, 1)
+ signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
+
+ interruptWG.Add(1)
+ go func() {
+ defer interruptWG.Done()
+ for {
+ select {
+ case <-sigChan:
+ if interruptHandled.CompareAndSwap(false, true) {
+ fmt.Println()
+ color.New(term.ColorHiYellow, color.Bold).Println("\nπŸ‘‰ Caught interrupt. Exiting gracefully...")
+ fmt.Println()
+
+ interrupted.Store(true)
+
+ if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGINT); err != nil {
+ log.Printf("Failed to send SIGINT to process group: %v", err)
+ }
+
+ select {
+ case <-time.After(2 * time.Second):
+ fmt.Println()
+ color.New(term.ColorHiYellow, color.Bold).Println("πŸ‘‰ Commands didn't exit after 2 seconds. Sending SIGKILL.")
+ fmt.Println()
+ if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGKILL); err != nil {
+ log.Printf("Failed to send SIGKILL to process group: %v", err)
+ }
+ case <-ctx.Done():
+ return
+ }
+ }
+ case <-ctx.Done():
+ return
+ }
+ }
+ }()
+
+ var outputBuilder strings.Builder
+ scanner := bufio.NewScanner(pipe)
+ go func() {
+ for scanner.Scan() {
+ line := scanner.Text()
+ fmt.Println(line)
+ outputBuilder.WriteString(line + "\n")
+ }
+ }()
+
+ waitErr := execCmd.Wait()
+
+ cancel()
+ interruptWG.Wait()
+ signal.Stop(sigChan)
+ close(sigChan)
+
+ if scanErr := scanner.Err(); scanErr != nil {
+ log.Printf("⚠️ Scanner error reading subprocess output: %v", scanErr)
+ }
term.StopSpinner()
- outputStr := string(output)
- if outputStr == "" && err != nil {
- // If no output but error occurred, include error in output
- outputStr = err.Error()
+ outputStr := outputBuilder.String()
+ if outputStr == "" && waitErr != nil {
+ outputStr = waitErr.Error()
}
if outputStr != "" {
fmt.Println(outputStr)
}
- if err == nil {
+ didSucceed := waitErr == nil
+
+ if interrupted.Load() {
+ fmt.Println()
+ color.New(term.ColorHiYellow, color.Bold).Println("⚠️ Execution interrupted")
+
+ res, canceled, err := term.ConfirmYesNoCancel("Did the command succeed?")
+
+ if err != nil {
+ term.OutputErrorAndExit("Failed to get confirmation user input: %s", err)
+ }
+
+ didSucceed = res
+
+ if canceled {
+ os.Exit(0)
+ }
+ }
+
+ if didSucceed {
if attempt == 0 {
fmt.Printf("βœ… Command %s succeeded on first try\n", color.New(color.Bold, term.ColorHiCyan).Sprintf(cmdStr))
} else {
@@ -112,14 +213,14 @@ func doDebug(cmd *cobra.Command, args []string) {
}
// Prepare prompt for TellPlan
- exitErr, ok := err.(*exec.ExitError)
+ exitErr, ok := waitErr.(*exec.ExitError)
status := -1
if ok {
status = exitErr.ExitCode()
}
prompt := fmt.Sprintf("'%s' failed with exit status %d. Output:\n\n%s\n\n--\n\n",
- strings.Join(cmdArgs, " "), status, string(output))
+ strings.Join(cmdArgs, " "), status, outputStr)
tellFlags := types.TellFlags{
AutoContext: tellAutoContext,
diff --git a/app/cli/cmd/plan_exec_helpers.go b/app/cli/cmd/plan_exec_helpers.go
index 33bccf58..faf509ce 100644
--- a/app/cli/cmd/plan_exec_helpers.go
+++ b/app/cli/cmd/plan_exec_helpers.go
@@ -146,13 +146,20 @@ func validatePlanExecFlags() {
}
func mustSetPlanExecFlags(cmd *cobra.Command) {
+ mustSetPlanExecFlagsWithConfig(cmd, nil)
+}
+
+func mustSetPlanExecFlagsWithConfig(cmd *cobra.Command, config *shared.PlanConfig) {
if lib.CurrentPlanId == "" {
term.OutputNoCurrentPlanErrorAndExit()
}
- config, apiErr := api.Client.GetPlanConfig(lib.CurrentPlanId)
- if apiErr != nil {
- term.OutputErrorAndExit("Error getting plan config: %v", apiErr)
+ if config == nil {
+ var apiErr *shared.ApiError
+ config, apiErr = api.Client.GetPlanConfig(lib.CurrentPlanId)
+ if apiErr != nil {
+ term.OutputErrorAndExit("Error getting plan config: %v", apiErr)
+ }
}
// Set flag vars from config when flags aren't explicitly set
diff --git a/app/cli/plan_exec/action_menu.go b/app/cli/plan_exec/action_menu.go
index 318201f5..3061927b 100644
--- a/app/cli/plan_exec/action_menu.go
+++ b/app/cli/plan_exec/action_menu.go
@@ -228,13 +228,14 @@ func handleHotkeyOption(option hotkeyOption, diffs []string, params ExecParams)
}
fmt.Println()
} else if option.char == "a" {
+ fmt.Print("(a)")
fmt.Println()
_, err := lib.ExecPlandexCommand([]string{"apply"})
if err != nil {
fmt.Printf("\nError applying changes: %v\n", err)
}
fmt.Println()
- exitUnlessDiffs()
+ os.Exit(0)
} else if option.char == "r" {
fmt.Println()
_, err := lib.ExecPlandexCommand([]string{"reject"})
@@ -244,23 +245,20 @@ func handleHotkeyOption(option hotkeyOption, diffs []string, params ExecParams)
fmt.Println()
exitUnlessDiffs()
} else if option.char == "f" {
+ fmt.Print("(f)")
+ fmt.Println()
+
+ color.New(term.ColorHiYellow, color.Bold).Println("⚠️ Full auto mode allows automatic apply, execution, and multiple rounds of debugging without review.")
fmt.Println()
- _, err := lib.ExecPlandexCommand([]string{"apply", "--auto"})
+
+ _, err := lib.ExecPlandexCommand([]string{"apply", "--full"})
if err != nil {
fmt.Printf("\nError applying changes: %v\n", err)
}
fmt.Println()
- exitUnlessDiffs()
+ os.Exit(0)
} else if option.char == "q" || option.key == keyboard.KeyEnter {
os.Exit(0)
- // } else if option.char == "f" {
- // if term.IsRepl {
- // color.New(color.Bold).Println("Write a prompt πŸ‘‡")
- // os.Exit(0)
- // } else {
- // term.PrintCmds("", "tell", "chat")
- // os.Exit(0)
- // }
} else {
fmt.Println("\nInvalid hotkey")
}
diff --git a/app/cli/plan_exec/apply_exec.go b/app/cli/plan_exec/apply_exec.go
index c42a9c5d..48c41abb 100644
--- a/app/cli/plan_exec/apply_exec.go
+++ b/app/cli/plan_exec/apply_exec.go
@@ -73,30 +73,34 @@ func getOnApplyExecFail(applyFlags types.ApplyFlags, tellFlags types.TellFlags,
term.OutputErrorAndExit("failed to get plan config: %s", apiErr)
}
- config.SetAutoMode(shared.AutoModeFull)
- apiErr = api.Client.UpdatePlanConfig(lib.CurrentPlanId, shared.UpdatePlanConfigRequest{
- Config: config,
- })
-
- if apiErr != nil {
- term.OutputErrorAndExit("failed to update plan config: %s", apiErr)
+ if config.AutoMode != shared.AutoModeFull {
+ config.SetAutoMode(shared.AutoModeFull)
+ apiErr = api.Client.UpdatePlanConfig(lib.CurrentPlanId, shared.UpdatePlanConfigRequest{
+ Config: config,
+ })
+
+ if apiErr != nil {
+ term.OutputErrorAndExit("failed to update plan config: %s", apiErr)
+ }
+
+ applyFlags.AutoCommit = true
+ applyFlags.AutoConfirm = true
+ applyFlags.AutoExec = true
+ applyFlags.AutoDebug = config.AutoDebugTries
+
+ tellFlags.AutoApply = true
+ tellFlags.AutoContext = true
+ tellFlags.ExecEnabled = true
+ tellFlags.SmartContext = true
+
+ term.StopSpinner()
+ fmt.Println()
+ fmt.Println("βœ… Full auto mode enabled")
+ fmt.Println()
+ } else {
+ term.StopSpinner()
}
- applyFlags.AutoCommit = true
- applyFlags.AutoConfirm = true
- applyFlags.AutoExec = true
- applyFlags.AutoDebug = config.AutoDebugTries
-
- tellFlags.AutoApply = true
- tellFlags.AutoContext = true
- tellFlags.ExecEnabled = true
- tellFlags.SmartContext = true
-
- term.StopSpinner()
- fmt.Println()
- fmt.Println("βœ… Full auto mode enabled")
- fmt.Println()
-
case RollbackChangesAndExit:
if toRollback != nil {
lib.Rollback(toRollback, true)