Skip to content

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

structured edits algorithm improvements, fixes, tests. structured edits prompt example additions. context loading prompt improvements. increase db pool size.

Dane Schneider71443cd82bde
diff --git a/app/cli/cmd/continue.go b/app/cli/cmd/continue.go
index f95dc85f..f32f0aa4 100644
--- a/app/cli/cmd/continue.go
+++ b/app/cli/cmd/continue.go
@@ -27,6 +27,7 @@ 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(&tellAutoContext, "auto-context", false, "Load and manage context automatically")
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)")
@@ -52,7 +53,7 @@ func doContinue(cmd *cobra.Command, args []string) {
CurrentBranch: lib.CurrentBranch,
ApiKeys: apiKeys,
CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
- return lib.CheckOutdatedContextWithOutput(false, autoConfirm || tellAutoApply, maybeContexts)
+ return lib.CheckOutdatedContextWithOutput(false, autoConfirm || tellAutoApply || tellAutoContext, maybeContexts)
},
}, "", plan_exec.TellFlags{
TellBg: tellBg,
@@ -60,6 +61,7 @@ func doContinue(cmd *cobra.Command, args []string) {
TellNoBuild: tellNoBuild,
IsUserContinue: true,
ExecEnabled: !noExec,
+ AutoContext: tellAutoContext,
})
if tellAutoApply {
diff --git a/app/cli/main.go b/app/cli/main.go
index 40ee3ef6..ef3b87cd 100644
--- a/app/cli/main.go
+++ b/app/cli/main.go
@@ -52,6 +52,7 @@ func init() {
// Set the output of the logger to the file
log.SetOutput(file)
+ log.SetFlags(log.LstdFlags | log.Lmicroseconds)
// log.Println("Starting Plandex - logging initialized")
}
diff --git a/app/server/db/db.go b/app/server/db/db.go
index 66f8c647..fe50d2d9 100644
--- a/app/server/db/db.go
+++ b/app/server/db/db.go
@@ -44,11 +44,11 @@ func Connect() error {
log.Println("connected to database")
if os.Getenv("GOENV") == "production" {
- Conn.SetMaxOpenConns(30) // Allow up to 30 concurrent connections per task
- Conn.SetMaxIdleConns(30) // Keep up to 30 idle connections for reuse
+ Conn.SetMaxOpenConns(100) // Allow up to 100 concurrent connections per task
+ Conn.SetMaxIdleConns(100) // Keep up to 100 idle connections for reuse
} else {
- Conn.SetMaxOpenConns(10) // Allow up to 10 concurrent connections per task
- Conn.SetMaxIdleConns(10) // Keep up to 10 idle connections for reuse
+ Conn.SetMaxOpenConns(20) // Allow up to 20 concurrent connections per task
+ Conn.SetMaxIdleConns(20) // Keep up to 20 idle connections for reuse
}
_, err = Conn.Exec("SET TIMEZONE='UTC';")
diff --git a/app/server/go.mod b/app/server/go.mod
index 836308a5..ff0c4433 100644
--- a/app/server/go.mod
+++ b/app/server/go.mod
@@ -8,7 +8,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/pkg/errors v0.9.1
github.com/plandex/plandex/shared v0.0.0-00010101000000-000000000000
- github.com/sashabaranov/go-openai v1.35.7
+ github.com/sashabaranov/go-openai v1.36.0
)
require (
diff --git a/app/server/go.sum b/app/server/go.sum
index b42caf1d..e63e013d 100644
--- a/app/server/go.sum
+++ b/app/server/go.sum
@@ -95,8 +95,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
-github.com/sashabaranov/go-openai v1.35.7 h1:icyrRbkYoKPa4rbO1WSInpJu3qDQrPEnsoJVZ6QymdI=
-github.com/sashabaranov/go-openai v1.35.7/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
+github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=
+github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=
diff --git a/app/server/model/plan/build_exec.go b/app/server/model/plan/build_exec.go
index 6d911486..1148e174 100644
--- a/app/server/model/plan/build_exec.go
+++ b/app/server/model/plan/build_exec.go
@@ -205,8 +205,8 @@ func (fileState *activeBuildStreamFileState) buildFile() {
var currentState string
currentPlanFile, fileInCurrentPlan := currentPlan.CurrentPlanFiles.Files[filePath]
- log.Println("plan files:")
- spew.Dump(currentPlan.CurrentPlanFiles.Files)
+ // log.Println("plan files:")
+ // spew.Dump(currentPlan.CurrentPlanFiles.Files)
if fileInCurrentPlan {
log.Printf("File %s found in current plan.\n", filePath)
diff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.go
index a1cc72b8..d3d143bf 100644
--- a/app/server/model/plan/build_structured_edits.go
+++ b/app/server/model/plan/build_structured_edits.go
@@ -110,6 +110,10 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
}
log.Println("buildStructuredEdits - calling model for references")
+ // log.Println("anchorsFileMessages:")
+ // for _, msg := range anchorsFileMessages {
+ // log.Println(msg.Content)
+ // }
modelReq := openai.ChatCompletionRequest{
Model: config.BaseModelConfig.ModelName,
@@ -300,7 +304,11 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
if !beginsWithRef &&
!strings.Contains(activeBuild.FileDescription, "overwrite the entire file") &&
- !strings.Contains(activeBuild.FileDescription, "the start of the file") {
+ !((strings.Contains(activeBuild.FileDescription, "replace code") ||
+ strings.Contains(activeBuild.FileDescription, "remove code")) &&
+ strings.Contains(activeBuild.FileDescription, "start of the file")) {
+
+ log.Println("buildStructuredEdits - adding ... existing code ... to start of file")
// structured edits handle normalization of comments, so just use // ... existing code ... here
proposedContentLines = append([]string{"// ... existing code ..."}, proposedContentLines...)
@@ -314,13 +322,21 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
if !endsWithRef &&
!strings.Contains(activeBuild.FileDescription, "overwrite the entire file") &&
- !strings.Contains(activeBuild.FileDescription, "the end of the file") {
+ !(strings.Contains(activeBuild.FileDescription, "replace code") ||
+ strings.Contains(activeBuild.FileDescription, "remove code")) &&
+ !strings.Contains(activeBuild.FileDescription, "end of the file") {
+
+ log.Println("buildStructuredEdits - adding ... existing code ... to end of file")
+
proposedContentLines = append(proposedContentLines, "// ... existing code ...")
references = append(references, syntax.Reference(len(proposedContentLines)))
}
proposedContent = strings.Join(proposedContentLines, "\n")
+ log.Println("buildStructuredEdits - proposed content:")
+ log.Println(proposedContent)
+
updatedFile, err := syntax.ApplyChanges(
activePlan.Ctx,
fileState.language,
diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.go
index 80324c39..aac4aab2 100644
--- a/app/server/model/plan/tell_exec.go
+++ b/app/server/model/plan/tell_exec.go
@@ -164,15 +164,15 @@ func execTellPlan(
}
}
- // if auto context is enabled, we only include maps and trees on the first iteration, which is the context-gathering step
+ // if auto context is enabled, we only include maps and trees on the first iteration, which is the context-gathering step, and the second iteration, which is the planning step
var (
includeMaps = true
includeTrees = true
)
- if req.AutoContext && iteration > 0 {
- includeMaps = false
- includeTrees = false
- }
+ // if req.AutoContext && iteration > 1 {
+ // includeMaps = false
+ // includeTrees = false
+ // }
modelContextText, modelContextTokens, err := lib.FormatModelContext(state.modelContext, includeMaps, includeTrees)
if err != nil {
@@ -204,6 +204,8 @@ func execTellPlan(
sysCreate += prompts.NoApplyScriptPrompt
}
+ // log.Println("sysCreate before context:\n", sysCreate)
+
systemMessageText := sysCreate + modelContextText
systemMessage := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
@@ -297,7 +299,7 @@ func execTellPlan(
var sysCreateTokens int
if req.AutoContext {
- if iteration == 0 {
+ if iteration == 0 && !req.IsUserContinue {
sysCreateTokens = prompts.AutoContextPreambleNumTokens
} else {
sysCreateTokens = prompts.SysCreateAutoContextNumTokens
@@ -463,6 +465,7 @@ func execTellPlan(
state.messages = append(state.messages, *promptMessage)
} else {
log.Println("Missing file response:", missingFileResponse, "setting replyParser")
+ log.Printf("Current reply content:\n%s\n", active.CurrentReplyContent)
state.replyParser.AddChunk(active.CurrentReplyContent, true)
res := state.replyParser.Read()
@@ -511,17 +514,48 @@ func execTellPlan(
})
if missingFileResponse == shared.RespondMissingFileChoiceSkip {
- res := state.replyParser.Read()
+ res := state.replyParser.FinishAndRead()
+ skipPrompt := prompts.GetSkipMissingFilePrompt(res.CurrentFilePath)
+ prompt := prompts.GetWrappedPrompt(skipPrompt, req.OsDetails, applyScriptSummary) + "\n\n" + skipPrompt // repetition of skip prompt to improve instruction following
+
+ skipPromptTokens, err := shared.GetNumTokens(skipPrompt)
+ if err != nil {
+ log.Printf("Error getting num tokens for skip prompt: %v\n", err)
+ active.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: "Error getting num tokens for skip prompt",
+ }
+ return
+ }
+
+ state.totalRequestTokens += skipPromptTokens
state.messages = append(state.messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
- Content: prompts.GetSkipMissingFilePrompt(res.CurrentFilePath),
+ Content: prompt,
})
} else {
+ missingPrompt := prompts.GetMissingFileContinueGeneratingPrompt(res.CurrentFilePath)
+ prompt := prompts.GetWrappedPrompt(missingPrompt, req.OsDetails, applyScriptSummary) + "\n\n" + missingPrompt // repetition of missing prompt to improve instruction following
+
+ promptTokens, err = shared.GetNumTokens(prompt)
+ if err != nil {
+ log.Printf("Error getting num tokens for missing file continue prompt: %v\n", err)
+ active.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: "Error getting num tokens for missing file continue prompt",
+ }
+ return
+ }
+
+ state.totalRequestTokens += promptTokens
+
state.messages = append(state.messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
- Content: prompts.MissingFileContinueGeneratingPrompt,
+ Content: prompt,
})
}
}
diff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.go
index 4335cf2e..9a69e0e7 100644
--- a/app/server/model/plan/tell_stream.go
+++ b/app/server/model/plan/tell_stream.go
@@ -530,6 +530,7 @@ mainLoop:
parserRes := replyParser.Read()
if !fileOpen && parserRes.CurrentFilePath != "" {
+ log.Printf("File open: %s\n", parserRes.CurrentFilePath)
fileOpen = true
}
@@ -576,10 +577,34 @@ mainLoop:
continue mainLoop
}
+ var previousReplyContent string
+ var trimmedContent string
+
UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {
ap.MissingFilePath = currentFile
+ previousReplyContent = ap.CurrentReplyContent
+ trimmedContent = replyParser.GetReplyForMissingFile()
+ ap.CurrentReplyContent = trimmedContent
})
+ log.Println("Previous reply content:")
+ log.Println(previousReplyContent)
+
+ log.Println("Trimmed content:")
+ log.Println(trimmedContent)
+
+ chunkToStream := getCroppedChunk(previousReplyContent+content, trimmedContent, content)
+
+ log.Printf("chunkToStream: %s\n", chunkToStream)
+
+ if chunkToStream != "" {
+ log.Printf("Streaming remaining chunk before missing file prompt: %s\n", chunkToStream)
+ active.Stream(shared.StreamMessage{
+ Type: shared.StreamMessageReply,
+ ReplyChunk: chunkToStream,
+ })
+ }
+
log.Printf("Prompting user for missing file: %s\n", currentFile)
active.Stream(shared.StreamMessage{
@@ -589,8 +614,8 @@ mainLoop:
})
log.Printf("Stopping stream for missing file: %s\n", currentFile)
-
- // log.Printf("Current reply content: %s\n", active.CurrentReplyContent)
+ log.Printf("Chunk content: %s\n", content)
+ log.Printf("Current reply content: %s\n", active.CurrentReplyContent)
// stop stream for now
active.CancelModelStreamFn()
@@ -617,6 +642,7 @@ mainLoop:
UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {
ap.MissingFilePath = ""
+ ap.CurrentReplyContent = replyParser.GetReplyForMissingFile()
})
log.Println("Continuing stream")
@@ -674,6 +700,9 @@ mainLoop:
if req.BuildMode == shared.BuildModeAuto {
log.Printf("Queuing build for %s\n", file)
+ log.Println("Content:")
+ log.Println(fileContents[i])
+
buildState := &activeBuildStreamState{
tellState: state,
clients: clients,
@@ -773,8 +802,12 @@ func (state *activeTellStreamState) checkAutoLoadContext() []string {
return nil
}
- // only load context on the first iteration
- if state.iteration > 0 {
+ if state.req.IsUserContinue {
+ return nil
+ }
+
+ // only load context on the first or second iteration of a non-continue prompt
+ if state.iteration > 1 {
return nil
}
@@ -910,3 +943,12 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, con
Msg: "Stream error: " + streamErr.Error(),
}
}
+
+func getCroppedChunk(uncropped, cropped, chunk string) string {
+ uncroppedIdx := strings.Index(uncropped, chunk)
+ if uncroppedIdx == -1 {
+ return ""
+ }
+ croppedChunk := cropped[uncroppedIdx:]
+ return croppedChunk
+}
diff --git a/app/server/model/prompts/create.go b/app/server/model/prompts/create.go
index b3f89417..4fdc26c2 100644
--- a/app/server/model/prompts/create.go
+++ b/app/server/model/prompts/create.go
@@ -2,6 +2,7 @@ package prompts
import (
"fmt"
+ "log"
"time"
"github.com/plandex/plandex/shared"
@@ -109,40 +110,57 @@ In response to the user's latest prompt, do the following:
- For each step in the plan, also note which files will be needed in context to complete the step. This MUST include *all* files that will be updated, but can also include other files that will be helpful, like examples of similar code, documentation, and so on. Be thorough and exhaustive in listing all files that are necessary or helpful to completing the plan effectively.
- - If any code similar to what the user is asking for exists in the project, include at least one example so that you can use it as a reference for how to implement the user's request in a way that is consistent with the existing code. If a user asks you to implement an API endpoint, for example, and you see that there is an existing endpoint in the project that is similar, include it in the context so that you can use it as a reference. Similarly, if a task requires implementing a database migration, and you see that there is an existing migration, include it in the context so that you can use it as a reference. If you are asked to implement a frontend or UI feature, and you see that there are existing UI components, pages, styles, or other related code, include some of it so that you can use it as a reference and keep a consistent style, both in terms of the code and the user experience.
-
- - If you'll be using any definitions from a file—calling a function or method, instantiating a class, accessing a variable, using a type, and so on—include that file in the context. Include the file even if it's not being modified. For example, if you are creating an object that uses a type defined in a 'types.ts' file, include the 'types.ts' file in the context even if you're not updating the 'types.ts' file. If you're calling a function from a 'utils.py' file, include the 'utils.py' file in the context even if you're not updating the 'utils.py' file. This will ensure you correctly call functions/methods, use types, use variables and constants, etc. so it is *critical* that you include all files necessary to make a good plan that is well-integrated with the existing code.
-
- - Include any files that the user has mentioned directly or indirectly in the prompt. If a user has mentioned files by name, path, by describing them, by referring to definitions or other code they contain, or by referring to them in any other way, include those files.
-
- - If you aren't sure whether a file will be helpful or not, but you think it might be, include it in the 'Load Context' list. It's better to load more context than you need than to miss an important or helpful file.
-
- For each step in the plan, note if any of the necessary files are *already* in context. If so, you MUST NOT load them again—they must be omitted from the 'Load Context' list.
- At the end of your response, list *all* of those files (which are *not* already in context) in this EXACT format:
- ### Load Context
- - ` + "`src/main.rs`" + `
- - ` + "`lib/term.go`" + `
+ ` + LoadContextFormatPrompt + `
- You MUST use the exact same format as shown directly above. First the '### Load Context' header, then a blank line, then the list of files, with a '-' and a space before each file path. List files individually—do not list directories. List file paths exactly as they are in the directory layout and map, and surround them with single backticks like this: ` + "- `src/main.rs`." + ` Each file path in the list MUST be on its own line. Use this EXACT format.
-
- After the 'Load Context' list, you MUST ALWAYS immediately *stop there* so these files can be loaded before you continue.
+ ` + ContextLoadingRules + `
+
+` + FileMapScanningRules + `
+
+` + ContextCompletionCriteria + `
+
+` + ContextVerificationSteps + `
- If instead you already have enough information from the directory layout, map, and current context to make a detailed plan or respond effectively to the user and so you won't need to load any additional context, then explicitly say "No context needs to be loaded." and continue on to the instructions below.
- Every response MUST end with either the 'Load Context' list in the exact format described above or the exact phrase "No context needs to be loaded." and nothing else. This MUST be the final text in your response.
-The 'Load Context' list MUST *ONLY* include files that are present in the directory layout or map and are not *already* in context. You MUST NOT include any files that are already in context or that do not exist in the directory layout or map. **Do NOT include files that need to be created**—only files that already exist.
-
Don't output multiple lists of the files to load in your response. There MUST only be one 'Load Context' list in your response and no other list of the files to load.
[END OF YOUR INSTRUCTIONS]
`
+const LoadContextFormatPrompt = `
+### Load Context
+- ` + "`src/main.rs`" + `
+- ` + "`lib/term.go`" + `
+
+You MUST use the exact same format as shown directly above. First the '### Load Context' header, then a blank line, then the list of files, with a '-' and a space before each file path. List files individually—do not list directories. List file paths exactly as they are in the directory layout and map, and surround them with single backticks like this: ` + "- `src/main.rs`." + ` Each file path in the list MUST be on its own line. Use this EXACT format.
+
+After the 'Load Context' list, you MUST ALWAYS immediately *stop there* so these files can be loaded before you continue.
+
+The 'Load Context' list MUST *ONLY* include files that are present in the directory layout or map and are not *already* in context. You MUST NOT include any files that are already in context or that do not exist in the directory layout or map. **Do NOT include files that need to be created**—only files that already exist.
+
+Again, *DO NOT* load files that are already in context. *DO NOT* load files that do not exist *right now* in the directory layout or map.
+
+If any code similar to what the user is asking for exists in the project, include at least one example so that you can use it as a reference for how to implement the user's request in a way that is consistent with the existing code. If a user asks you to implement an API endpoint, for example, and you see that there is an existing endpoint in the project that is similar, include it in the context so that you can use it as a reference. Similarly, if a task requires implementing a database migration, and you see that there is an existing migration, include it in the context so that you can use it as a reference. If you are asked to implement a frontend or UI feature, and you see that there are existing UI components, pages, styles, or other related code, include some of it so that you can use it as a reference and keep a consistent style, both in terms of the code and the user experience.
+
+If you'll be using any definitions from a file—calling a function or method, instantiating a class, accessing a variable, using a type, and so on—include that file in the context. Include the file even if it's not being modified. For example, if you are creating an object that uses a type defined in a 'types.ts' file, include the 'types.ts' file in the context even if you're not updating the 'types.ts' file. If you're calling a function from a 'utils.py' file, include the 'utils.py' file in the context even if you're not updating the 'utils.py' file. This will ensure you correctly call functions/methods, use types, use variables and constants, etc. so it is *critical* that you include all files necessary to make a good plan that is well-integrated with the existing code.
+
+Include any files that the user has mentioned directly or indirectly in the prompt. If a user has mentioned files by name, path, by describing them, by referring to definitions or other code they contain, or by referring to them in any other way, include those files.
+
+If you aren't sure whether a file will be helpful or not, but you think it might be, include it in the 'Load Context' list. It's better to load more context than you need than to miss an important or helpful file.
+`
+
var AutoContextPreambleNumTokens int
func GetCreatePrompt(params CreatePromptParams) string {
+ log.Println("GetCreatePrompt")
+ log.Println("AutoContext:", params.AutoContext)
+
prompt := Identity + ` A plan is a set of files with an attached context.
[YOUR INSTRUCTIONS:]
@@ -156,11 +174,11 @@ func GetCreatePrompt(params CreatePromptParams) string {
if params.AutoContext {
prompt += `
- 1. Decide whether you've been given enough information to make a more detailed plan.
- - Do your best with whatever information 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.
- a. If you really don't have enough information to make a plan:
+ 1. Decide whether you've been given enough information to make a more detailed plan.
+ - In terms of information from the user's prompt, do your best with whatever information 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.
+ a. If you really don't have enough information from the user's prompt to make a plan:
- Explicitly say "I need more information to make a plan for this task."
- - Ask the user for more information and stop there.
+ - Ask the user for more information and stop there.
`
} else {
prompt += `
@@ -199,9 +217,9 @@ func GetCreatePrompt(params CreatePromptParams) string {
`
if params.AutoContext {
- prompt += `
- - Since you are in 'auto-context mode', below the description of each subtask, you MUST include a comma-separated 'Uses:' list of the files that will be needed in context to complete each task. Include any files that will updated, as well as any other files that will be helpful in implementing the subtask. ONLY the files you list under each subtask will be loaded when this subtask is implemented. List files individually—do not list directories. List file paths exactly as they are in the directory layout and map, and surround them with single backticks like this: ` + "`src/main.rs`." + `
- - Since you now have the context you need loaded, use it to make a more detailed plan than the plan you made in your previous response. Be thorough in your planning.
+ prompt += `
+ - Since you now have the context you need loaded, use it to make a more detailed plan than the plan you made in your previous response. Be thorough in your planning.
+ ` + UsesPrompt + `
`
}
@@ -213,6 +231,7 @@ func GetCreatePrompt(params CreatePromptParams) string {
- 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.
- Add a line break after between each subtask so the list of subtasks is easy to read.
- Do NOT ask the user to confirm after you've made subtasks. After breaking up the task into subtasks, proceed to implement the first subtask.
+ - Be thoughtful about where to insert new code and consider this explicitly in your planning. Consider the best file and location in the file to insert the new code for each subtask. Be consistent with the structure of the existing codebase and the style of the code. Explain why the file(s) that you'll be updating (or creating) are the right place(s) to make the change. Keep consistent code organization in mind. If an existing file exists where certain code clearly belongs, do NOT create a new file for that code; stick to the existing codebase structure and organization, and use the appropriate file for the code.
## Code blocks and files
@@ -238,6 +257,8 @@ func GetCreatePrompt(params CreatePromptParams) string {
` + ChangeExplanationPrompt + `
+ Do NOT treat files that do not exist in context as files to be updated. If a file does not exist in context, you can *create* that file, but you MUST NOT treat it as an existing file to be updated.
+
For code in markdown blocks, always include the language name after the opening triple backticks.
If there are triple backticks within any file in context, they will be escaped with backslashes like this '` + "\\`\\`\\`" + `'. If you are outputting triple backticks in a code block, you MUST escape them in exactly the same way.
@@ -1314,45 +1335,304 @@ Either way, you MUST NOT leave out the "... existing code ..." comments for ANY
*
+When including code from the original file to that is not changing and is intended to be used as an *anchor* to locate the insertion point of the new code, you ABSOLUTELY MUST NOT EVER change the order of the code in the original file. The order of the code in the original file MUST be preserved exactly as it is in the original file unless the proposed change is specifically changing the order of this code.
+
+If you are making multiple changes to the same file in a single file block, you MUST adhere to the order of the original file as closely as possible.
+
+If the original file is:
+
+---
+func buck() {
+ console.log("buck")
+}
+
+func qux() {
+ console.log("qux")
+}
+
+func fooBar() {
+ console.log("fooBar")
+}
+
+func baz() {
+ console.log("baz")
+}
+
+func yup() {
+ console.log("yup")
+}
+---
+
+DO NOT output a file block like this to demonstrate that new code will be inserted between the 'fooBar' and 'baz' functions:
+
+---
+// ... existing code ...
+
+func baz() {
+ console.log("baz-updated")
+}
+
+// ... existing code ...
+
+func qux() {
+ console.log("qux-updated")
+}
+
+// ... existing code ...
+
+---
+
+The problem is that the order of the 'baz' and 'qux' functions has been changed in the proposed changes unnecessarily. Instead, do this:
+
+---
+// ... existing code ...
+
+func qux() {
+ console.log("qux-updated")
+}
+
+// ... existing code ...
+
+func baz() {
+ console.log("baz-updated")
+}
+
+// ... existing code ...
+---
+
+Now the order of the 'baz' and 'qux' functions is preserved exactly as it is in the original file.
+
+*
+
+Here are some important examples of INCORRECT vs CORRECT file updates:
+
+Example 1 - Adding a new route:
+
+❌ INCORRECT - Replacing instead of inserting:
+` + "```go" + `
+// ... existing code ...
+
+r.HandleFunc(prefix+"/api/users", handlers.ListUsersHandler).Methods("GET")
+
+r.HandleFunc(prefix+"/api/config", handlers.GetConfigHandler).Methods("GET")
+
+// ... existing code ...
+` + "```" + `
+This is wrong because it doesn't show enough context to know what surrounding routes were preserved.
+
+✅ CORRECT - Proper insertion with context:
+` + "```go" + `
+// ... existing code ...
+
+r.HandleFunc(prefix+"/api/users", handlers.ListUsersHandler).Methods("GET")
+r.HandleFunc(prefix+"/api/teams", handlers.ListTeamsHandler).Methods("GET")
+
+r.HandleFunc(prefix+"/api/config", handlers.GetConfigHandler).Methods("GET")
+
+r.HandleFunc(prefix+"/api/settings", handlers.GetSettingsHandler).Methods("GET")
+r.HandleFunc(prefix+"/api/status", handlers.GetStatusHandler).Methods("GET")
+
+// ... existing code ...
+` + "```" + `
+
+Example 2 - Adding a method to a class:
+
+❌ INCORRECT - Ambiguous insertion:
+` + "```go" + `
+class UserService {
+ // ... existing code ...
+
+ async createUser(data) {
+ // new method
+ }
+
+ // ... existing code ...
+}
+` + "```" + `
+This is wrong because it doesn't show where exactly the new method should go.
+
+✅ CORRECT - Clear insertion point:
+` + "```go" + `
+class UserService {
+ // ... existing code ...
+
+ async getUser(id) {
+ return await this.db.users.findOne(id)
+ }
+
+ async createUser(data) {
+ return await this.db.users.create(data)
+ }
+
+ async updateUser(id, data) {
+ return await this.db.users.update(id, data)
+ }
+
+ // ... existing code ...
+}
+` + "```" + `
+
+Example 3 - Adding a configuration section:
+
+❌ INCORRECT - Lost context:
+` + "```json" + `
+{
+ "database": {
+ "host": "localhost",
+ "port": 5432
+ },
+ "newFeature": {
+ "enabled": true,
+ "timeout": 30
+ }
+}
+` + "```" + `
+This is wrong because it dropped existing configuration sections.
+
+✅ CORRECT - Preserved context:
+` + "```json" + `
+{
+ // ... existing code ...
+
+ "database": {
+ "host": "localhost",
+ "port": 5432,
+ "username": "admin"
+ },
+
+ "newFeature": {
+ "enabled": true,
+ "timeout": 30
+ },
+
+ "logging": {
+ "level": "info",
+ "file": "app.log"
+ }
+
+ // ... existing code ...
+}
+` + "```" + `
+
+Key principles demonstrated in these examples:
+1. Always show the surrounding context that will be preserved
+2. Make insertion points unambiguous by showing adjacent code
+3. Never remove existing functionality
+4. Use "... existing code ..." comments properly to indicate preserved sections
+5. Show enough context to understand the code structure
+
+*
+
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.
`
const ChangeExplanationPrompt = `
-Prior to any file block that is *updating* an existing file in context, you MUST briefly explain the change in the following format:
+Prior to any file block that is *updating* an existing file in context, you MUST explain the change in the following format EXACTLY:
+---
**Updating ` + "`[file path]`:**" + ` I'll [action explanation].
+---
-'action explanation' can take one of the following forms:
-- 'add [new code description] between [specific code or structure in original file] and [specific code or structure in original file]'
-- 'add [new code description] between the start of the file and [specific code or structure in original file]'
-- 'add [new code description] between [specific code or structure in original file] and the end of the file'
+'action explanation' MUST ALWAYS take one of the following forms:
+- 'add [new code description] between [specific code or structure in original file] and [specific *adjacent* code or structure in original file]'
+- 'add [new code description] immediately after [specific code or structure in original file]'
+- 'add [new code description] immediately before [specific code or structure in original file]'
+- 'prepend [new code description] to the start of the file'
+- 'append [new code description] to the end of the file'
- 'overwrite the entire file with [new code description]'
-- 'replace code between [specific code or structure in original file] and [specific code or structure in original file] with [new code description]'
-- 'remove code between [specific code or structure in original file] and [specific code or structure in original file]'
+- 'replace code between [specific code or structure in original file] and [specific *adjacent* code or structure in original file] with [new code description]'
+- 'replace [specific code or structure in original file] with [new code description]'
+- 'remove code between [specific code or structure in original file] and [specific *adjacent* code or structure in original file]'
+- 'remove [specific code or structure in original file]'
You ABSOLUTELY MUST use one of the above formats exactly as described, and EVERY file block that updates an existing file in context MUST *ALWAYS* be preceded with an explanation of the change in this *exact* format. Use the EXACT wording as described above. DO NOT CHANGE THE FORMATTING OR WORDING IN ANY WAY!
-When referring to the start of the file, use the exact language 'the start of the file'.
+If you are inserting code between two existing code blocks, do NOT use the start or end of the file to describe the position of the change; instead, use the code or structure that is *immediately before* and *immediately after* the point where the new code will be inserted.
-When referring to the end of the file, use the exact language 'the end of the file'.
+When inserting or replacing code, the "specific code or structure" or "specific *adjacent* code or structure" must be the code or structure that is *immediately before* and *immediately after* the point where the new code will be inserted. These two code structures ABSOLUTELY MUST be *adjacent* in the original file. DO NOT insert code between two code structures that aren't *immediately adjacent* in the original file. You must be precise in pinpointing the exact location of the change.
Do NOT leave off any part of the explanation as described above. Do NOT output something like: 'I'll add the doRequest method to the class' or 'I'll add the types for making the api call'. These do NOT exactly match one of the above formats. Instead, you MUST output the full explanation as described above like:
+
- **Updating ` + "`server/api/client.go`**" + `: I'll add the ` + "`doRequest`" + ` method between the constructor method and the ` + "`getUser`" + ` method.
- **Updating ` + "`server/types/api.go`**" + `: I'll add the types for making the api call between the imports and the ` + "`init`" + ` method.
- **Updating ` + "`cli/cmd/update.go`**" + `: I'll overwrite the entire file with new code for the ` + "`update`" + ` CLI command.
- **Updating ` + "`server/db/user.go`**" + `: I'll add the ` + "`update`" + ` function between the ` + "`get`" + ` and the end of the file.
+- **Updating ` + "`server/db/user.go`**" + `: I'll append a new ` + "`update`" + ` function to the end of the file.
+- **Updating ` + "`server/db/user.go`**" + `: I'll prepend a new ` + "`update`" + ` function to the start of the file.
+- **Updating ` + "`server/db/user.go`**" + `: I'll add a new ` + "`update`" + ` function immediately after the ` + "`get`" + ` method.
+- **Updating ` + "`server/db/user.go`**" + `: I'll add a new ` + "`update`" + ` function immediately before the ` + "`getUser`" + ` method.
+- **Updating ` + "`server/db/user.go`**" + `: I'll remove the ` + "`getUser`" + ` method.
You ABSOLUTELY MUST use this template EXACTLY as described above. DO NOT CHANGE THE FORMATTING OR WORDING IN ANY WAY! DO NOT OMIT ANY PART OF THE EXPLANATION AS DESCRIBED ABOVE. AND ABSOLUTELY DO NOT EVEN THINK ABOUT LEAVING OUT THIS MESSAGE! It is EXTREMELY IMPORTANT that you include this message in every file block that updates an existing file.
-When creating a *new* file, do NOT include this explanation.
+When creating a *new* file, do NOT include this explanation. Include *one* explanation in this format per file block that *updates* an existing file. Do NOT include multiple explanations in the same file block.
+
+When describing two specific code structures in the original file that new code will be inserted between, the two code structures MUST be *immediately adjacent* in the original file. DO NOT insert code between two code structures that aren't *immediately adjacent* in the original file. For example, if the original file is:
+
+---
+package something
+
+type T struct {}
+
+func Add(t *T) {
+ t.doSomething()
+ t.doSomethingElse()
+ t.doSomethingElseAgain()
+ t.doSomethingOnceMore()
+}
+---
+
+DO NOT output an explanation like this:
+
+---
+**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call between the 'doSomethingElse' method and the 'doSomethingOnceMore' method.
+---
+
+The problem is the 'doSomethingElse' function call and the 'doSomethingOnceMore' method are not *immediately adjacent* in the original file. Instead, output an explanation like this:
+
+---
+**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call between the 'doSomethingElse' method and the 'doSomethingElseAgain' method.
+---
+
+The 'doSomethingElse' method and the 'doSomethingElseAgain' method are *immediately adjacent* in the original file, so the explanation is correct.
+
+You can also output an explanation like this:
+
+---
+**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call immediately after the 'doSomethingOnceMore' method.
+---
+
+The explanation MUST ALWAYS WITHOUT EXCPETION be immediately followed by the file block that updates the file in the EXACT format specified in section 2a of your instructions. DO NOT omit the file block label.
+
+Example:
+
+---
+**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call immediately after the 'doSomethingElse' method.
+
+- server/something/something.go:
+` + "```" + `
+// ... existing code ...
+
+func Add(t *T) {
+ // ... existing code ...
+ doSomethingElse()
+ anotherThing()
+ doSomethingElseAgain()
+ // ... existing code ...
+}
+` + "```" + `
*
-If a file is being *updated* and the above explanation does not indicate that the file is being *overwritten* or that the change is being inserted at the *start* of the file, then the file block ABSOLUTELY ALWAYS MUST begin with an "... existing code ..." comment to account for all the code before the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
+ALL code structures that are mentioned in the explanation MUST be included as *anchors* in the file block that updates the file. If you are inserting new code between [structure 1] and [structure 2], then you MUST include both [structure 1] and [structure 2] as anchors in the file block that updates the file. You do not need to include the full structures—use "... existing code ..." reference comments if necessary to avoid outputting full structures, but you MUST include the anchors to make it clear and unambiguous where the new code is being inserted. The same applies if you are replacing code between [structure 1] and [structure 2] or removing code between [structure 1] and [structure 2].
-If a file is being *updated* and the above explanation indicates that the file is being *overwritten* or that the change is being inserted at the *end* of the file, then the file block ABSOLUTELY ALWAYS MUST end with an "... existing code ..." comment to account for all the code after the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
+*
+
+If a file is being *updated* and the above explanation does not indicate that the file is being *overwritten* or that the change is being prepended to the *start* of the file, then the file block ABSOLUTELY ALWAYS MUST begin with an "... existing code ..." comment to account for all the code before the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
+
+If a file is being *updated* and the above explanation indicates that the file is being *overwritten* or that the change is being appended to the *end* of the file, then the file block ABSOLUTELY ALWAYS MUST end with an "... existing code ..." comment to account for all the code after the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
Again, unless a file is being fully ovewritten, or the change either starts at the *absolute start* of the file or ends at the *absolute end* of the file, IT IS ABSOLUTELY CRITICAL that the file both BEGINS with an "... existing code ..." comment and ENDS with an "... existing code ..." comment.
@@ -1634,3 +1914,106 @@ You MUST consider the plan complete if the only remaining tasks must be complete
`
var NoApplyScriptPromptNumTokens int
+
+// Not using currently, considering for use in smarter context loader in the future (load just what a subtask needs)
+const UsesPrompt = `
+- Since you are in 'auto-context mode', below the description of each subtask, you MUST include a comma-separated 'Uses:' list of the files that will be needed in context to complete each task. Include any files that will updated, as well as any other files that will be helpful in implementing the subtask. ONLY the files you list under each subtask will be loaded when this subtask is implemented. List files individually—do not list directories. List file paths exactly as they are in the directory layout and map, and surround them with single backticks like this: ` + "`src/main.rs`." + `
+`
+
+const ContextLoadingRules = `When loading context, you MUST follow these rules:
+
+1. Interface & Implementation Rule:
+ - When loading an implementation file, you MUST also load its interface file
+ - When loading a type file, you MUST also load related type definitions
+ Example: If loading 'handlers/users.go', you must also load 'types/user.go'
+
+2. Reference Implementation Rule:
+ - When implementing a feature similar to an existing one, you MUST load the existing feature's files as reference
+ - Look for files with similar patterns, names, or purposes
+ Example: If adding user settings, load existing settings-related files
+
+3. Command Chain Rule:
+ - When adding/modifying CLI commands, you MUST load:
+ * The root command file (root.go)
+ * Similar existing command files
+ * Any parent command files
+ Example: If adding 'config set', load root.go and similar 'set' commands
+
+4. API Client Chain Rule:
+ - When working with API clients, you MUST load:
+ * The API interface file
+ * The client implementation file
+ * The client setup file
+ * Related error handling files
+ Example: If updating API methods, load both types/api.go and api/methods.go
+
+5. Database Chain Rule:
+ - When working with database operations, you MUST load:
+ * Related model files
+ * Related helper files
+ * Similar existing DB operations
+ Example: If adding user settings table, load other settings-related DB files
+
+6. Utility Dependencies Rule:
+ - Examine the code you're writing for any utility function calls
+ - Load ALL files containing utilities you might need
+ Example: If using string formatting utilities, load the utils file
+
+Remember: It's better to load more context than you need than to miss an important file. If you're not sure whether a file will be helpful, include it.`
+
+const FileMapScanningRules = `When examining the codebase, you MUST:
+
+1. Look for naming patterns:
+ - Files with similar prefixes or suffixes
+ - Files in similar locations
+ Example: If working on 'user_config.go', look for other '*_config.go' files
+
+2. Look for feature groupings:
+ - Find all files related to similar features
+ - Look for files that work together
+ Example: If adding settings, find all existing settings-related files
+
+3. Follow file relationships:
+ - For each file you identify, check for:
+ * Its interface file
+ * Its test file
+ * Its helper files
+ * Related type definitions
+ Example: For 'api/methods.go', look for 'types/api.go', 'api/methods_test.go'
+
+4. Check common dependencies:
+ - Always check for related files in:
+ * /shared - for shared types
+ * /types - for interfaces
+ * /utils - for utilities
+ * /errors - for error types`
+
+const ContextCompletionCriteria = `Before finalizing your context loading, verify you have:
+
+1. ALL interface files for any implementations
+2. ALL type definitions needed
+3. ALL similar feature files for reference
+4. ALL utility files that might be needed
+5. ALL parent/child relationship files (like command hierarchies)
+
+You MUST explicitly state if you're missing any of these categories and load additional context if needed.`
+
+const ContextVerificationSteps = `After listing files to load, you MUST verify:
+
+1. Interface Coverage:
+ - For each implementation file, is its interface loaded?
+ - For each type use, is its definition loaded?
+
+2. Reference Coverage:
+ - For similar features, have all reference files been loaded?
+ - Are you missing any patterns to follow?
+
+3. Dependency Coverage:
+ - For each function/method call, is its definition loaded?
+ - For each type usage, is its definition loaded?
+
+4. Command Coverage:
+ - For CLI changes, is the command hierarchy loaded?
+ - Are similar command patterns loaded?
+
+If any of these are "no", you MUST load additional context.`
diff --git a/app/server/model/prompts/missing_file.go b/app/server/model/prompts/missing_file.go
index 9c987eac..599dacc1 100644
--- a/app/server/model/prompts/missing_file.go
+++ b/app/server/model/prompts/missing_file.go
@@ -6,4 +6,6 @@ func GetSkipMissingFilePrompt(path string) string {
return fmt.Sprintf(`You *must not* generate content for the file %s. Skip this file and continue with the plan according to the 'Your instructions' section if there are any remaining tasks or subtasks. Don't repeat any part of the previous message. If there are no remaining tasks or subtasks, stop there.`, path)
}
-const MissingFileContinueGeneratingPrompt = "Continue generating exactly where you left off in the previous message. Don't produce any other output before continuing or repeat any part of the previous message. Do *not* duplicate the last line of the previous response before continuing. Do *not* include triple backticks and a language name like '```python' or '```yaml' at the start of the response, since these have already been included in the previous message. Continue from where you left off seamlessly to generate the rest of the file block. You must include closing triple backticks at the end of the file block. When the file block is finished, continue with the plan according to the 'Your instructions' sections if there are any remaining tasks or subtasks. If there are no remaining tasks or subtasks, stop there."
+func GetMissingFileContinueGeneratingPrompt(path string) string {
+ return fmt.Sprintf("Continue generating the file '%s'. Continue EXACTLY where you left off in the previous message. Don't produce any other output before continuing or repeat any part of the previous message. Do *not* duplicate the last line of the previous response before continuing. Do *not* include triple backticks and a language name like '```python' or '```yaml' at the start of the response, since these have already been included in the previous message. Continue from where you left off seamlessly to generate the rest of the file block. You must include closing triple backticks at the end of the file block. When the file block is finished, continue with the plan according to the 'Your instructions' sections if there are any remaining tasks or subtasks. If there are no remaining tasks or subtasks, stop there. DO NOT UNDER ANY CIRCUMSTANCES INCLUDE THE FILE PATH OR THE OPENING TRIPLE BACKTICKS IN THE RESPONSE. DO NOT UNDER ANY CIRCUMSTANCES begin your response with *anything* except for the code that belongs in the '%s' file block.", path, path)
+}
diff --git a/app/server/syntax/structural_edits.go b/app/server/syntax/structural_edits.go
index bfd5544e..5deed44b 100644
--- a/app/server/syntax/structural_edits.go
+++ b/app/server/syntax/structural_edits.go
@@ -3,6 +3,7 @@ package syntax
import (
"context"
"fmt"
+ "sort"
"strings"
"github.com/davecgh/go-spew/spew"
@@ -20,7 +21,7 @@ type Anchor struct {
type TreeSitterSection []*tree_sitter.Node
-const verboseLogging = false
+const verboseLogging = true
func ApplyChanges(
ctx context.Context,
@@ -108,8 +109,19 @@ func ApplyChanges(
proposedNodesByLineIndex := pRes.nodesByLine
originalParentsByLineIndex := oRes.parentsByLine
- findNextAnchor := func(s string, pLineNum int, pNode *tree_sitter.Node, fromLine int) *Anchor {
- oLineNum, ok := anchorLines[pLineNum]
+ anchorMap := buildAnchorMap(
+ originalLines,
+ proposedLines,
+ originalNodesByLineIndex,
+ anchorLines,
+ )
+
+ if verboseLogging {
+ fmt.Printf("anchorMap:\n%v\n", spew.Sdump(anchorMap))
+ }
+
+ findAnchor := func(pLineNum int) *Anchor {
+ oLineNum, ok := anchorMap[pLineNum]
if ok {
if verboseLogging {
fmt.Printf("found anchor in anchorLines: oLineNum: %d, pLineNum: %d\n", oLineNum, pLineNum)
@@ -128,55 +140,10 @@ func ApplyChanges(
return anchor
} else {
if verboseLogging {
- fmt.Printf("no anchor found in anchorLines: pLineNum: %d\n", pLineNum)
+ fmt.Printf("no anchor found in anchorMap: pLineNum: %d\n", pLineNum)
}
}
- for idx, line := range originalLines {
- if idx < fromLine {
- continue
- }
-
- if strings.TrimSpace(line) == "" {
- continue
- }
-
- // if verboseLogging {
- // fmt.Printf("line: %s, idx: %d\n", line, idx)
- // }
-
- oNode := originalNodesByLineIndex[idx]
-
- if verboseLogging {
- // fmt.Println("node:")
- // fmt.Println(oNode.Type())
- // fmt.Println(oNode)
- // fmt.Println(oNode.Content(originalBytes))
- }
-
- // just using string matching for now since there's too much ambiguity in node-based matching
- stringMatch := line == s
- // nodeMatch := oNode != nil && oNode.IsNamed() && nodesMatch(oNode, pNode, originalBytes, proposedBytes)
-
- if stringMatch {
- var endLineNum int
- if oNode != nil {
- if verboseLogging {
- fmt.Printf("oNode.Type(): %s\n", oNode.Type())
- fmt.Printf("oNode.EndPoint().Row: %d\n", oNode.EndPoint().Row)
- // fmt.Printf("%v\n", oNode)
- // fmt.Printf("oNode.Content(originalBytes):\n%q\n", oNode.Content(originalBytes))
- }
- endLineNum = int(oNode.EndPoint().Row) + 1
- }
- if verboseLogging {
- fmt.Printf("found match: num: %d, endLineNum: %d\n", idx+1, endLineNum)
- // fmt.Println(oNode.Content(originalBytes))
- }
-
- return &Anchor{Open: idx + 1, Close: endLineNum}
- }
- }
return nil
}
@@ -213,6 +180,12 @@ func ApplyChanges(
foundAnyAnchor := false
setOLineNum := func(n int) {
+ if n < 1 {
+ n = 1
+ }
+ if n > len(originalLines) {
+ n = len(originalLines)
+ }
oLineNum = n
if verboseLogging {
fmt.Printf("setting oLineNum: %d\n", oLineNum)
@@ -270,6 +243,12 @@ func ApplyChanges(
var fullRef []string
if eof {
start := refStart - 1
+ if start < 0 {
+ start = 0
+ }
+ if start >= len(originalLines) {
+ start = len(originalLines) - 1
+ }
fullRef = originalLines[start:]
if verboseLogging {
fmt.Println("eof")
@@ -283,10 +262,16 @@ func ApplyChanges(
if start < 0 {
start = 0
}
+ if start >= len(originalLines) {
+ start = len(originalLines) - 1
+ }
end := oLineNum - 1
if end < 1 {
end = 0
}
+ if end >= len(originalLines) {
+ end = len(originalLines) - 1
+ }
// Add detailed diagnostic logging for invalid slice bounds
if start > end {
@@ -382,11 +367,16 @@ func ApplyChanges(
isRef := refsByLine[Reference(pLineNum)]
isRemoval := removalsByLine[Removal(pLineNum)]
+ nextPLineIsRef := refsByLine[Reference(pLineNum+1)]
if verboseLogging {
fmt.Printf("isRef: %v\n", isRef)
fmt.Printf("isRemoval: %v\n", isRemoval)
- fmt.Printf("oLineNum: %d\n", oLineNum)
+ fmt.Printf("nextPLineIsRef: %v\n", nextPLineIsRef)
+ fmt.Printf("oLineNum before: %d\n", oLineNum)
+ if oLineNum > 0 && oLineNum < len(originalLines) {
+ fmt.Printf("oLine before: %q\n", originalLines[oLineNum-1])
+ }
fmt.Printf("currentPNode set: %v\n", currentPNode != nil)
fmt.Printf("currentPNodeEndsAtIdx: %d\n", currentPNodeEndsAtIdx)
fmt.Printf("currentPNodeMatches: %v\n", currentPNodeMatches)
@@ -454,16 +444,42 @@ func ApplyChanges(
addNewPostRefBuffer()
+ if oLineNum == len(originalLines) {
+ break
+ }
+
continue
}
if !refOpen && lastLineMatched && !currentPNodeMatches {
if strings.TrimSpace(pLine) == "" {
- write(pLine, !finalLine)
+ if verboseLogging {
+ fmt.Printf("pLine is blank\n")
+ if oLineNum < len(originalLines) {
+ fmt.Printf("nextOLine: %q\n", originalLines[oLineNum])
+ }
+ }
+
+ nextOLineIsBlank := oLineNum > 0 && oLineNum < len(originalLines) && strings.TrimSpace(originalLines[oLineNum]) == ""
+
+ if verboseLogging {
+ fmt.Printf("nextPLineIsRef: %v\n", nextPLineIsRef)
+ fmt.Printf("nextOLineIsBlank: %v\n", nextOLineIsBlank)
+ }
+
+ if !nextPLineIsRef || nextOLineIsBlank {
+ write(pLine, !finalLine)
+ }
+
// Check if next line in original is also blank
- if oLineNum+1 < len(originalLines) && strings.TrimSpace(originalLines[oLineNum+1]) == "" {
+ if nextOLineIsBlank {
setOLineNum(oLineNum + 1)
}
+
+ if oLineNum == len(originalLines) {
+ break
+ }
+
continue
}
}
@@ -490,7 +506,7 @@ func ApplyChanges(
noMatchUntilStructureClose = ""
} else if noMatchUntilStructureClose != pLine && !(currentPNode != nil && !currentPNodeMatches) {
// find next line in original that matches
- anchor := findNextAnchor(pLine, pLineNum, pNode, oLineNum-1)
+ anchor := findAnchor(pLineNum)
if anchor != nil {
foundAnyAnchor = true
if verboseLogging {
@@ -602,6 +618,9 @@ func ApplyChanges(
}
+ if oLineNum == len(originalLines) {
+ break
+ }
}
if refOpen {
@@ -955,3 +974,208 @@ func (s TreeSitterSection) String(sourceLines []string, bytes []byte) string {
return result
}
+
+type AnchorMap = map[int]int
+
+func buildAnchorMap(
+ originalLines []string,
+ proposedLines []string,
+ originalNodesByLineIndex map[int]*tree_sitter.Node,
+ anchorLines map[int]int,
+) AnchorMap {
+ result := AnchorMap{}
+
+ if verboseLogging {
+ fmt.Printf("\n=== Building Anchor Map ===\n")
+ fmt.Printf("Initial anchorLines: %v\n", anchorLines)
+ }
+
+ // First pass: add explicit anchors
+ for pLineNum, oLineNum := range anchorLines {
+ if oNode := originalNodesByLineIndex[oLineNum-1]; oNode != nil {
+ result[pLineNum] = oLineNum
+ if verboseLogging {
+ fmt.Printf("Adding explicit anchor: proposed line %d -> original line %d\n", pLineNum, oLineNum)
+ }
+ }
+ }
+
+ var matchSection func(pStart, pEnd, oStart, oEnd int)
+ matchSection = func(pStart, pEnd, oStart, oEnd int) {
+ if pEnd <= pStart || oEnd <= oStart {
+ return
+ }
+
+ if verboseLogging {
+ fmt.Printf("\n--- Processing Section ---\n")
+ fmt.Printf("Proposed lines %d-%d, Original lines %d-%d\n", pStart, pEnd, oStart, oEnd)
+ }
+
+ // First find unique matches in this section
+ sectionOriginal := make(map[string][]int)
+ sectionProposed := make(map[string][]int)
+
+ // Build frequency maps for this section
+ for i := oStart; i < oEnd; i++ {
+ if content := strings.TrimSpace(originalLines[i]); content != "" {
+ sectionOriginal[content] = append(sectionOriginal[content], i)
+ }
+ }
+
+ for i := pStart; i < pEnd; i++ {
+ if content := strings.TrimSpace(proposedLines[i]); content != "" {
+ sectionProposed[content] = append(sectionProposed[content], i)
+ }
+ }
+
+ // Handle unique matches first
+ if verboseLogging {
+ fmt.Printf("\nProcessing unique matches...\n")
+ }
+ for content, pIndices := range sectionProposed {
+ if oIndices, exists := sectionOriginal[content]; exists && len(oIndices) == 1 && len(pIndices) == 1 {
+ pIdx, oIdx := pIndices[0], oIndices[0]
+ if oNode := originalNodesByLineIndex[oIdx]; oNode != nil {
+ result[pIdx+1] = oIdx + 1
+ if verboseLogging {
+ fmt.Printf("Found unique match: %q\n", content)
+ fmt.Printf("Mapping proposed line %d (%q) -> original line %d (%q)\n",
+ pIdx+1, proposedLines[pIdx], oIdx+1, originalLines[oIdx])
+ }
+ }
+ }
+ }
+
+ // Get ordered anchors to establish subsections
+ var orderedAnchors []struct {
+ pLine int
+ oLine int
+ }
+ for pLine := pStart; pLine < pEnd; pLine++ {
+ if anchor, exists := result[pLine+1]; exists {
+ orderedAnchors = append(orderedAnchors, struct {
+ pLine int
+ oLine int
+ }{pLine, anchor - 1})
+ }
+ }
+
+ if verboseLogging {
+ fmt.Printf("\nOrdered anchors in section: %v\n", orderedAnchors)
+ }
+
+ // Sort anchors by proposed line number
+ sort.Slice(orderedAnchors, func(i, j int) bool {
+ return orderedAnchors[i].pLine < orderedAnchors[j].pLine
+ })
+
+ // Process each subsection between anchors
+ lastPLine := pStart
+ lastOLine := oStart
+
+ for i := 0; i <= len(orderedAnchors); i++ {
+ var nextPLine, nextOLine int
+ if i < len(orderedAnchors) {
+ nextPLine = orderedAnchors[i].pLine
+ nextOLine = orderedAnchors[i].oLine
+ } else {
+ nextPLine = pEnd
+ nextOLine = oEnd
+ }
+
+ if verboseLogging {
+ fmt.Printf("\nProcessing subsection %d\n", i)
+ fmt.Printf("Proposed lines %d-%d, Original lines %d-%d\n", lastPLine, nextPLine, lastOLine, nextOLine)
+ }
+
+ // Handle duplicates in this subsection using outside-in matching
+ subSectionOriginal := make(map[string][]int)
+ subSectionProposed := make(map[string][]int)
+
+ // Build frequency maps for this subsection
+ for i := lastOLine; i < nextOLine; i++ {
+ if content := strings.TrimSpace(originalLines[i]); content != "" {
+ subSectionOriginal[content] = append(subSectionOriginal[content], i)
+ }
+ }
+
+ for i := lastPLine; i < nextPLine; i++ {
+ if content := strings.TrimSpace(proposedLines[i]); content != "" {
+ subSectionProposed[content] = append(subSectionProposed[content], i)
+ }
+ }
+
+ // Match duplicates from outside-in
+ for content, pIndices := range subSectionProposed {
+ oIndices, exists := subSectionOriginal[content]
+ if !exists {
+ if verboseLogging {
+ fmt.Printf("New code found: %q at proposed lines %v\n", content, pIndices)
+ }
+ continue // This is new code to be inserted
+ }
+
+ if verboseLogging {
+ fmt.Printf("\nMatching duplicates for content: %q\n", content)
+ fmt.Printf("Found in proposed lines: %v\n", pIndices)
+ fmt.Printf("Found in original lines: %v\n", oIndices)
+ }
+
+ // Filter oIndices to only include matches within subsection boundaries
+ var validOIndices []int
+ for _, idx := range oIndices {
+ if idx >= lastOLine && idx < nextOLine {
+ validOIndices = append(validOIndices, idx)
+ }
+ }
+ oIndices = validOIndices
+
+ // Match from outside in until we run out of original occurrences
+ matched := 0
+ for matched < len(oIndices) && matched*2 < len(pIndices) {
+ // Match first unmatched occurrence
+ if oNode := originalNodesByLineIndex[oIndices[matched]]; oNode != nil {
+ result[pIndices[matched]+1] = oIndices[matched] + 1
+ if verboseLogging {
+ fmt.Printf("Matched first occurrence: proposed line %d (%q) -> original line %d (%q)\n",
+ pIndices[matched]+1, proposedLines[pIndices[matched]],
+ oIndices[matched]+1, originalLines[oIndices[matched]])
+ }
+ }
+
+ // Match last unmatched occurrence if we have more to match
+ if matched*2+1 < len(pIndices) {
+ lastOrigIdx := oIndices[len(oIndices)-1-matched]
+ lastPropIdx := pIndices[len(pIndices)-1-matched]
+ if oNode := originalNodesByLineIndex[lastOrigIdx]; oNode != nil {
+ result[lastPropIdx+1] = lastOrigIdx + 1
+ if verboseLogging {
+ fmt.Printf("Matched last occurrence: proposed line %d (%q) -> original line %d (%q)\n",
+ lastPropIdx+1, proposedLines[lastPropIdx],
+ lastOrigIdx+1, originalLines[lastOrigIdx])
+ }
+ }
+ }
+ matched++
+ }
+ // Any remaining occurrences in pIndices are new code to be inserted
+ }
+
+ // Recursively process the next subsection
+ if i < len(orderedAnchors) {
+ lastPLine = nextPLine
+ lastOLine = nextOLine
+ }
+ }
+ }
+
+ // Start recursive matching with full file
+ matchSection(0, len(proposedLines), 0, len(originalLines))
+
+ if verboseLogging {
+ fmt.Printf("\n=== Final Anchor Map ===\n")
+ fmt.Printf("Result: %v\n", result)
+ }
+
+ return result
+}
diff --git a/app/server/syntax/structural_edits_test.go b/app/server/syntax/structural_edits_test.go
index 7c6e0c59..4bd784c4 100644
--- a/app/server/syntax/structural_edits_test.go
+++ b/app/server/syntax/structural_edits_test.go
@@ -1260,10 +1260,1021 @@ class MetricsService(
`,
parser: GetParserForLanguage("javascript"),
},
+
+ {
+ name: "clean up extraneous newlines",
+ original: `
+ func func1 () {
+ fmt.Println("func1")
+ }
+
+ func func2 () {
+ fmt.Println("log something")
+ fmt.Println("func2")
+ }
+
+ func func3 () {
+ fmt.Println("func3")
+
+ fmt.Println("func3")
+ }
+ `,
+ proposed: `
+ // ... existing code ...
+
+ func func2 () {
+ // ... existing code ...
+ }
+
+ func newFunc () {
+ console.log("newFunc")
+ }
+
+ func func3 () {
+
+ // ... existing code ...
+ `,
+ references: []Reference{
+ 2, 5, 14,
+ },
+ want: `
+ func func1 () {
+ fmt.Println("func1")
+ }
+
+ func func2 () {
+ fmt.Println("log something")
+ fmt.Println("func2")
+ }
+
+ func newFunc () {
+ console.log("newFunc")
+ }
+
+ func func3 () {
+ fmt.Println("func3")
+
+ fmt.Println("func3")
+ }
+ `,
+ parser: GetParserForLanguage("go"),
+ },
+
+ {
+ name: "slice error",
+ original: `package cmd
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "os"
+ "os/exec"
+ "plandex/api"
+ "plandex/auth"
+ "plandex/lib"
+ "plandex/plan_exec"
+ "plandex/term"
+ "strconv"
+ "strings"
+ "time"
+
+ "context"
+
+ "os/signal"
+ "syscall"
+
+ "github.com/plandex/plandex/shared"
+ "github.com/spf13/cobra"
+)
+
+const defaultEditor = "vim"
+const defaultAutoDebugTries = 5
+
+var autoConfirm bool
+
+// const defaultEditor = "nano"
+
+var tellPromptFile string
+var tellBg bool
+var tellStop bool
+var tellNoBuild bool
+var tellAutoApply bool
+var tellAutoContext bool
+var noExec bool
+var autoDebug int
+
+// tellCmd represents the prompt command
+var tellCmd = &cobra.Command{
+ Use: "tell [prompt]",
+ Aliases: []string{"t"},
+ Short: "Send a prompt for the current plan",
+ // Long: ` + "``" + `,
+ Args: cobra.RangeArgs(0, 1),
+ Run: doTell,
+}
+
+func init() {
+ RootCmd.AddCommand(tellCmd)
+
+ tellCmd.Flags().StringVarP(&tellPromptFile, "file", "f", "", "File containing prompt")
+ tellCmd.Flags().BoolVarP(&tellStop, "stop", "s", false, "Stop after a single reply")
+ tellCmd.Flags().BoolVarP(&tellNoBuild, "no-build", "n", false, "Don't build files")
+ tellCmd.Flags().BoolVar(&tellBg, "bg", false, "Execute autonomously in the background")
+
+ 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 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) {
+ validateTellFlags()
+
+ auth.MustResolveAuthWithOrg()
+ lib.MustResolveProject()
+
+ if lib.CurrentPlanId == "" {
+ term.OutputNoCurrentPlanErrorAndExit()
+ }
+
+ var apiKeys map[string]string
+ if !auth.Current.IntegratedModelsMode {
+ apiKeys = lib.MustVerifyApiKeys()
+ }
+
+ prompt := getTellPrompt(args)
+
+ if prompt == "" {
+ fmt.Println("🤷‍♂️ No prompt to send")
+ return
+ }
+
+ plan_exec.TellPlan(plan_exec.ExecParams{
+ CurrentPlanId: lib.CurrentPlanId,
+ CurrentBranch: lib.CurrentBranch,
+ ApiKeys: apiKeys,
+ CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
+ return lib.CheckOutdatedContextWithOutput(false, autoConfirm || tellAutoApply || tellAutoContext, maybeContexts)
+ },
+ }, prompt, plan_exec.TellFlags{
+ TellBg: tellBg,
+ TellStop: tellStop,
+ TellNoBuild: tellNoBuild,
+ AutoContext: tellAutoContext,
+ ExecEnabled: !noExec,
+ })
+
+ if tellAutoApply {
+ 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),
+ )
+ }
+}
+
+func getTellPrompt(args []string) string {
+ var prompt string
+ var pipedData string
+
+ if len(args) > 0 {
+ prompt = args[0]
+ } else if tellPromptFile != "" {
+ bytes, err := os.ReadFile(tellPromptFile)
+ if err != nil {
+ term.OutputErrorAndExit("Error reading prompt file: %v", err)
+ }
+ prompt = string(bytes)
+ }
+
+ // Check if there's piped input
+ fileInfo, err := os.Stdin.Stat()
+ if err != nil {
+ term.OutputErrorAndExit("Failed to stat stdin: %v", err)
+ }
+
+ if fileInfo.Mode()&os.ModeNamedPipe != 0 {
+ reader := bufio.NewReader(os.Stdin)
+ pipedDataBytes, err := io.ReadAll(reader)
+ if err != nil {
+ term.OutputErrorAndExit("Failed to read piped data: %v", err)
+ }
+ pipedData = string(pipedDataBytes)
+ }
+
+ if prompt == "" && pipedData == "" {
+ prompt = getEditorPrompt()
+ } else if pipedData != "" {
+ if prompt != "" {
+ prompt = fmt.Sprintf("%s\n\n---\n\n%s", prompt, pipedData)
+ } else {
+ prompt = pipedData
+ }
+ }
+
+ return prompt
+}
+
+func prepareEditorCommand(editor string, filename string) *exec.Cmd {
+ switch editor {
+ case "vim":
+ return exec.Command(editor, "+normal G$", "+startinsert!", filename)
+ case "nano":
+ return exec.Command(editor, "+99999999", filename)
+ default:
+ return exec.Command(editor, filename)
+ }
+}
+
+func getEditorInstructions(editor string) string {
+ return "👉 Write your prompt below, then save and exit to send it to Plandex.\n• To save and exit, press ESC, then type :wq! and press ENTER.\n• To exit without saving, press ESC, then type :q! and press ENTER.\n\n\n"
+}
+
+func getEditorPrompt() string {
+ editor := os.Getenv("EDITOR")
+ if editor == "" {
+ editor = os.Getenv("VISUAL")
+ if editor == "" {
+ editor = defaultEditor
+ }
+ }
+
+ tempFile, err := os.CreateTemp(os.TempDir(), "plandex_prompt_*")
+ if err != nil {
+ term.OutputErrorAndExit("Failed to create temporary file: %v", err)
+ }
+
+ instructions := getEditorInstructions(editor)
+ filename := tempFile.Name()
+ err = os.WriteFile(filename, []byte(instructions), 0644)
+ if err != nil {
+ term.OutputErrorAndExit("Failed to write instructions to temporary file: %v", err)
+ }
+
+ editorCmd := prepareEditorCommand(editor, filename)
+ editorCmd.Stdin = os.Stdin
+ editorCmd.Stdout = os.Stdout
+ editorCmd.Stderr = os.Stderr
+ err = editorCmd.Run()
+ if err != nil {
+ term.OutputErrorAndExit("Error opening editor: %v", err)
+ }
+
+ bytes, err := os.ReadFile(tempFile.Name())
+ if err != nil {
+ term.OutputErrorAndExit("Error reading temporary file: %v", err)
+ }
+
+ prompt := string(bytes)
+
+ err = os.Remove(tempFile.Name())
+ if err != nil {
+ term.OutputErrorAndExit("Error removing temporary file: %v", err)
+ }
+
+ prompt = strings.TrimPrefix(prompt, strings.TrimSpace(instructions))
+ prompt = strings.TrimSpace(prompt)
+
+ return prompt
+
+}
+
+func validateTellFlags() {
+ if tellAutoApply && tellNoBuild {
+ term.OutputErrorAndExit("🚨 --apply can't be used with --no-build/-n")
+ }
+ if tellAutoApply && tellBg {
+ term.OutputErrorAndExit("🚨 --apply can't be used with --bg")
+ }
+ if autoCommit && !tellAutoApply {
+ 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 {
+ term.OutputErrorAndExit("🚨 --auto-context/-c can't be used with --bg")
+ }
+ if tellAutoContext && tellStop {
+ term.OutputErrorAndExit("🚨 --auto-context/-c can't be used with --stop/-s")
+ }
+}
+
+func maybeShowDiffs() {
+ diffs, err := api.Client.GetPlanDiffs(lib.CurrentPlanId, lib.CurrentBranch, plainTextOutput || showDiffUi)
+ if err != nil {
+ term.OutputErrorAndExit("Error getting plan diffs: %v", err)
+ return
+ }
+
+ if len(diffs) > 0 {
+ cmd := exec.Command(os.Args[0], "diffs", "--ui")
+
+ // Create a context that's cancelled when the program exits
+ ctx, cancel := context.WithCancel(context.Background())
+
+ // Ensure cleanup on program exit
+ go func() {
+ // Wait for program exit signal
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt, syscall.SIGTERM)
+ <-c
+
+ // Cancel context and kill the process
+ cancel()
+ if cmd.Process != nil {
+ cmd.Process.Kill()
+ }
+ }()
+
+ go func() {
+ if err := cmd.Start(); err != nil {
+ fmt.Fprintf(os.Stderr, "Error starting diffs command: %v\n", err)
+ return
+ }
+
+ // Wait in a separate goroutine
+ go cmd.Wait()
+
+ // Wait for either context cancellation or process completion
+ <-ctx.Done()
+ if cmd.Process != nil {
+ cmd.Process.Kill()
+ }
+ }()
+
+ // Give the UI a moment to start
+ 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"
+}
+`,
+ proposed: `
+// ... existing code ...
+
+func doTell(cmd *cobra.Command, args []string) {
+ auth.MustResolveAuthWithOrg()
+ lib.MustResolveProject()
+
+ if lib.CurrentPlanId == "" {
+ term.OutputNoCurrentPlanErrorAndExit()
+ }
+
+ var apiKeys map[string]string
+ if !auth.Current.IntegratedModelsMode {
+ apiKeys = lib.MustVerifyApiKeys()
+ }
+
+ plan, err := api.Client.GetPlan(lib.CurrentPlanId)
+ if err != nil {
+ term.HandleApiError(err)
+ return
+ }
+
+ // Use plan config if available
+ if plan.Config != nil {
+ autoConfirm = plan.Config.AutoApply
+ autoCommit = plan.Config.AutoCommit
+ tellAutoContext = plan.Config.AutoContext
+ noExec = plan.Config.NoExec
+ autoDebug = plan.Config.AutoDebugTries
+ } else {
+ // Try user default config
+ config, err := api.Client.GetUserConfig()
+ if err != nil {
+ term.HandleApiError(err)
+ return
+ }
+ if config != nil {
+ autoConfirm = config.AutoApply
+ autoCommit = config.AutoCommit
+ tellAutoContext = config.AutoContext
+ noExec = config.NoExec
+ autoDebug = config.AutoDebugTries
+ }
+ }
+
+ prompt := getTellPrompt(args)
+
+ // ... existing code ...
+}
+
+// ... existing code ...
+`,
+ references: []Reference{2, 48, 51},
+ want: `package cmd
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "os"
+ "os/exec"
+ "plandex/api"
+ "plandex/auth"
+ "plandex/lib"
+ "plandex/plan_exec"
+ "plandex/term"
+ "strconv"
+ "strings"
+ "time"
+
+ "context"
+
+ "os/signal"
+ "syscall"
+
+ "github.com/plandex/plandex/shared"
+ "github.com/spf13/cobra"
+)
+
+const defaultEditor = "vim"
+const defaultAutoDebugTries = 5
+
+var autoConfirm bool
+
+// const defaultEditor = "nano"
+
+var tellPromptFile string
+var tellBg bool
+var tellStop bool
+var tellNoBuild bool
+var tellAutoApply bool
+var tellAutoContext bool
+var noExec bool
+var autoDebug int
+
+// tellCmd represents the prompt command
+var tellCmd = &cobra.Command{
+ Use: "tell [prompt]",
+ Aliases: []string{"t"},
+ Short: "Send a prompt for the current plan",
+ // Long: ` + "``" + `,
+ Args: cobra.RangeArgs(0, 1),
+ Run: doTell,
+}
+
+func init() {
+ RootCmd.AddCommand(tellCmd)
+
+ tellCmd.Flags().StringVarP(&tellPromptFile, "file", "f", "", "File containing prompt")
+ tellCmd.Flags().BoolVarP(&tellStop, "stop", "s", false, "Stop after a single reply")
+ tellCmd.Flags().BoolVarP(&tellNoBuild, "no-build", "n", false, "Don't build files")
+ tellCmd.Flags().BoolVar(&tellBg, "bg", false, "Execute autonomously in the background")
+
+ 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 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) {
+ auth.MustResolveAuthWithOrg()
+ lib.MustResolveProject()
+
+ if lib.CurrentPlanId == "" {
+ term.OutputNoCurrentPlanErrorAndExit()
+ }
+
+ var apiKeys map[string]string
+ if !auth.Current.IntegratedModelsMode {
+ apiKeys = lib.MustVerifyApiKeys()
+ }
+
+ plan, err := api.Client.GetPlan(lib.CurrentPlanId)
+ if err != nil {
+ term.HandleApiError(err)
+ return
+ }
+
+ // Use plan config if available
+ if plan.Config != nil {
+ autoConfirm = plan.Config.AutoApply
+ autoCommit = plan.Config.AutoCommit
+ tellAutoContext = plan.Config.AutoContext
+ noExec = plan.Config.NoExec
+ autoDebug = plan.Config.AutoDebugTries
+ } else {
+ // Try user default config
+ config, err := api.Client.GetUserConfig()
+ if err != nil {
+ term.HandleApiError(err)
+ return
+ }
+ if config != nil {
+ autoConfirm = config.AutoApply
+ autoCommit = config.AutoCommit
+ tellAutoContext = config.AutoContext
+ noExec = config.NoExec
+ autoDebug = config.AutoDebugTries
+ }
+ }
+
+ prompt := getTellPrompt(args)
+
+ if prompt == "" {
+ fmt.Println("🤷‍♂️ No prompt to send")
+ return
+ }
+
+ plan_exec.TellPlan(plan_exec.ExecParams{
+ CurrentPlanId: lib.CurrentPlanId,
+ CurrentBranch: lib.CurrentBranch,
+ ApiKeys: apiKeys,
+ CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
+ return lib.CheckOutdatedContextWithOutput(false, autoConfirm || tellAutoApply || tellAutoContext, maybeContexts)
+ },
+ }, prompt, plan_exec.TellFlags{
+ TellBg: tellBg,
+ TellStop: tellStop,
+ TellNoBuild: tellNoBuild,
+ AutoContext: tellAutoContext,
+ ExecEnabled: !noExec,
+ })
+
+ if tellAutoApply {
+ 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),
+ )
+ }
+}
+
+func getTellPrompt(args []string) string {
+ var prompt string
+ var pipedData string
+
+ if len(args) > 0 {
+ prompt = args[0]
+ } else if tellPromptFile != "" {
+ bytes, err := os.ReadFile(tellPromptFile)
+ if err != nil {
+ term.OutputErrorAndExit("Error reading prompt file: %v", err)
+ }
+ prompt = string(bytes)
+ }
+
+ // Check if there's piped input
+ fileInfo, err := os.Stdin.Stat()
+ if err != nil {
+ term.OutputErrorAndExit("Failed to stat stdin: %v", err)
+ }
+
+ if fileInfo.Mode()&os.ModeNamedPipe != 0 {
+ reader := bufio.NewReader(os.Stdin)
+ pipedDataBytes, err := io.ReadAll(reader)
+ if err != nil {
+ term.OutputErrorAndExit("Failed to read piped data: %v", err)
+ }
+ pipedData = string(pipedDataBytes)
+ }
+
+ if prompt == "" && pipedData == "" {
+ prompt = getEditorPrompt()
+ } else if pipedData != "" {
+ if prompt != "" {
+ prompt = fmt.Sprintf("%s\n\n---\n\n%s", prompt, pipedData)
+ } else {
+ prompt = pipedData
+ }
+ }
+
+ return prompt
+}
+
+func prepareEditorCommand(editor string, filename string) *exec.Cmd {
+ switch editor {
+ case "vim":
+ return exec.Command(editor, "+normal G$", "+startinsert!", filename)
+ case "nano":
+ return exec.Command(editor, "+99999999", filename)
+ default:
+ return exec.Command(editor, filename)
+ }
+}
+
+func getEditorInstructions(editor string) string {
+ return "👉 Write your prompt below, then save and exit to send it to Plandex.\n• To save and exit, press ESC, then type :wq! and press ENTER.\n• To exit without saving, press ESC, then type :q! and press ENTER.\n\n\n"
+}
+
+func getEditorPrompt() string {
+ editor := os.Getenv("EDITOR")
+ if editor == "" {
+ editor = os.Getenv("VISUAL")
+ if editor == "" {
+ editor = defaultEditor
+ }
+ }
+
+ tempFile, err := os.CreateTemp(os.TempDir(), "plandex_prompt_*")
+ if err != nil {
+ term.OutputErrorAndExit("Failed to create temporary file: %v", err)
+ }
+
+ instructions := getEditorInstructions(editor)
+ filename := tempFile.Name()
+ err = os.WriteFile(filename, []byte(instructions), 0644)
+ if err != nil {
+ term.OutputErrorAndExit("Failed to write instructions to temporary file: %v", err)
+ }
+
+ editorCmd := prepareEditorCommand(editor, filename)
+ editorCmd.Stdin = os.Stdin
+ editorCmd.Stdout = os.Stdout
+ editorCmd.Stderr = os.Stderr
+ err = editorCmd.Run()
+ if err != nil {
+ term.OutputErrorAndExit("Error opening editor: %v", err)
+ }
+
+ bytes, err := os.ReadFile(tempFile.Name())
+ if err != nil {
+ term.OutputErrorAndExit("Error reading temporary file: %v", err)
+ }
+
+ prompt := string(bytes)
+
+ err = os.Remove(tempFile.Name())
+ if err != nil {
+ term.OutputErrorAndExit("Error removing temporary file: %v", err)
+ }
+
+ prompt = strings.TrimPrefix(prompt, strings.TrimSpace(instructions))
+ prompt = strings.TrimSpace(prompt)
+
+ return prompt
+
+}
+
+func validateTellFlags() {
+ if tellAutoApply && tellNoBuild {
+ term.OutputErrorAndExit("🚨 --apply can't be used with --no-build/-n")
+ }
+ if tellAutoApply && tellBg {
+ term.OutputErrorAndExit("🚨 --apply can't be used with --bg")
+ }
+ if autoCommit && !tellAutoApply {
+ 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 {
+ term.OutputErrorAndExit("🚨 --auto-context/-c can't be used with --bg")
+ }
+ if tellAutoContext && tellStop {
+ term.OutputErrorAndExit("🚨 --auto-context/-c can't be used with --stop/-s")
+ }
+}
+
+func maybeShowDiffs() {
+ diffs, err := api.Client.GetPlanDiffs(lib.CurrentPlanId, lib.CurrentBranch, plainTextOutput || showDiffUi)
+ if err != nil {
+ term.OutputErrorAndExit("Error getting plan diffs: %v", err)
+ return
+ }
+
+ if len(diffs) > 0 {
+ cmd := exec.Command(os.Args[0], "diffs", "--ui")
+
+ // Create a context that's cancelled when the program exits
+ ctx, cancel := context.WithCancel(context.Background())
+
+ // Ensure cleanup on program exit
+ go func() {
+ // Wait for program exit signal
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt, syscall.SIGTERM)
+ <-c
+
+ // Cancel context and kill the process
+ cancel()
+ if cmd.Process != nil {
+ cmd.Process.Kill()
+ }
+ }()
+
+ go func() {
+ if err := cmd.Start(); err != nil {
+ fmt.Fprintf(os.Stderr, "Error starting diffs command: %v\n", err)
+ return
+ }
+
+ // Wait in a separate goroutine
+ go cmd.Wait()
+
+ // Wait for either context cancellation or process completion
+ <-ctx.Done()
+ if cmd.Process != nil {
+ cmd.Process.Kill()
+ }
+ }()
+
+ // Give the UI a moment to start
+ 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"
+}
+`,
+ parser: GetParserForLanguage("go"),
+ },
+
+ {
+ name: "extraneous references, no change",
+ original: `package cmd
+
+import (
+ "fmt"
+ "os"
+ "plandex/term"
+
+ "github.com/fatih/color"
+ "github.com/spf13/cobra"
+)
+
+var helpShowAll bool
+
+// RootCmd represents the base command when called without any subcommands
+var RootCmd = &cobra.Command{
+ Use: ` + "`plandex [command] [flags]`" + `,
+ // Short: "Plandex: iterative development with AI",
+ SilenceErrors: true,
+ SilenceUsage: true,
+ Run: func(cmd *cobra.Command, args []string) {
+ run(cmd, args)
+ },
+}
+
+// Execute adds all child commands to the root command and sets flags appropriately.
+// This is called by main.main(). It only needs to happen once to the rootCmd.
+func Execute() {
+ if err := RootCmd.Execute(); err != nil {
+ // term.OutputErrorAndExit("Error executing root command: %v", err)
+ // log.Fatalf("Error executing root command: %v", err)
+
+ // output the error message to stderr
+ term.OutputSimpleError("Error: %v", err)
+
+ fmt.Println()
+
+ color.New(color.Bold, color.BgGreen, color.FgHiWhite).Println(" Usage ")
+ color.New(color.Bold).Println(" plandex [command] [flags]")
+ color.New(color.Bold).Println(" pdx [command] [flags]")
+ fmt.Println()
+
+ color.New(color.Bold, color.BgGreen, color.FgHiWhite).Println(" Help ")
+ color.New(color.Bold).Println(" plandex help # show basic usage")
+ color.New(color.Bold).Println(" plandex help --all # show all commands")
+ color.New(color.Bold).Println(" plandex [command] --help")
+ fmt.Println()
+
+ os.Exit(1)
+
+ }
+}
+
+func run(cmd *cobra.Command, args []string) {
+}
+
+func init() {
+ var helpCmd = &cobra.Command{
+ Use: "help",
+ Aliases: []string{"h"},
+ Short: "Display help for Plandex",
+ Long: ` + "`Display help for Plandex.`" + `,
+ Run: func(cmd *cobra.Command, args []string) {
+ term.PrintCustomHelp(helpShowAll)
+ },
+ }
+
+ RootCmd.AddCommand(helpCmd)
+
+ // add an --all/-a flag
+ helpCmd.Flags().BoolVarP(&helpShowAll, "all", "a", false, "Show all commands")
+}
+`,
+ proposed: `// ... existing code ...
+
+func init() {
+ var helpCmd = &cobra.Command{
+ Use: "help",
+ Aliases: []string{"h"},
+ Short: "Display help for Plandex",
+ Long: ` + "`Display help for Plandex.`" + `,
+ Run: func(cmd *cobra.Command, args []string) {
+ term.PrintCustomHelp(helpShowAll)
+ },
+ }
+
+ RootCmd.AddCommand(helpCmd)
+
+ // add an --all/-a flag
+ helpCmd.Flags().BoolVarP(&helpShowAll, "all", "a", false, "Show all commands")
+}
+
+// ... existing code ...
+`,
+ references: []Reference{1, 20},
+ want: `package cmd
+
+import (
+ "fmt"
+ "os"
+ "plandex/term"
+
+ "github.com/fatih/color"
+ "github.com/spf13/cobra"
+)
+
+var helpShowAll bool
+
+// RootCmd represents the base command when called without any subcommands
+var RootCmd = &cobra.Command{
+ Use: ` + "`plandex [command] [flags]`" + `,
+ // Short: "Plandex: iterative development with AI",
+ SilenceErrors: true,
+ SilenceUsage: true,
+ Run: func(cmd *cobra.Command, args []string) {
+ run(cmd, args)
+ },
+}
+
+// Execute adds all child commands to the root command and sets flags appropriately.
+// This is called by main.main(). It only needs to happen once to the rootCmd.
+func Execute() {
+ if err := RootCmd.Execute(); err != nil {
+ // term.OutputErrorAndExit("Error executing root command: %v", err)
+ // log.Fatalf("Error executing root command: %v", err)
+
+ // output the error message to stderr
+ term.OutputSimpleError("Error: %v", err)
+
+ fmt.Println()
+
+ color.New(color.Bold, color.BgGreen, color.FgHiWhite).Println(" Usage ")
+ color.New(color.Bold).Println(" plandex [command] [flags]")
+ color.New(color.Bold).Println(" pdx [command] [flags]")
+ fmt.Println()
+
+ color.New(color.Bold, color.BgGreen, color.FgHiWhite).Println(" Help ")
+ color.New(color.Bold).Println(" plandex help # show basic usage")
+ color.New(color.Bold).Println(" plandex help --all # show all commands")
+ color.New(color.Bold).Println(" plandex [command] --help")
+ fmt.Println()
+
+ os.Exit(1)
+
+ }
+}
+
+func run(cmd *cobra.Command, args []string) {
+}
+
+func init() {
+ var helpCmd = &cobra.Command{
+ Use: "help",
+ Aliases: []string{"h"},
+ Short: "Display help for Plandex",
+ Long: ` + "`Display help for Plandex.`" + `,
+ Run: func(cmd *cobra.Command, args []string) {
+ term.PrintCustomHelp(helpShowAll)
+ },
+ }
+
+ RootCmd.AddCommand(helpCmd)
+
+ // add an --all/-a flag
+ helpCmd.Flags().BoolVarP(&helpShowAll, "all", "a", false, "Show all commands")
+}
+`,
+ parser: GetParserForLanguage("go"),
+ },
}
for _, tt := range tests {
- // if tt.name != "top-level with anchor after" {
+ // if tt.name != "extraneous references, no change" {
// continue
// }
t.Run(tt.name, func(t *testing.T) {
@@ -1291,6 +2302,8 @@ class MetricsService(
// punting for now on minor newline discrepancies
// but it should be possible to get this exactly right
assert.Equal(t, strings.ReplaceAll(tt.want, "\n", ""), strings.ReplaceAll(got, "\n", ""))
+
+ // assert.Equal(t, tt.want, got)
})
}
}
diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.go
index 20cbab2a..be60cc86 100644
--- a/app/server/types/active_plan.go
+++ b/app/server/types/active_plan.go
@@ -169,8 +169,10 @@ func (ap *ActivePlan) FlushStreamBuffer() {
ap.streamMu.Unlock()
if len(bufferToFlush) == 1 {
+ log.Println("ActivePlan.FlushStreamBuffer: flushing single message")
ap.Stream(bufferToFlush[0])
} else {
+ log.Println("ActivePlan.FlushStreamBuffer: flushing multi-message")
ap.Stream(shared.StreamMessage{
Type: shared.StreamMessageMulti,
StreamMessages: bufferToFlush,
@@ -179,24 +181,39 @@ func (ap *ActivePlan) FlushStreamBuffer() {
}
func (ap *ActivePlan) Stream(msg shared.StreamMessage) {
+ // log.Println("ActivePlan.Stream:")
+ // log.Println(msg)
+
ap.streamMu.Lock()
+ skipBuffer := false
+ if msg.Type == shared.StreamMessagePromptMissingFile || msg.Type == shared.StreamMessageFinished {
+ skipBuffer = true
+ }
+
// Special messages bypass buffering
- if msg.Type != shared.StreamMessageFinished &&
- msg.Type != shared.StreamMessagePromptMissingFile {
+ if !skipBuffer {
+ // log.Println("ActivePlan.Stream: time since last message sent:", time.Since(ap.lastStreamMessageSent))
if time.Since(ap.lastStreamMessageSent) < MaxStreamRate {
+ // log.Println("ActivePlan.Stream: buffering message")
+
// Buffer the message
ap.streamMessageBuffer = append(ap.streamMessageBuffer, msg)
ap.streamMu.Unlock()
return
} else if len(ap.streamMessageBuffer) > 0 {
+ // log.Println("ActivePlan.Stream: flushing buffer")
+
// Need to flush buffer first
ap.streamMessageBuffer = append(ap.streamMessageBuffer, msg)
bufferToFlush := ap.streamMessageBuffer
ap.streamMessageBuffer = []shared.StreamMessage{}
ap.streamMu.Unlock()
+ // log.Println("ActivePlan.Stream: sending multi-message:")
+ // log.Println(bufferToFlush)
+
// Send as multi-message
ap.Stream(shared.StreamMessage{
Type: shared.StreamMessageMulti,
@@ -218,30 +235,46 @@ func (ap *ActivePlan) Stream(msg shared.StreamMessage) {
return
}
- if msg.Type == shared.StreamMessageFinished && len(ap.streamMessageBuffer) > 0 {
- // Handle any remaining buffered messages before finishing
+ if skipBuffer && len(ap.streamMessageBuffer) > 0 {
+ // Handle any remaining buffered messages before sending the message
+ // log.Println("ActivePlan.Stream: message is a skip buffer type and there are buffered messages")
+ // log.Println("ActivePlan.Stream: flushing remaining buffered messages before skip buffer message is sent")
bufferToFlush := ap.streamMessageBuffer
ap.streamMessageBuffer = []shared.StreamMessage{}
ap.streamMu.Unlock()
log.Println("Flushing buffered messages before finishing")
+ // log.Println("ActivePlan.Stream: sending multi-message:")
+ // log.Println(bufferToFlush)
ap.Stream(shared.StreamMessage{
Type: shared.StreamMessageMulti,
StreamMessages: bufferToFlush,
})
- log.Println("Finished flushing buffered messages, waiting 50ms before sending finish message")
+ log.Println("ActivePlan.Stream: finished flushing buffered messages. waiting 50ms before sending skip buffer type message")
time.Sleep(50 * time.Millisecond)
- log.Println("Sending finish message")
- ap.Stream(msg) // send the finish message
+ log.Println("ActivePlan.Stream: sending finish message")
+ ap.Stream(msg) // send the skip buffer type 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)
- log.Println("Sending done signal")
- ap.StreamDoneCh <- nil
- return
+ ap.streamMu.Lock()
+ now := time.Now()
+ if now.After(ap.lastStreamMessageSent) {
+ ap.lastStreamMessageSent = now
+ }
+ ap.streamMu.Unlock()
+
+ if msg.Type == shared.StreamMessageFinished {
+ // wait for the finish message to be sent then send the done signal
+ log.Println("ActivePlan.Stream: waiting 50ms before sending done signal")
+ time.Sleep(50 * time.Millisecond)
+ log.Println("ActivePlan.Stream: sending done signal")
+ ap.StreamDoneCh <- nil
+ return
+ }
}
+ // log.Println("ActivePlan.Stream: sending direct message")
+ // log.Println(string(msgJson))
+
ap.streamCh <- string(msgJson)
now := time.Now()
@@ -251,9 +284,9 @@ func (ap *ActivePlan) Stream(msg shared.StreamMessage) {
ap.streamMu.Unlock()
if msg.Type == shared.StreamMessageFinished {
- log.Println("Waiting 50ms before sending done signal")
+ log.Println("ActivePlan.Stream: waiting 50ms before sending done signal")
time.Sleep(50 * time.Millisecond)
- log.Println("Sending done signal")
+ log.Println("ActivePlan.Stream: sending done signal")
ap.StreamDoneCh <- nil
}
}
diff --git a/app/server/types/reply.go b/app/server/types/reply.go
index 76a4469a..f6c97608 100644
--- a/app/server/types/reply.go
+++ b/app/server/types/reply.go
@@ -231,6 +231,31 @@ func (r *ReplyParser) GetReplyBeforePath(path string) string {
return strings.Join(r.lines[:idx], "\n")
}
+func (r *ReplyParser) GetReplyForMissingFile() string {
+ path := r.currentFilePath
+
+ var idx int
+ for i := len(r.lines) - 1; i >= 0; i-- {
+ line := r.lines[i]
+ if lineMaybeHasFilePath(line) && path == extractFilePath(line) {
+ idx = i
+ break
+ }
+ }
+
+ if idx == -1 {
+ return strings.Join(r.lines, "\n")
+ }
+
+ idx = idx + 2
+
+ if idx > len(r.lines)-1 {
+ return strings.Join(r.lines, "\n")
+ }
+
+ return strings.Join(r.lines[:idx], "\n") + "\n"
+}
+
func lineMaybeHasFilePath(line string) bool {
couldBe := (strings.HasPrefix(line, "-")) || strings.HasPrefix(line, "-file:") || strings.HasPrefix(line, "- file:") || (strings.HasPrefix(line, "**") && strings.HasSuffix(line, "**")) || (strings.HasPrefix(line, "#") && strings.HasSuffix(line, ":"))