Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
lots of work on structured edits. now fairly reliable but still needs work on full file fallback. also some work on main prompt, improving context loading and planning steps. working toward smart context loading with 'Uses'
Dane Schneiderff46731b506f
diff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.goindex 98dce5ef..57693975 100644--- a/app/cli/cmd/tell.go+++ b/app/cli/cmd/tell.go@@ -256,9 +256,7 @@ func validateTellFlags() {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() {diff --git a/app/server/db/diff_helpers.go b/app/server/db/diff_helpers.goindex ad9e114b..55ef7881 100644--- a/app/server/db/diff_helpers.go+++ b/app/server/db/diff_helpers.go@@ -1,16 +1,11 @@package dbimport (- "bufio""fmt"- "log""os""os/exec""path/filepath"- "strconv"- "strings"- "github.com/google/uuid""github.com/plandex/plandex/shared")@@ -135,145 +130,3 @@ func GetPlanDiffs(orgId, planId string, plain bool) (string, error) {return string(res), nil}--func GetDiffs(original, updated string) (string, error) {- // create temp directory- tempDirPath, err := os.MkdirTemp("", "tmp-diffs-*")-- if err != nil {- return "", fmt.Errorf("error creating temp dir: %v", err)- }-- defer func() {- go os.RemoveAll(tempDirPath)- }()-- // write the original file to the temp dir- err = os.WriteFile(filepath.Join(tempDirPath, "original"), []byte(original), 0644)- if err != nil {- return "", fmt.Errorf("error writing original file: %v", err)- }-- // write the updated file to the temp dir- err = os.WriteFile(filepath.Join(tempDirPath, "updated"), []byte(updated), 0644)- if err != nil {- return "", fmt.Errorf("error writing updated file: %v", err)- }-- cmd := exec.Command("git", "-C", tempDirPath, "diff", "--no-color", "--no-index", "original", "updated")-- res, err := cmd.CombinedOutput()-- if err != nil {- exitError, ok := err.(*exec.ExitError)- if ok && exitError.ExitCode() == 1 {- // Exit status 1 means diffs were found, which is expected- } else {- log.Printf("Error getting diffs: %v\n", err)- log.Printf("Diff output: %s\n", res)- return "", fmt.Errorf("error getting diffs: %v", err)- }- }-- return string(res), nil-}--type change struct {- Old string- New string- Line int- Length int-}--func GetDiffReplacements(original, updated string) ([]*shared.Replacement, error) {- diff, err := GetDiffs(original, updated)- if err != nil {- return nil, fmt.Errorf("error getting git diffs: %v", err)- }-- var changes []*change- scanner := bufio.NewScanner(strings.NewReader(diff))-- var currentHunk *change- var oldLines, newLines []string-- for scanner.Scan() {- line := scanner.Text()-- // Parse hunk header- if strings.HasPrefix(line, "@@") {- // If we have a previous hunk, process it- if currentHunk != nil {- change := processHunk(oldLines, newLines, currentHunk.Line)- if change != nil {- changes = append(changes, change)- }- }-- // Parse the new hunk header- lineInfo := strings.Split(line, " ")[1:] // Skip @@ part- oldInfo := strings.Split(lineInfo[0], ",")- startLine, _ := strconv.Atoi(strings.TrimPrefix(oldInfo[0], "-"))-- currentHunk = &change{- Line: startLine,- }- oldLines = []string{}- newLines = []string{}- continue- }-- if currentHunk == nil {- continue // Skip until we find a hunk- }-- // Process the lines within a hunk- switch {- case strings.HasPrefix(line, "-"):- oldLines = append(oldLines, strings.TrimPrefix(line, "-"))- case strings.HasPrefix(line, "+"):- newLines = append(newLines, strings.TrimPrefix(line, "+"))- case strings.HasPrefix(line, " "):- // Context lines - add to both- line = strings.TrimPrefix(line, " ")- oldLines = append(oldLines, line)- newLines = append(newLines, line)- }- }-- if err := scanner.Err(); err != nil {- return nil, fmt.Errorf("error scanning diff: %v", err)- }-- // Process the last hunk if exists- if currentHunk != nil {- change := processHunk(oldLines, newLines, currentHunk.Line)- if change != nil {- changes = append(changes, change)- }- }-- replacements := make([]*shared.Replacement, len(changes))- for i, change := range changes {- replacements[i] = &shared.Replacement{- Id: uuid.New().String(),- Old: change.Old,- New: change.New,- }- }-- return replacements, nil-}--func processHunk(oldLines, newLines []string, startLine int) *change {- if len(oldLines) == 0 && len(newLines) == 0 {- return nil- }-- return &change{- Old: strings.Join(oldLines, "\n"),- New: strings.Join(newLines, "\n"),- Line: startLine,- Length: len(oldLines),- }-}diff --git a/app/server/db/result_helpers.go b/app/server/db/result_helpers.goindex 39cf6212..e866277a 100644--- a/app/server/db/result_helpers.go+++ b/app/server/db/result_helpers.go@@ -37,7 +37,7 @@ func StorePlanResult(result *PlanFileResult) error {return fmt.Errorf("error creating results dir: %v", err)}- log.Printf("Storing plan result: %s", result.Id)+ log.Printf("Storing plan result: %s - %s", result.Path, result.Id)err = os.WriteFile(filepath.Join(resultsDir, result.Id+".json"), bytes, 0644)diff --git a/app/server/diff/diff.go b/app/server/diff/diff.gonew file mode 100644index 00000000..0775913f--- /dev/null+++ b/app/server/diff/diff.go@@ -0,0 +1,157 @@+package diff++import (+ "bufio"+ "fmt"+ "log"+ "os"+ "os/exec"+ "path/filepath"+ "strconv"+ "strings"++ "github.com/google/uuid"+ "github.com/plandex/plandex/shared"+)++func GetDiffs(original, updated string) (string, error) {+ // create temp directory+ tempDirPath, err := os.MkdirTemp("", "tmp-diffs-*")++ if err != nil {+ return "", fmt.Errorf("error creating temp dir: %v", err)+ }++ defer func() {+ go os.RemoveAll(tempDirPath)+ }()++ // write the original file to the temp dir+ err = os.WriteFile(filepath.Join(tempDirPath, "original"), []byte(original), 0644)+ if err != nil {+ return "", fmt.Errorf("error writing original file: %v", err)+ }++ // write the updated file to the temp dir+ err = os.WriteFile(filepath.Join(tempDirPath, "updated"), []byte(updated), 0644)+ if err != nil {+ return "", fmt.Errorf("error writing updated file: %v", err)+ }++ cmd := exec.Command("git", "-C", tempDirPath, "diff", "--no-color", "--no-index", "original", "updated")++ res, err := cmd.CombinedOutput()++ if err != nil {+ exitError, ok := err.(*exec.ExitError)+ if ok && exitError.ExitCode() == 1 {+ // Exit status 1 means diffs were found, which is expected+ } else {+ log.Printf("Error getting diffs: %v\n", err)+ log.Printf("Diff output: %s\n", res)+ return "", fmt.Errorf("error getting diffs: %v", err)+ }+ }++ return string(res), nil+}++type change struct {+ Old string+ New string+ Line int+ Length int+}++func GetDiffReplacements(original, updated string) ([]*shared.Replacement, error) {+ diff, err := GetDiffs(original, updated)+ if err != nil {+ return nil, fmt.Errorf("error getting git diffs: %v", err)+ }++ var changes []*change+ scanner := bufio.NewScanner(strings.NewReader(diff))++ var currentHunk *change+ var oldLines, newLines []string++ for scanner.Scan() {+ line := scanner.Text()++ // Parse hunk header+ if strings.HasPrefix(line, "@@") {+ // If we have a previous hunk, process it+ if currentHunk != nil {+ change := processHunk(oldLines, newLines, currentHunk.Line)+ if change != nil {+ changes = append(changes, change)+ }+ }++ // Parse the new hunk header+ lineInfo := strings.Split(line, " ")[1:] // Skip @@ part+ oldInfo := strings.Split(lineInfo[0], ",")+ startLine, _ := strconv.Atoi(strings.TrimPrefix(oldInfo[0], "-"))++ currentHunk = &change{+ Line: startLine,+ }+ oldLines = []string{}+ newLines = []string{}+ continue+ }++ if currentHunk == nil {+ continue // Skip until we find a hunk+ }++ // Process the lines within a hunk+ switch {+ case strings.HasPrefix(line, "-"):+ oldLines = append(oldLines, strings.TrimPrefix(line, "-"))+ case strings.HasPrefix(line, "+"):+ newLines = append(newLines, strings.TrimPrefix(line, "+"))+ case strings.HasPrefix(line, " "):+ // Context lines - add to both+ line = strings.TrimPrefix(line, " ")+ oldLines = append(oldLines, line)+ newLines = append(newLines, line)+ }+ }++ if err := scanner.Err(); err != nil {+ return nil, fmt.Errorf("error scanning diff: %v", err)+ }++ // Process the last hunk if exists+ if currentHunk != nil {+ change := processHunk(oldLines, newLines, currentHunk.Line)+ if change != nil {+ changes = append(changes, change)+ }+ }++ replacements := make([]*shared.Replacement, len(changes))+ for i, change := range changes {+ replacements[i] = &shared.Replacement{+ Id: uuid.New().String(),+ Old: change.Old,+ New: change.New,+ }+ }++ return replacements, nil+}++func processHunk(oldLines, newLines []string, startLine int) *change {+ if len(oldLines) == 0 && len(newLines) == 0 {+ return nil+ }++ return &change{+ Old: strings.Join(oldLines, "\n"),+ New: strings.Join(newLines, "\n"),+ Line: startLine,+ Length: len(oldLines),+ }+}diff --git a/app/server/model/plan/build_exec.go b/app/server/model/plan/build_exec.goindex 1148e174..3f7539ef 100644--- a/app/server/model/plan/build_exec.go+++ b/app/server/model/plan/build_exec.go@@ -263,7 +263,7 @@ func (fileState *activeBuildStreamFileState) buildFile() {// SyntaxErrors: validationRes.Errors,}- log.Println("build exec - Plan file result:")+ // log.Println("build exec - new file result")// spew.Dump(planRes)fileState.isNewFile = truediff --git a/app/server/model/plan/build_finish.go b/app/server/model/plan/build_finish.goindex ef4e6c36..82b9a87e 100644--- a/app/server/model/plan/build_finish.go+++ b/app/server/model/plan/build_finish.go@@ -257,7 +257,7 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanF}}()- log.Println("Storing plan result")+ log.Println("Storing plan result", planRes.Path)spew.Dump(planRes)err = db.StorePlanResult(planRes)@@ -271,7 +271,7 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanFreturn err}- log.Println("Plan result stored")+ // log.Println("Plan result stored", planRes.Path)return nil}()diff --git a/app/server/model/plan/build_refs.go b/app/server/model/plan/build_refs.goindex b41df97d..26cf60ed 100644--- a/app/server/model/plan/build_refs.go+++ b/app/server/model/plan/build_refs.go@@ -6,6 +6,7 @@ import ("log""math/rand""plandex-server/db"+ "plandex-server/diff""plandex-server/hooks""plandex-server/model""plandex-server/model/prompts"@@ -226,7 +227,7 @@ func (fileState *activeBuildStreamFileState) buildExpandReferences() {fileState.updated = updatedFile- replacements, err := db.GetDiffReplacements(originalFile, updatedFile)+ replacements, err := diff.GetDiffReplacements(originalFile, updatedFile)if err != nil {log.Printf("buildExpandReferences - error getting diff replacements: %v\n", err)fileState.expandRefsRetryOrError(fmt.Errorf("error getting diff replacements: %v", err))diff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.goindex d3d143bf..45155fb9 100644--- a/app/server/model/plan/build_structured_edits.go+++ b/app/server/model/plan/build_structured_edits.go@@ -6,12 +6,12 @@ import ("log""math/rand""plandex-server/db"+ diff_pkg "plandex-server/diff""plandex-server/hooks""plandex-server/model""plandex-server/model/prompts""plandex-server/syntax""plandex-server/types"- "slices""strings""time"@@ -20,6 +20,8 @@ import ("github.com/sashabaranov/go-openai")+const BuildStructuredEditsMaxTries = 4+func (fileState *activeBuildStreamFileState) buildStructuredEdits() {auth := fileState.authfilePath := fileState.filePath@@ -32,9 +34,7 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {parser := fileState.parserif parser == nil {- log.Println("buildStructuredEdits - tree-sitter parser is nil")- fileState.onBuildFileError(fmt.Errorf("tree-sitter parser is nil"))- return+ log.Printf("buildStructuredEdits - tree-sitter parser is nil for file %s\n", filePath)}activePlan := GetActivePlan(planId, branch)@@ -45,313 +45,202 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {return}- proposedContent := activeBuild.FileContent- proposedContentLines := strings.Split(proposedContent, "\n")- originalContentLines := strings.Split(originalFile, "\n")-- var references []syntax.Reference- var removals []syntax.Removal-- for i, line := range proposedContentLines {- content := strings.TrimSpace(line)-- found := false-- if strings.Contains(strings.ToLower(content), "... existing code ...") {- references = append(references, syntax.Reference(i+1))- found = true- } else if strings.Contains(strings.ToLower(content), "plandex: removed code") {- removals = append(removals, syntax.Removal(i+1))- found = true- }-- if found {- proposedContentLines[i] = strings.Replace(proposedContentLines[i], content, "", 1)- }- }-- proposedContent = strings.Join(proposedContentLines, "\n")-- log.Println("buildStructuredEdits - getting references prompt")-- anchorsSysPrompt := prompts.GetSemanticAnchorsPrompt(filePath, originalFile, proposedContent, activeBuild.FileDescription)-- anchorsFileMessages := []openai.ChatCompletionMessage{- {- Role: openai.ChatMessageRoleSystem,- Content: anchorsSysPrompt,- },- }-- promptTokens, err := shared.GetNumTokens(anchorsSysPrompt)-- if err != nil {- log.Printf("buildStructuredEdits - error getting num tokens for prompt: %v\n", err)- fileState.onBuildFileError(fmt.Errorf("error getting num tokens for prompt: %v", err))- return- }-- inputTokens := prompts.ExtraTokensPerRequest + prompts.ExtraTokensPerMessage + promptTokens-- fileState.inputTokens = inputTokens+ numTries := 1- _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: fileState.plan,- WillSendModelRequestParams: &hooks.WillSendModelRequestParams{- InputTokens: inputTokens,- OutputTokens: shared.AvailableModelsByName[fileState.settings.ModelPack.Builder.BaseModelConfig.ModelName].DefaultReservedOutputTokens,- ModelName: fileState.settings.ModelPack.Builder.BaseModelConfig.ModelName,- },- })- if apiErr != nil {- activePlan.StreamDoneCh <- apiErr- return- }-- 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,- Messages: anchorsFileMessages,- Temperature: config.Temperature,- TopP: config.TopP,- }-- envVar := config.BaseModelConfig.ApiKeyEnvVar- client := clients[envVar]-- resp, err := model.CreateChatCompletionWithRetries(client, activePlan.Ctx, modelReq)+ proposedContent := activeBuild.FileContent+ desc := activeBuild.FileDescription- if err != nil {- log.Printf("buildStructuredEdits - error calling model: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error calling model: %v", err))- return- }+ log.Printf("buildStructuredEdits - %s - applying changes\n", filePath)+ applyRes := syntax.ApplyChanges(+ originalFile,+ proposedContent,+ desc,+ true,+ )+ // log.Println("buildStructuredEdits - applyRes.NewFile:")+ // log.Println(applyRes.NewFile)- log.Println("buildStructuredEdits - usage:")- spew.Dump(resp.Usage)-- _, apiErr = hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{- Auth: auth,- Plan: fileState.plan,- DidSendModelRequestParams: &hooks.DidSendModelRequestParams{- InputTokens: resp.Usage.PromptTokens,- OutputTokens: resp.Usage.CompletionTokens,- ModelName: fileState.settings.ModelPack.Builder.BaseModelConfig.ModelName,- ModelProvider: fileState.settings.ModelPack.Builder.BaseModelConfig.Provider,- ModelPackName: fileState.settings.ModelPack.Name,- ModelRole: shared.ModelRoleBuilder,- Purpose: "Generated file update (structured edits)",- },- })+ proposedContent = applyRes.Proposed- if apiErr != nil {- activePlan.StreamDoneCh <- apiErr- return- }+ for len(applyRes.NeedsVerifyReasons) > 0 && numTries < BuildStructuredEditsMaxTries {+ log.Printf("buildStructuredEdits verify call - numTries: %d\n", numTries)- if len(resp.Choices) == 0 {- log.Printf("buildStructuredEdits - no choices in response\n")- fileState.structuredEditRetryOrError(fmt.Errorf("no choices in response"))- return- }+ numTries++- refsChoice := resp.Choices[0]- content := refsChoice.Message.Content+ log.Println("buildStructuredEdits - needs verify reasons:")+ spew.Dump(applyRes.NeedsVerifyReasons)- log.Println("buildStructuredEdits - content:")- log.Println(content)+ log.Println("buildStructuredEdits - getting verify edits prompt")- anchorsXmlString, err := GetXMLTag(content, "PlandexSemanticAnchors")- if err != nil {- log.Printf("buildStructuredEdits - error parsing PlandexSemanticAnchors xml: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error parsing PlandexSemanticAnchors xml xml: %v", err))- return- }+ var diff string+ if applyRes.NewFile != "" {+ var err error+ diff, err = diff_pkg.GetDiffs(originalFile, applyRes.NewFile)+ if err != nil {+ log.Printf("buildStructuredEdits - error getting diffs: %v\n", err)+ fileState.structuredEditRetryOrError(fmt.Errorf("error getting diffs: %v", err))+ return+ }+ }+ // if diff != "" {+ // log.Println("buildStructuredEdits - diff:")+ // log.Println(diff)+ // }++ validateSysPrompt := prompts.GetValidateEditsPrompt(filePath, originalFile, desc, proposedContent, diff, applyRes.NeedsVerifyReasons)++ validateFileMessages := []openai.ChatCompletionMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: validateSysPrompt,+ },+ }- summaryXmlString, err := GetXMLTag(content, "PlandexSummary")- if err != nil {- log.Printf("buildStructuredEdits - error parsing PlandexSummary xml: %v\n", err)- }+ promptTokens, err := shared.GetNumTokens(validateSysPrompt)- var summaryElement types.SummaryTag- if summaryXmlString != "" {- err = xml.Unmarshal([]byte(summaryXmlString), &summaryElement)if err != nil {- log.Printf("buildStructuredEdits - error unmarshalling Summary xml: %v\n", err)+ log.Printf("buildStructuredEdits - error getting num tokens for prompt: %v\n", err)+ fileState.onBuildFileError(fmt.Errorf("error getting num tokens for prompt: %v", err))+ return}- }- var anchorsElement types.SemanticAnchorsTag+ inputTokens := prompts.ExtraTokensPerRequest + prompts.ExtraTokensPerMessage + promptTokens++ fileState.inputTokens = inputTokens++ _, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{+ Auth: auth,+ Plan: fileState.plan,+ WillSendModelRequestParams: &hooks.WillSendModelRequestParams{+ InputTokens: inputTokens,+ OutputTokens: shared.AvailableModelsByName[fileState.settings.ModelPack.Builder.BaseModelConfig.ModelName].DefaultReservedOutputTokens,+ ModelName: fileState.settings.ModelPack.Builder.BaseModelConfig.ModelName,+ },+ })+ if apiErr != nil {+ activePlan.StreamDoneCh <- apiErr+ return+ }- err = xml.Unmarshal([]byte(anchorsXmlString), &anchorsElement)- if err != nil {- log.Printf("buildStructuredEdits - error unmarshalling xml: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error unmarshalling xml: %v", err))- return- }+ log.Println("buildStructuredEdits - calling model for applied changes validation")- log.Printf("buildStructuredEdits - got %d anchors\n", len(anchorsElement.Anchors))+ modelReq := openai.ChatCompletionRequest{+ Model: config.BaseModelConfig.ModelName,+ Messages: validateFileMessages,+ Temperature: config.Temperature,+ TopP: config.TopP,+ }- anchorLines := make(map[int]int)+ envVar := config.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]- for _, anchor := range anchorsElement.Anchors {- fmt.Printf("anchor: %v\n", anchor)+ resp, err := model.CreateChatCompletionWithRetries(client, activePlan.Ctx, modelReq)- var originalLine, proposedLine int- originalLine, err = shared.ExtractLineNumberWithPrefix(anchor.OriginalLine, "pdx-")if err != nil {- log.Printf("buildStructuredEdits - error parsing anchor original line num: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error parsing anchor original line num: %v", err))+ log.Printf("buildStructuredEdits - error calling model: %v\n", err)+ fileState.structuredEditRetryOrError(fmt.Errorf("error calling model: %v", err))return}- proposedLine, err = shared.ExtractLineNumberWithPrefix(anchor.ProposedLine, "pdx-new-")- if err != nil {- log.Printf("buildStructuredEdits - error parsing anchor proposed line num: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error parsing anchor proposed line num: %v", err))+ log.Println("buildStructuredEdits - usage:")+ spew.Dump(resp.Usage)++ _, apiErr = hooks.ExecHook(hooks.DidSendModelRequest, hooks.HookParams{+ Auth: auth,+ Plan: fileState.plan,+ DidSendModelRequestParams: &hooks.DidSendModelRequestParams{+ InputTokens: resp.Usage.PromptTokens,+ OutputTokens: resp.Usage.CompletionTokens,+ ModelName: fileState.settings.ModelPack.Builder.BaseModelConfig.ModelName,+ ModelProvider: fileState.settings.ModelPack.Builder.BaseModelConfig.Provider,+ ModelPackName: fileState.settings.ModelPack.Name,+ ModelRole: shared.ModelRoleBuilder,+ Purpose: "File edit",+ },+ })++ if apiErr != nil {+ activePlan.StreamDoneCh <- apiErrreturn}- proposedContent := proposedContentLines[proposedLine-1]- originalContent := originalContentLines[originalLine-1]-- if proposedContent != originalContent {- anchorLines[proposedLine] = originalLine- }- }-- refsXmlString, err := GetXMLTag(content, "PlandexReferences")- if err != nil {- log.Printf("buildStructuredEdits - error parsing PlandexReferences xml: %v\n", err)- }-- var refsElement types.ReferencesTag- if refsXmlString != "" {- err = xml.Unmarshal([]byte(refsXmlString), &refsElement)- if err != nil {- log.Printf("buildStructuredEdits - error unmarshalling PlandexReferences xml: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error unmarshalling PlandexReferences xml: %v", err))+ if len(resp.Choices) == 0 {+ log.Printf("buildStructuredEdits - no choices in response\n")+ fileState.structuredEditRetryOrError(fmt.Errorf("no choices in response"))return}- for _, ref := range refsElement.References {- proposedLine, err := shared.ExtractLineNumberWithPrefix(ref.ProposedLine, "pdx-new-")- if err != nil {- log.Printf("buildStructuredEdits - error parsing anchor proposed line num: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error parsing anchor proposed line num: %v", err))- return- }+ refsChoice := resp.Choices[0]+ content := refsChoice.Message.Content- references = append(references, syntax.Reference(proposedLine))- }- }+ log.Printf("buildStructuredEdits - %s - content:\n%s\n", filePath, content)- removalsXmlString, err := GetXMLTag(content, "PlandexRemovals")- if err != nil {- log.Printf("buildStructuredEdits - error parsing PlandexRemovals xml: %v\n", err)- }- var removalsElement types.RemovalsTag- if removalsXmlString != "" {- err = xml.Unmarshal([]byte(removalsXmlString), &removalsElement)- if err != nil {- log.Printf("buildStructuredEdits - error unmarshalling PlandexRemovals xml: %v\n", err)- }+ fixedProposedUpdatesXmlString := GetXMLTag(content, "PlandexProposedUpdates", true)++ // log.Println("buildStructuredEdits - fixed proposed updates xml string:")+ // log.Println(fixedProposedUpdatesXmlString)- for _, removal := range removalsElement.Removals {- proposedLine, err := shared.ExtractLineNumberWithPrefix(removal.ProposedLine, "pdx-new-")+ if fixedProposedUpdatesXmlString == "" {+ // edits are valid+ break+ } else {+ var fixedProposedUpdates syntax.FixedProposedUpdatesTag+ err = xml.Unmarshal([]byte(fixedProposedUpdatesXmlString), &fixedProposedUpdates)if err != nil {- log.Printf("buildStructuredEdits - error parsing anchor proposed line num: %v\n", err)+ log.Printf("buildStructuredEdits - error unmarshalling PlandexProposedUpdates xml: %v\n", err)}- removals = append(removals, syntax.Removal(proposedLine))- }- }-- slices.Sort(references)- slices.Sort(removals)+ proposedContent = fixedProposedUpdates.Content++ // remove code block formatting if it was mistakenly included in the proposed content+ trimmedProposedContent := strings.TrimSpace(proposedContent)+ splitProposedContent := strings.Split(trimmedProposedContent, "\n")+ firstLine := strings.TrimSpace(splitProposedContent[0])+ secondLine := strings.TrimSpace(splitProposedContent[1])+ lastLine := strings.TrimSpace(splitProposedContent[len(splitProposedContent)-1])+ if types.LineMaybeHasFilePath(firstLine) && strings.HasPrefix(secondLine, "```") {+ if lastLine == "```" {+ proposedContent = strings.Join(splitProposedContent[1:len(splitProposedContent)-1], "\n")+ }+ } else if strings.HasPrefix(firstLine, "```") && lastLine == "```" {+ proposedContent = strings.Join(splitProposedContent[1:len(splitProposedContent)-1], "\n")+ }- hasRefByLine := make(map[int]bool)- for _, ref := range references {- hasRefByLine[int(ref)] = true- }- for _, rev := range removals {- hasRefByLine[int(rev)] = true- }+ // log.Println("buildStructuredEdits - fixed proposed content:")+ // log.Println(proposedContent)- var beginsWithRef bool = false- var endsWithRef bool = false- var foundNonRefLine bool = false+ fixedDescString := GetXMLTag(content, "PlandexProposedUpdatesExplanation", false)- for i, line := range proposedContentLines {- hasRef := hasRefByLine[i+1]+ // log.Println("buildStructuredEdits - fixed desc xml string:")+ // log.Println(fixedDescString)- if hasRef {- if !foundNonRefLine {- beginsWithRef = true+ if fixedDescString != "" {+ var descElement syntax.ProposedUpdatesExplanationTag+ err = xml.Unmarshal([]byte(fixedDescString), &descElement)+ if err != nil {+ log.Printf("buildStructuredEdits - error unmarshalling PlandexProposedUpdatesExplanation xml: %v\n", err)+ }+ if descElement.Content != "" {+ desc = descElement.Content+ }}- endsWithRef = true- } else if line != "" {- foundNonRefLine = true- endsWithRef = false- }- }- if !beginsWithRef &&- !strings.Contains(activeBuild.FileDescription, "overwrite the entire file") &&- !((strings.Contains(activeBuild.FileDescription, "replace code") ||- strings.Contains(activeBuild.FileDescription, "remove code")) &&- strings.Contains(activeBuild.FileDescription, "start of the file")) {+ log.Printf("buildStructuredEdits - %s - applying changes again\n", filePath)- log.Println("buildStructuredEdits - adding ... existing code ... to start of file")+ applyRes = syntax.ApplyChanges(+ originalFile,+ proposedContent,+ desc,+ true,+ )- // structured edits handle normalization of comments, so just use // ... existing code ... here- proposedContentLines = append([]string{"// ... existing code ..."}, proposedContentLines...)+ log.Printf("buildStructuredEdits - %s - applyRes.NeedsVerifyReasons: %v\n", filePath, applyRes.NeedsVerifyReasons)- // bump all existing references up by 1- for i, ref := range references {- references[i] = syntax.Reference(int(ref) + 1)+ proposedContent = applyRes.Proposed}- references = append([]syntax.Reference{syntax.Reference(1)}, references...)- }-- if !endsWithRef &&- !strings.Contains(activeBuild.FileDescription, "overwrite the entire 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,- parser,- originalFile,- proposedContent,- references,- removals,- anchorLines,- )- if err != nil {- log.Printf("buildStructuredEdits - error applying references: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error applying references: %v", err))- return- }+ updatedFile := applyRes.NewFilebuildInfo := &shared.BuildInfo{Path: filePath,@@ -366,17 +255,15 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {fileState.updated = updatedFile- replacements, err := db.GetDiffReplacements(originalFile, updatedFile)+ replacements, err := diff_pkg.GetDiffReplacements(originalFile, updatedFile)if err != nil {log.Printf("buildStructuredEdits - error getting diff replacements: %v\n", err)fileState.structuredEditRetryOrError(fmt.Errorf("error getting diff replacements: %v", err))return}- if summaryElement.Content != "" {- for _, replacement := range replacements {- replacement.Summary = strings.TrimSpace(summaryElement.Content)- }+ for _, replacement := range replacements {+ replacement.Summary = strings.TrimSpace(desc)}res := db.PlanFileResult{@@ -391,22 +278,6 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {CanVerify: false, // no verification step with structural edits}- if !fileState.preBuildStateSyntaxInvalid {- validationRes, err := syntax.Validate(activePlan.Ctx, filePath, updatedFile)- if err != nil {- log.Printf("buildStructuredEdits - error validating syntax: %v\n", err)- fileState.structuredEditRetryOrError(fmt.Errorf("error validating syntax: %v", err))- return- }-- if validationRes.HasParser && !validationRes.TimedOut && !validationRes.Valid {- log.Printf("buildStructuredEdits - syntax is invalid\n")- }-- res.SyntaxValid = validationRes.Valid- res.SyntaxErrors = validationRes.Errors- }-fileState.onFinishBuildFile(&res, updatedFile)}diff --git a/app/server/model/plan/build_verify.go b/app/server/model/plan/build_verify.goindex 51aa1a61..abd977f4 100644--- a/app/server/model/plan/build_verify.go+++ b/app/server/model/plan/build_verify.go@@ -4,7 +4,7 @@ import ("encoding/json""fmt""log"- "plandex-server/db"+ diff_pkg "plandex-server/diff""plandex-server/hooks""plandex-server/model""plandex-server/model/prompts"@@ -56,7 +56,7 @@ func (fileState *activeBuildStreamFileState) verifyFileBuild() {var diff stringif verifyState.preBuildFileState != "" {- diff, err = db.GetDiffs(verifyState.preBuildFileState, updated)+ diff, err = diff_pkg.GetDiffs(verifyState.preBuildFileState, updated)if err != nil {log.Printf("Error getting diffs for file '%s': %v\n", filePath, err)diff --git a/app/server/model/lib/context.go b/app/server/model/plan/context.gosimilarity index 57%rename from app/server/model/lib/context.gorename to app/server/model/plan/context.goindex d50ccdeb..d1e5844d 100644--- a/app/server/model/lib/context.go+++ b/app/server/model/plan/context.go@@ -1,17 +1,20 @@-package lib+package planimport ("fmt"- "plandex-server/db""strings""github.com/plandex/plandex/shared")-func FormatModelContext(context []*db.Context, includeMaps, includeTrees bool) (string, int, error) {- var contextMessages []string+func (state *activeTellStreamState) formatModelContext(includeMaps, includeTrees bool) (string, int, error) {+ var contextMessages []string = []string{+ "### LATEST PLAN CONTEXT ###",+ }var numTokens int- for _, part := range context {+ addedFilesSet := map[string]bool{}++ for _, part := range state.modelContext {var message stringvar fmtStr stringvar args []any@@ -24,7 +27,22 @@ func FormatModelContext(context []*db.Context, includeMaps, includeTrees bool) (args = append(args, part.FilePath, part.Body)} else if part.ContextType == shared.ContextFileType {fmtStr = "\n\n- %s:\n\n```\n%s\n```"- args = append(args, part.FilePath, part.Body)++ var body string+ var found bool+ if state.currentPlanState.CurrentPlanFiles != nil {+ res, ok := state.currentPlanState.CurrentPlanFiles.Files[part.FilePath]+ if ok {+ body = res+ found = true+ }+ }+ if !found {+ body = part.Body+ }+ addedFilesSet[part.FilePath] = true++ args = append(args, part.FilePath, body)} else if part.ContextType == shared.ContextMapType {if !includeMaps {continue@@ -54,5 +72,13 @@ func FormatModelContext(context []*db.Context, includeMaps, includeTrees bool) (}}- return strings.Join(contextMessages, "\n"), numTokens, nil++ // Add any current files in plan that weren't added to the context+ for filePath, body := range state.currentPlanState.CurrentPlanFiles.Files {+ if !addedFilesSet[filePath] {+ contextMessages = append(contextMessages, fmt.Sprintf("\n\n- %s:\n\n```\n%s\n```", filePath, body))+ }+ }++ return strings.Join(contextMessages, "\n### END OF CONTEXT ###\n"), numTokens, nil}diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex aac4aab2..691d1a74 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -9,7 +9,6 @@ import ("plandex-server/db""plandex-server/hooks""plandex-server/model"- "plandex-server/model/lib""plandex-server/model/prompts""plandex-server/types"@@ -169,12 +168,12 @@ func execTellPlan(includeMaps = trueincludeTrees = true)- // if req.AutoContext && iteration > 1 {- // includeMaps = false- // includeTrees = false- // }+ if req.AutoContext && iteration > 1 {+ includeMaps = false+ includeTrees = false+ }- modelContextText, modelContextTokens, err := lib.FormatModelContext(state.modelContext, includeMaps, includeTrees)+ modelContextText, modelContextTokens, err := state.formatModelContext(includeMaps, includeTrees)if err != nil {err = fmt.Errorf("error formatting model modelContext: %v", err)log.Println(err)@@ -188,9 +187,11 @@ func execTellPlan(}var sysCreate string+ var contextStage boolif req.AutoContext {if iteration == 0 {- sysCreate = prompts.AutoContextPreamble+ sysCreate = prompts.AutoContextPreamble + prompts.SysCreateAutoContext+ contextStage = true} else {sysCreate = prompts.SysCreateAutoContext}@@ -198,10 +199,12 @@ func execTellPlan(sysCreate = prompts.SysCreateBasic}- if req.ExecEnabled {- sysCreate += prompts.ApplyScriptPrompt- } else {- sysCreate += prompts.NoApplyScriptPrompt+ if !contextStage {+ if req.ExecEnabled {+ sysCreate += prompts.ApplyScriptPrompt+ } else {+ sysCreate += prompts.NoApplyScriptPrompt+ }}// log.Println("sysCreate before context:\n", sysCreate)@@ -465,7 +468,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)+ // log.Printf("Current reply content:\n%s\n", active.CurrentReplyContent)state.replyParser.AddChunk(active.CurrentReplyContent, true)res := state.replyParser.Read()diff --git a/app/server/model/plan/tell_load.go b/app/server/model/plan/tell_load.goindex 8eff7351..7581ca1e 100644--- a/app/server/model/plan/tell_load.go+++ b/app/server/model/plan/tell_load.go@@ -62,6 +62,7 @@ func (state *activeTellStreamState) loadTellPlan() error {var summaries []*db.ConvoSummaryvar settings *shared.PlanSettingsvar latestSummaryTokens int+ var currentPlan *shared.CurrentPlanState// get name for plan and rename it's a draftgo func() {@@ -148,6 +149,7 @@ func (state *activeTellStreamState) loadTellPlan() error {}modelContext = res}+errCh <- nil}()@@ -288,6 +290,21 @@ func (state *activeTellStreamState) loadTellPlan() error {}}+ res, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{+ OrgId: currentOrgId,+ PlanId: planId,+ Contexts: modelContext,+ })+ if err != nil {+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: fmt.Sprintf("Error getting current plan state: %v", err),+ }+ return err+ }+ currentPlan = res+return nil}()@@ -300,6 +317,7 @@ func (state *activeTellStreamState) loadTellPlan() error {state.summaries = summariesstate.latestSummaryTokens = latestSummaryTokensstate.settings = settings+ state.currentPlanState = currentPlanreturn nil}diff --git a/app/server/model/plan/tell_state.go b/app/server/model/plan/tell_state.goindex 9241639b..87ff470f 100644--- a/app/server/model/plan/tell_state.go+++ b/app/server/model/plan/tell_state.go@@ -20,6 +20,7 @@ type activeTellStreamState struct {replyId stringmodelContext []*db.Contextconvo []*db.ConvoMessage+ currentPlanState *shared.CurrentPlanStatemissingFileResponse shared.RespondMissingFileChoicesummaries []*db.ConvoSummarysummarizedToMessageId stringdiff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.goindex 9a69e0e7..31f0b980 100644--- a/app/server/model/plan/tell_stream.go+++ b/app/server/model/plan/tell_stream.go@@ -186,7 +186,13 @@ mainLoop:if choice.FinishReason != "" {log.Println("Model stream finished")- // spew.Dump(choice)+ log.Println("Finish reason: ", choice.FinishReason)++ if choice.FinishReason == "error" {+ state.onError(fmt.Errorf("model stopped with error status | This usually means the model is not responding."), true, "", "")+ continue mainLoop+ }+active.FlushStreamBuffer()time.Sleep(100 * time.Millisecond)@@ -590,12 +596,12 @@ mainLoop:log.Println("Previous reply content:")log.Println(previousReplyContent)- log.Println("Trimmed content:")- log.Println(trimmedContent)+ // log.Println("Trimmed content:")+ // log.Println(trimmedContent)chunkToStream := getCroppedChunk(previousReplyContent+content, trimmedContent, content)- log.Printf("chunkToStream: %s\n", chunkToStream)+ // log.Printf("chunkToStream: %s\n", chunkToStream)if chunkToStream != "" {log.Printf("Streaming remaining chunk before missing file prompt: %s\n", chunkToStream)@@ -614,8 +620,8 @@ mainLoop:})log.Printf("Stopping stream for missing file: %s\n", currentFile)- log.Printf("Chunk content: %s\n", content)- 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 nowactive.CancelModelStreamFn()@@ -700,8 +706,8 @@ mainLoop:if req.BuildMode == shared.BuildModeAuto {log.Printf("Queuing build for %s\n", file)- log.Println("Content:")- log.Println(fileContents[i])+ // log.Println("Content:")+ // log.Println(fileContents[i])buildState := &activeBuildStreamState{tellState: state,diff --git a/app/server/model/plan/xml.go b/app/server/model/plan/xml.goindex ed10541f..18a816eb 100644--- a/app/server/model/plan/xml.go+++ b/app/server/model/plan/xml.go@@ -1,7 +1,6 @@package planimport (- "fmt""regexp""strings")@@ -35,22 +34,47 @@ func UnescapeCdata(xmlString string) string {return escaped}-func GetXMLTag(xmlString, tagName string) (string, error) {+func StripCdata(xmlString, tagName string) string {+ openTag := "<" + tagName + ">"+ closeTag := "</" + tagName + ">"+ xmlString = regexp.MustCompile(openTag+`\s*<!\[CDATA\[`).ReplaceAllString(xmlString, openTag)+ xmlString = regexp.MustCompile(`]]>\s*`+closeTag).ReplaceAllString(xmlString, closeTag)+ return xmlString+}++func WrapCdata(xmlString, tagName string) string {+ openTag := "<" + tagName + ">"+ closeTag := "</" + tagName + ">"+ xmlString = StripCdata(xmlString, tagName)++ xmlString = strings.ReplaceAll(xmlString, openTag, openTag+"<![CDATA[")+ xmlString = strings.ReplaceAll(xmlString, closeTag, "]]>"+closeTag)++ return xmlString+}++func GetXMLTag(xmlString, tagName string, wrapCdata bool) string {openTag := "<" + tagName + ">"closeTag := "</" + tagName + ">"+ // Get everything after the last opening tagsplit := strings.Split(xmlString, openTag)- if len(split) != 2 {- return "", fmt.Errorf("error processing xml")+ if len(split) < 2 {+ return ""}- afterOpenTag := split[1]+ afterOpenTag := split[len(split)-1]+ // Get everything before the first closing tagsplit2 := strings.Split(afterOpenTag, closeTag)- if len(split2) != 2 {- return "", fmt.Errorf("error processing xml")+ if len(split2) < 1 {+ return ""}processedXml := openTag + EscapeInvalidXMLAttributeCharacters(split2[0]) + closeTag- return processedXml, nil+ if wrapCdata {+ processedXml = WrapCdata(processedXml, tagName)+ }++ return processedXml}diff --git a/app/server/model/prompts/build_update.go b/app/server/model/prompts/build_update.goindex 9dbeff9a..da02e656 100644--- a/app/server/model/prompts/build_update.go+++ b/app/server/model/prompts/build_update.go@@ -2,6 +2,7 @@ package promptsimport ("fmt"+ "plandex-server/syntax""github.com/plandex/plandex/shared")@@ -631,3 +632,190 @@ const WholeFileEnding = `Do NOT include any additional text after the <file> element. The output must end after </file>. DO NOT use the string <file> anywhere else in the output. ONLY use it to start the <file> element.`++func GetValidateEditsPrompt(+ path,+ original,+ desc,+ proposed,+ diff string,+ reasons []syntax.NeedsVerifyReason,+) string {+ var s string++ s += fmt.Sprintf(`+[Explanation Rules]++%s++[End Explanation Rules]++[Proposed Updates Rules]++%s++[End Proposed Updates Rules]+`, ChangeExplanationPrompt, UpdateFormatPrompt)++ s += fmt.Sprintf("Path: %s\n\nOriginal file:\n%s\n\nProposed changes explanation:\n%s\n\nProposed changes:\n%s", path, original, desc, proposed)+ s += fmt.Sprintf("\n\nApplied changes:\n%s", diff)+ s += `You will examine the original file, proposed changes explanation, proposed changes, and applied changes diff.`++ var editsIncorrect bool++ for _, reason := range reasons {+ if reason == syntax.NeedsVerifyReasonAmbiguousLocation {+ editsIncorrect = true+ s += `The proposed changes were applied to an ambiguous location. This may mean that anchors were written incorrectly (with incorrect spacing or indentation, for example), or else that the ordering of anchors was incorrect. It may also mean that necessary surrounding context and anchors were not included.`+ } else if reason == syntax.NeedsVerifyReasonNoChanges {+ s += `The proposed changes did not cause any change to the resulting file when applied. Was this intentional?`+ } else if reason == syntax.NeedsVerifyReasonCodeRemoved {+ s += `The proposed changes removed or replaced code. Was this intentional?`+ } else if reason == syntax.NeedsVerifyReasonCodeDuplicated {+ s += `The proposed may have duplicated code. Was this intentional?`+ }+ }++ if editsIncorrect {+ s += EditsIncorrectPrompt+ } else {+ s += EditsValidatePrompt+ }++ s += getEditsExamples(!editsIncorrect)++ s += `++- You MUST NOT make *functional* changes when rewriting the explanation or proposed updates. Your job is not to change functionality or write additional code, but to ensure that the explanation and proposed updates follow all the rules correctly. Stick as close to the original proposed updates as possible when rewriting. Fix the problems, but keep everything else the same as much as possible, including comments, line breaks, spacing, etc.++- You MUST NOT add 'Plandex: removed' comments unless the intention of the plan and the explanation is to remove code. NEVER add 'Plandex: removed' comments to the *proposed updates* unless the *explanation* includes the word 'remove'. The *only* time you can add a 'Plandex: removed' comment is if 1 - you are correcting a mistaken comment that was intended as a removal comment, but used the wrong format—for example, if the 'Plandex: ' prefix was incorrectly omitted. In that case you must fix the comment in the fixed proposed updates. 2 - the explanation explicitly stats that code should be removed, but a necessary 'Plandex: removed' comment is missing from the proposed updates, causing the code that should have been removed to incorrectly remain in the file. In that case you must add the missing 'Plandex: removed' comment to the proposed updates in the correct location, with correct surrounding context and anchors.+`++ return s+}++const EditsValidatePrompt = `+- In a '## Evaluate Diff' section, examine the applied changes diff and determine whether the changes were applied correctly. Carefully asses whether:+ a. Any code was removed that should have been kept.+ b. Any code was duplicated that should not have been.+ c. Any code was incorrectly inserted or replaced.+ d. Any code was inserted at the wrong location.++- If the changes were applied correctly, output "**Changes Applied Correctly**" after the evaluation and stop your response here. You MUST NOT output any further explanation or *any other text* after the "**Changes Applied Correctly**" string. End your response *immediately* after the "**Changes Applied Correctly**" string.++- If the changes were *not* applied correctly, continue with the following steps:+` + EditsIncorrectPrompt++const EditsIncorrectPrompt = `+- In a '## Evaluate Explanation' section, examine the proposed changes explanation and determine whether it is correct. Did it follow all the rules in the [Proposed Updates Rules] section?+ a. If the changes are inserting, removing, or replacing code between two specific code structures, are these the correct code structures to use for the update? Are these structures *immediately* adjacent to each other or is there other code between them?+ b. If the changes are inserting, removing, or replacing code between a specific code structure and the start or end of the file, are these the correct code structures or locations to use for the update? If the change is between the start of the file and a specific code structure, is there any other code between the start of the file and that code structure? (There must not be). If the change is between a specific code structure and the end of the file, is there any other code between that code structure and the end of the file? (There must not be).++- If the proposed changes explanation is *not* correct, you must rewrite the proposed changes explanation to correctly follow *all* the rules in the [Proposed Updates Rules] section in a <PlandexProposedUpdatesExplanation> element. Include *only* the rewritten proposed changes explanation inside the <PlandexProposedUpdatesExplanation> element, and no other text. If the proposed changes explanation is already correct, skip this step.+ - When rewriting the proposed changes explanation, DO NOT change the alter the *type* of the change. You must ONLY update the portion of the explanation that explains the *location* of the change, not the *type* of the change. For example, if the proposed changes explanation is "I'll add 'someFunction' at the end of the file, immediately after 'existingFunction1'", you must rewrite it to correctly explain the *location* of the change, like "I'll add 'someFunction' at the end of the file, immediately after 'existingFunction2'". You MUST NOT change the type of the change, such as from 'add' to 'remove'.+ - If the explanation does not match the changes that were made in the proposed updates (for example, the explanation says to add code, but the proposed updates removed code), this means that the proposed updates are *incorrect*—you MUST NOT alter the explanation to match the incorrect proposed updates (for example, you MUST NOT change the explanation to remove code instead of adding it just because the proposed updates incorrectly removed code). Instead, focus on ensuring that the explanation correctly shows the *location* of the change so that the proposed updates can be corrected.+ - If you are outputting a rewritten proposed changes explanation, YOU ABSOLUTELY MUST DO SO within the <PlandexProposedUpdatesExplanation> element. DO NOT output it outside of the <PlandexProposedUpdatesExplanation> element or anywhere else in your response—ONLY output the rewritten proposed changes explanation inside the <PlandexProposedUpdatesExplanation> element.++- In a '## Evaluate Proposed Changes' section, examine the proposed changes and determine whether they are correct.+ a. Did they follow all the rules in the [Proposed Updates Rules] section?+ b. Are they consistent with the proposed changes explanation (or the rewritten proposed changes explanation, if you had to rewrite it)?+ c. Is more surrounding context needed or are more *anchors* needed to resolve ambiguity in the proposed changes and *precisely* locate the change *unambiguously* in the original file?+ d. Are any *reference comments* or *removal comments* written incorrectly? All reference comments must be *exactly* '... existing code ...' and all removal comments must be *exactly* 'plandex: removed code'. Each must use the correct comment syntax for the language. If any comments are variations of those rather than the exact strings, they are incorrect.+ e. Are any *reference comments* or *removal comments* missing?+ - If the proposed changes incorrectly removed code, this does *NOT MEAN* that a removal comment is missing; you MUST NOT say that a removal comment is missing in this case. *ONLY* state that a removal comment is missing if the explanation *explicitly* states that code should be removed and it was not correctly removed due to a missing removal comment.++- In a <PlandexProposedUpdates> element, output the corrected proposed changes. Include *only* the corrected proposed changes inside the <PlandexProposedUpdates> element, and no other text. The corrected proposed changes MUST follow *all* the rules in the [Proposed Updates Rules] section.+ - If you are outputting a corrected proposed changes, YOU ABSOLUTELY MUST DO SO within the <PlandexProposedUpdates> element. DO NOT output it outside of the <PlandexProposedUpdates> element or anywhere else in your response—ONLY output the corrected proposed changes inside the <PlandexProposedUpdates> element. You ABSOLUTELY MUST NOT use triple backticks or any other formatting around the rewritten proposed changes. Output ONLY the corrected proposed changes inside the <PlandexProposedUpdates> element with no other text or formatting.+ - DO NOT include the proposed changes within a formatted code block. Include *ONLY* the code directly inside the <PlandexProposedUpdates> element.+ - If more surrounding context or *anchors* are needed to resolve ambiguity in the proposed changes and *precisely* locate the change *unambiguously* in the original file, you MUST output the corrected proposed changes with the additional context or *anchors* included correctly.+ - If any *reference comments* or *removal comments* are written incorrectly, you MUST output the corrected proposed changes with the *reference comments* or *removal comments* corrected. A reference comment must be *exactly* '... existing code ...' and a removal comment must be *exactly* 'plandex: removed code'. Use the correct comment syntax for the language.+ - If any *reference comments* or *removal comments* are missing, you MUST output the corrected proposed changes with the *reference comments* or *removal comments* added.+ - If the proposed changes incorrectly removed code, this does *NOT MEAN* that a removal comment is missing; you MUST NOT output a removal comment in this case. Instead, fix the proposed changes to prevent the removal of code that should have been kept.+ - When outputting *anchors* in the proposed changes, lines from the proposed changes that match lines in the original file, spacing and indentation must be preserved *exactly* as they are in the original file—the two lines must be *exactly identical*.+`++func getEditsExamples(evaluateDiff bool) string {+ s := ""++ if evaluateDiff {+ s += `+- Example Response if changes were applied correctly ('## Evaluate Diff' section included):++--++## Evaluate Diff++The new function 'someFunction' was correctly added to the end of the file.++**Changes Applied Correctly**+--++- Example Response if changes were *not* applied correctly, but explanation and proposed changes were correct:++--++## Evaluate Diff++The new function 'someFunction' was incorrectly added to the end of the file.++## Evaluate Explanation++The explanation correctly describes how the new function should be added to the file.++## Evaluate Proposed Changes++The proposed changes correctly use a reference comment to indicate that the new function should be added after 'existingFunction' at the end of the file.++--++Note that if the changes were applied correctly, YOU ABSOLUTLY MUST NEVER output a <PlandexProposedUpdates> element. Just stop the response after the ## Evaluate Proposed Changes section.+`+ }++ s += `+- Example Response if explanation and proposed changes were *not* correct:++--+`++ if evaluateDiff {+ s += `+## Evaluate Diff++The proposed changes incorrectly removed the 'existingFunction2' function.+`+ }++ s += `+## Evaluate Explanation++The explanation incorrectly used 'existingFunction1' as an anchor when there is code between 'existingFunction1' and the end of the file. It should have used 'existingFunction2' instead.+`++ s += `+<PlandexProposedUpdatesExplanation>+ **Updating some/path/to/file.js:** I'll add 'someFunction' at the end of the file, immediately after 'existingFunction2'.+</PlandexProposedUpdatesExplanation>++## Evaluate Proposed Changes++The proposed changes incorrectly used 'existingFunction1' as an anchor when there is code between 'existingFunction1' and the end of the file. It should have used 'existingFunction2' instead.++<PlandexProposedUpdates>+// ... existing code ...++function existingFunction2() {+ // ... existing code ...+}++function someFunction() {+ console.log("New behavior");+ const res = await execUpdate();+}+</PlandexProposedUpdates>++--+`++ return s+}diff --git a/app/server/model/prompts/create.go b/app/server/model/prompts/create.goindex 4fdc26c2..96e9ec76 100644--- a/app/server/model/prompts/create.go+++ b/app/server/model/prompts/create.go@@ -92,9 +92,8 @@ var SysCreateAutoContext = GetCreatePrompt(CreatePromptParams{})var SysCreateAutoContextNumTokens int-const AutoContextPreamble = Identity + ` A plan is a set of files with an attached context.--[YOUR INSTRUCTIONS:]+const AutoContextPreamble = `+[CONTEXT INSTRUCTIONS:]You are operating in 'auto-context mode'. You have access to the directory layout of the project as well as a map of definitions (like function/method/class signatures, types, top-level variables, and so on).@@ -104,7 +103,7 @@ In response to the user's latest prompt, do the following:- Reply with an overview of how you will approach implementing the task (if you've been given a task) or responding to the user (if you're responding in chat form). Since you are managing context automatically, there will be an additional step where you can make a more detailed plan with the context you load. Still, try to consider *everything* the task will require and all the areas of the project it will need to touch. Be thorough and exhaustive in your plan, and don't leave out any steps. For example, if you're being asked to implement an API handler, don't forget that you will need to add the necessary routes to the router as well. Think carefully through details like these and strive not to leave out anything.- - In your own words state something to the effect of: "Since I'm managing context automatically, I'll begin by examining the codebase and determining which files I need."+ - State something to the effect of: "I'll examine the codebase to determine which files I need."- Using the directory layout and the map, explain how the project is organized, with particular focus on areas that may be relevant to the user's task, question, or message.@@ -152,7 +151,15 @@ If you'll be using any definitions from a file—calling a function or method, iInclude 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.+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.++If context is included your prompt, you ABSOLUTELY MUST consider the context that is *ALREADY INCLUDED* and not load additional unnecessary context. It's EXTREMELY IMPORTANT that you do *NOT* load unnecessary context that is already included in the prompt. It's also EXTREMELY IMPORTANT you only list files that will be *UPDATED* during the plan. YOU ABSOLUTELY MUST NEVER include files that will be *CREATED* during the plan.++If you have previously loaded context with a 'Load Context' list in an earlier response during this conversation, focus only on loading any *ADDITIONAL* context that are now helpful or relevant based on the user's latest prompt.++If you have previously loaded context with a 'Load Context' list in an earlier response during this conversation, you DO NOT need to output an '### Additional Context' section. Load any additional context that is now helpful or relevant based on the user's latest prompt and then STOP there *immediately*.++Once you've loaded all necessary context, move on to the instructions below.`var AutoContextPreambleNumTokens int@@ -213,7 +220,23 @@ func GetCreatePrompt(params CreatePromptParams) string {` + "```" + `b. If not:- Explicitly say "Let's break up this task."- - Divide the task into smaller subtasks and list them in a numbered list. Subtasks MUST ALWAYS be numbered. Stop there.+ - Divide the task into smaller subtasks and list them in a numbered list in a '### Subtasks' section. Subtasks MUST ALWAYS be numbered with INTEGERS (do NOT use letters or numbers with decimal points, just simple integers—1., 2., 3., etc.) Subtask numbers MUST be followed by a period and a space, and then the subtask description. Subtasks MUST ALWAYS be listed in the '### Subtasks' section. Follow each subtask with an empty line for readability. Example:++ ### Subtasks++ 1. Create a new file called 'game_logic.h'++ 2. Add the necessary code to the 'game_logic.h' file to define the 'updateGameLogic' function++ 3. Create a new file called 'game_logic.c'++ 4. Add the necessary code to the 'game_logic.c' file to implement the 'updateGameLogic' function++ 5. Update the 'main.c' file to call the 'updateGameLogic' function++ - When you have broken a task up in to multiple subtasks, you MUST ALWAYS follow it with the '### Additional Tasks' section as described below, and then you *ABSOLUTELY MUST END YOUR RESPONSE*. You ABSOLUTELY MUST NOT continue on and begin implementing subtasks.++ - If you have already broken up a task into subtasks in a previous response during this conversation, and you are adding, removing, or modifying subtasks based on a new user prompt, you MUST output the full list of subtasks again in a '### Subtasks' section with the same format as before. You ABSOLUTLEY MUST NEVER output only a partial list of subtasks. Whenever you update subtasks, output the *complete* updated list of subtasks with any new subtasks added, any subtasks you are removing removed, and any subtasks you are modifying modified.`if params.AutoContext {@@ -226,8 +249,8 @@ func GetCreatePrompt(params CreatePromptParams) string {prompt += `- If you are already working on a subtask and the subtask is still too large to be implemented in a single response, it should be further broken down into smaller subtasks. In that case, explicitly say "Let's further break up this subtask", further divide the subtask into even smaller steps, and list them in a numbered list. Stop there. Do NOT do this repetitively for the same subtask. Only break down a given subtask into smaller steps once.- Be thorough and exhaustive in your list of subtasks. Ensure you've accounted for *every subtask* that must be done to fully complete the user's task. Ensure that you list *every* file that needs to be created or updated. Be specific and detailed in your list of subtasks.- - Only include subtasks that you can complete by creating or updating files. If a subtask requires executing code or commands, mention it to the user, but do not include it as a subtask in the plan. Do not include subtasks like "Testing and integration" or "Deployment" that require executing code or commands. Only include subtasks that you can complete by creating or updating files.- - Only break the task up into subtasks that you can do yourself. If a subtask requires executing code or commands, or other tasks that go beyond coding like testing or verifying, deploying, user testing, and son, you can mention it to the user, but you MUST NOT include it as a subtask in the plan. Only include subtasks that can be completed directly with code by creating or updating files.+ - Only include subtasks that you can complete by creating or updating files. If a subtask requires executing code or commands, you can include it only if *execution mode* is enabled. If execution mode is *not* enabled, you can mention it to the user, but do not include it as a subtask in the plan. Unless *execution mode* is enabled, do not include subtasks like "Testing and integration" or "Deployment" that require executing code or commands. Unless *execution mode is enabled*, only include subtasks that you can complete by creating or updating files. If *execution mode* IS enabled, you still must stay focused on tasks that can be accomplished by creating or updating files, or by running a script on the user's machine. Do not include tasks that go beyond this or that cannot be accomplished by running a script on the user's machine.+ - Only break the task up into subtasks that you can do yourself. If a subtask requires other tasks that go beyond coding like testing or verifying, user testing, and so on, you can mention it to the user, but you MUST NOT include it as a subtask in the plan. Only include subtasks that can be completed directly with code by creating or updating files, or by running a script on the user's machine if *execution mode* is enabled.- 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.@@ -313,6 +336,33 @@ func GetCreatePrompt(params CreatePromptParams) string {You MUST ALWAYS work on subtasks IN ORDER. You must not skip a subtask or work on subtasks out of order. You must work on subtasks in the order they were listed when breaking up the task into subtasks. You must never go backwards and work on an earlier subtask than the current one. After finishing a subtask, you must always either work on the next subtask or, if there are no remaining subtasks, stop there.".+ ## Additional tasks section++ If you have broken a task up into multiple subtasks, the list of subtasks ABSOLUTELY MUST ALWAYS be immediately followed by a '### Additional Tasks' section, then the *end* of the response. In the '### Additional Tasks' section, systematically think through whether any additional tasks are needed beyond the list of subtasks that has already been output.++ Think about the nature of the user's task and what kind of subtasks related to it are less obvious or could easily be overlooked.++ Only include tasks that require creating or updating files+ Only include tasks that are strictly necessary to implement the user's request+ Do NOT include tasks related to testing, documentation, or deployment unless specifically requested+ Do NOT include tasks that require running commands or scripts+ Do NOT include tasks that would add features beyond the user's request+ Do NOT include tasks that are about code quality, optimization, or refactoring unless they are strictly necessary++ For any additional tasks identified:++ Add them to the list of subtasks with their own number in the same exact format as the '### Subtasks' section.++ DO NOT output any other numbered list in the '### Additional Tasks' section apart from any additional subtasks. If you want to output a list in your response, use bullet points instead of numbers.pdx++ The '### Additional Tasks' section must be thorough in identifying necessary tasks while avoiding scope creep or unnecessary additions.++ If no additional tasks are strictly necessary, say so and then immediately end the response. You do not need to add additional tasks if none are necessary. Reason carefully and use your best judgement.++ If you have already output an '### Additional Tasks' section in a previous response when you initially broke the plan into subtasks, DO NOT output another '### Additional Tasks' section. Stop the response immediately after the '### Subtasks' section.++ The response ABSOLUTELY MUST NOT include any other text after the '### Additional Tasks' section.+## Things you can and can't doYou are always able to create and update files. Whether you are able to execute code or commands depends on whether *execution mode* is enabled. This will be specified later in the prompt.@@ -1536,15 +1586,15 @@ Prior to any file block that is *updating* an existing file in context, you MUST---'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] between [specific code or structure in original file] and the immediately adjacent [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'+- 'prepend [new code description] to the start of the file, immediately before [specific code or structure in original file]'+- 'append [new code description] to the end of the file, immediately after [specific code or structure in original file]'- 'overwrite the entire file with [new code description]'-- 'replace code between [specific code or structure in original file] and [specific *adjacent* code or structure in original file] with [new code description]'+- 'replace code between [specific code or structure in original file] and the immediately adjacent [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 code between [specific code or structure in original file] and the immediately adjacent [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!@@ -1918,8 +1968,27 @@ 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`." + `++Example:++### Subtasks++1. Add the necessary code to the 'game_logic.h' file to define the 'updateGameLogic' function+Uses: ` + "`src/game_logic.h`" + `++2. Add the necessary code to the 'game_logic.c' file to implement the 'updateGameLogic' function+Uses: ` + "`src/game_logic.c`" + `++3. Update the 'main.c' file to call the 'updateGameLogic' function+Uses: ` + "`src/main.c`" + `++Be exhaustive in the 'Uses:' list. Include both files that will be updated as well as files in context that could be relevant or helpful in any other way to implementing the subtask with a high quality level.++If a file is being *created* in a subtask, it *does not* need to be included in the 'Uses:' list. Only include files that will be *updated* in the subtask.`+var UsesPromptNumTokens int+const ContextLoadingRules = `When loading context, you MUST follow these rules:1. Interface & Implementation Rule:@@ -1979,14 +2048,7 @@ const FileMapScanningRules = `When examining the codebase, you MUST:* 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`+ Example: For 'api/methods.go', look for 'types/api.go', 'api/methods_test.go'`const ContextCompletionCriteria = `Before finalizing your context loading, verify you have:@@ -1998,11 +2060,11 @@ const ContextCompletionCriteria = `Before finalizing your context loading, verifYou 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:+const ContextVerificationSteps = `After thinking through the initial set of 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?+ - For each implementation in use, are relevant types or interfaces also loaded?+ - For each type or interface in use, is its definition loaded?2. Reference Coverage:- For similar features, have all reference files been loaded?@@ -2016,4 +2078,13 @@ const ContextVerificationSteps = `After listing files to load, you MUST verify:- For CLI changes, is the command hierarchy loaded?- Are similar command patterns loaded?-If any of these are "no", you MUST load additional context.`+If any of these are "no", you MUST note additional context to load.++You ABSOLUTELY MUST reason through these points in a section titled "### Additional Context". EVERY response MUST include this section *before* the "Load Context" list.++When reasoning through these points in "### Additional Context", *list specific file paths* that fit the criteria and have not yet been included in the response so far. DO NOT output a line like "Need API interface file" or "Need settings files". You MUST also include the specific file paths that fit the criteria. For example, "Need API interface file:` + "`types/api.go`" + ` or "Need settings files:` + "`settings/config.go`" + `,` + "`settings/user.go`" + ` ".++You only need to include each of the above points in the '### Additional Context' section if they're relevant. Use your own words and your best judgement to think through which additional files may be helpful to completing this task or answering the user's question with a high level of thoroughness and quality.++If necessary files for one of these points has already been listed in the response, do not list them again. This section is for adding any additional files that haven't been mentioned previously. If all necessary files have already been listed, and so no additional context is necessary, say so and move on.+`diff --git a/app/server/syntax/structural_edits.go b/app/server/syntax/structural_edits.goindex 5deed44b..de82d1fe 100644--- a/app/server/syntax/structural_edits.go+++ b/app/server/syntax/structural_edits.go@@ -1,38 +1,283 @@package syntaximport (- "context"+ "encoding/xml""fmt"+ "log"+ "regexp""sort""strings""github.com/davecgh/go-spew/spew"- "github.com/plandex/plandex/shared"tree_sitter "github.com/smacker/go-tree-sitter")+const duplicationThreshold = 20+type Reference inttype Removal int-type Anchor struct {- Open int- Close int-}+type Anchor inttype TreeSitterSection []*tree_sitter.Node-const verboseLogging = true+type NeedsVerifyReason string++const (+ NeedsVerifyReasonCodeRemoved NeedsVerifyReason = "code_removed"+ NeedsVerifyReasonCodeDuplicated NeedsVerifyReason = "code_duplicated"+ NeedsVerifyReasonAmbiguousLocation NeedsVerifyReason = "ambiguous_location"+ NeedsVerifyReasonNoChanges NeedsVerifyReason = "no_changes"+)++type ApplyChangesResult struct {+ NewFile string+ Proposed string+ NeedsVerifyReasons []NeedsVerifyReason+}++type FixedProposedUpdatesTag struct {+ XMLName xml.Name `xml:"PlandexProposedUpdates"`+ Content string `xml:",chardata"`+}++type ProposedUpdatesExplanationTag struct {+ XMLName xml.Name `xml:"PlandexProposedUpdatesExplanation"`+ Content string `xml:",chardata"`+}++type AnchorMap = map[int]int++type ReferenceBlock struct {+ start int // Start line number in original file (inclusive)+ end int // End line number in original file (inclusive)+}++// type ApplyChangesRetryParams struct {+// Ctx context.Context+// Language shared.TreeSitterLanguage+// Parser *tree_sitter.Parser+// References []Reference+// Removals []Removal+// AnchorLines map[int]int+// }++// type treeSitterParams struct {+// language shared.TreeSitterLanguage+// parser *tree_sitter.Parser+// ctx context.Context+// }++const verboseLogging = false++func isRef(content string) bool {+ trimmedLower := strings.ToLower(strings.TrimSpace(content))+ if strings.Contains(trimmedLower, "... existing code ...") {+ return true+ }+ regex := regexp.MustCompile(`(\.\.\.)?.*?existing.*?\.\.\.$`)+ return regex.MatchString(trimmedLower)+}++func isRemoval(content string) bool {+ return strings.Contains(strings.ToLower(content), "plandex: removed")+}func ApplyChanges(- ctx context.Context,- language shared.TreeSitterLanguage,- parser *tree_sitter.Parser,+ original,+ proposed,+ desc string,+ addMissingStartEndRefs bool,+) *ApplyChangesResult {+ proposedInitial := proposed++ proposedLines := strings.Split(proposed, "\n")+ originalLines := strings.Split(original, "\n")++ var references []Reference+ hasRefByLine := map[int]bool{}++ var removals []Removal+ hasRemovalByLine := map[int]bool{}++ for i, line := range proposedLines {+ lineNum := i + 1+ content := strings.TrimSpace(line)+ found := false+ if isRef(content) {+ if !hasRefByLine[lineNum] {+ references = append(references, Reference(lineNum))+ hasRefByLine[lineNum] = true+ }+ found = true+ } else if isRemoval(content) {+ if !hasRemovalByLine[lineNum] {+ removals = append(removals, Removal(lineNum))+ hasRemovalByLine[lineNum] = true+ }+ found = true+ }++ if found {+ proposedLines[i] = strings.Replace(proposedLines[i], content, "", 1)+ }+ }++ if addMissingStartEndRefs {+ var beginsWithRef bool = false+ var endsWithRef bool = false+ var foundNonRefLine bool = false++ for i, line := range proposedLines {+ hasRef := hasRefByLine[i+1] || hasRemovalByLine[i+1]++ if hasRef {+ if !foundNonRefLine {+ beginsWithRef = true+ }+ endsWithRef = true+ } else if line != "" {+ foundNonRefLine = true+ endsWithRef = false+ }+ }++ if !beginsWithRef &&+ !strings.Contains(desc, "overwrite the entire file") &&+ !((strings.Contains(desc, "replace code") ||+ strings.Contains(desc, "remove code")) &&+ strings.Contains(desc, "start of the file")) {++ if verboseLogging {+ fmt.Println("adding ... existing code ... to start of file")+ }++ proposedLines = append([]string{""}, proposedLines...)++ // bump all existing references up by 1+ for i, ref := range references {+ references[i] = Reference(int(ref) + 1)+ }+ references = append([]Reference{Reference(1)}, references...)+ }++ if !endsWithRef &&+ !strings.Contains(desc, "overwrite the entire file") &&+ !(strings.Contains(desc, "replace code") ||+ strings.Contains(desc, "remove code")) &&+ !strings.Contains(desc, "end of the file") {++ if verboseLogging {+ fmt.Println("adding ... existing code ... to end of file")+ }++ proposedLines = append(proposedLines, "")+ references = append(references, Reference(len(proposedLines)))+ }+ }++ proposed = strings.Join(proposedLines, "\n")++ // log.Println("ApplyChanges - proposed:")+ // log.Println(proposed)++ // log.Println("ApplyChanges - references:")+ // spew.Dump(references)+ // log.Println("ApplyChanges - removals:")+ // spew.Dump(removals)++ res := ExecApplyChanges(+ original,+ proposed,+ originalLines,+ proposedLines,+ references,+ removals,+ )++ res.Proposed = proposed++ if len(res.NeedsVerifyReasons) > 0 {+ log.Println("ApplyChanges - needs verify reasons:")+ spew.Dump(res.NeedsVerifyReasons)++ log.Println("ApplyChanges - proposed:")+ log.Println(proposedInitial)+ log.Println("--------------------------------")++ // log.Println("ApplyChanges - original:")+ // log.Println(original)+ // log.Println("--------------------------------")+ return res+ }++ if strings.TrimSpace(res.NewFile) == strings.TrimSpace(original) {+ // log.Println("ApplyChanges - no changes")+ // log.Println("res.NewFile:")+ // log.Println(res.NewFile)+ // log.Println()+ // log.Println("original:")+ // log.Println(original)+ res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonNoChanges)+ return res+ }++ originalLineMap := make(map[string]bool)+ for _, line := range originalLines {+ originalLineMap[strings.TrimSpace(line)] = true+ }++ newLines := strings.Split(res.NewFile, "\n")+ newLineMap := make(map[string]bool)+ for _, line := range newLines {+ newLineMap[strings.TrimSpace(line)] = true+ }++ // Check for removed lines (lines in original that are not in new)+ for line := range originalLineMap {+ if !newLineMap[line] {+ log.Println("ApplyChanges - code removed")+ log.Println("line:")+ log.Println(line)+ res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonCodeRemoved)+ break+ }+ }++ // Check for lines in proposed updates that are duplicated in new file+ newLineFreq := make(map[string]int)+ for _, line := range newLines {+ trimmed := strings.TrimSpace(line)+ if len(trimmed) > duplicationThreshold {+ newLineFreq[line]+++ }+ }+ for _, line := range proposedLines {+ trimmed := strings.TrimSpace(line)+ if len(trimmed) > duplicationThreshold {+ if newLineFreq[line] > 1 {+ log.Println("ApplyChanges - code duplicated")+ log.Println("line:")+ log.Println(line)+ res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonCodeDuplicated)+ break+ }+ }+ }++ return res+}++func ExecApplyChanges(original,proposed string,+ originalLines,+ proposedLines []string,references []Reference,removals []Removal,- anchorLines map[int]int,-) (string, error) {+) *ApplyChangesResult {+ res := &ApplyChangesResult{}+var b strings.Builderwrite := func(s string, newline bool) {@@ -64,56 +309,11 @@ func ApplyChanges(removalsByLine[removal] = true}- originalLines := strings.Split(original, "\n")- proposedLines := strings.Split(proposed, "\n")-- // normalize comments in case the wrong comment symbols were used- openingCommentSymbol, closingCommentSymbol := GetCommentSymbols(language)- if openingCommentSymbol != "" {- for i, line := range proposedLines {- // keep indentation for syntax parsing- content := strings.TrimSpace(line)-- if removalsByLine[Removal(i+1)] || refsByLine[Reference(i+1)] {- comment := openingCommentSymbol + " ref " + closingCommentSymbol- proposedLines[i] = strings.Replace(line, content, comment, 1)- }- }- }-- proposedWithNormalizedComments := strings.Join(proposedLines, "\n")-- originalBytes := []byte(original)- proposedBytes := []byte(proposedWithNormalizedComments)-- originalTree, err := parser.ParseCtx(ctx, nil, originalBytes)- if err != nil {- return "", fmt.Errorf("failed to parse the original content: %v", err)- }- defer originalTree.Close()-- proposedTree, err := parser.ParseCtx(ctx, nil, proposedBytes)- if err != nil {- return "", fmt.Errorf("failed to parse the proposed content: %v", err)- }- defer proposedTree.Close()-- if verboseLogging {- fmt.Printf("anchorLines: %v\n", anchorLines)- }-- oRes := buildNodeIndex(originalTree)- pRes := buildNodeIndex(proposedTree)-- originalNodesByLineIndex := oRes.nodesByLine- proposedNodesByLineIndex := pRes.nodesByLine- originalParentsByLineIndex := oRes.parentsByLine-anchorMap := buildAnchorMap(originalLines,proposedLines,- originalNodesByLineIndex,- anchorLines,+ refsByLine,+ removalsByLine,)if verboseLogging {@@ -127,17 +327,19 @@ func ApplyChanges(fmt.Printf("found anchor in anchorLines: oLineNum: %d, pLineNum: %d\n", oLineNum, pLineNum)}- oNode := originalNodesByLineIndex[oLineNum-1]-- if verboseLogging {- fmt.Printf("oNode.Type(): %s\n", oNode.Type())+ oLine := originalLines[oLineNum-1]+ if strings.TrimSpace(oLine) == "" {+ if verboseLogging {+ fmt.Printf("skipping anchor because oLine is blank: %q\n", oLine)+ }+ return nil}- anchor := &Anchor{Open: oLineNum, Close: int(oNode.EndPoint().Row) + 1}+ anchor := Anchor(oLineNum)if verboseLogging {- fmt.Printf("found anchor: %v\n", anchor)+ fmt.Printf("found anchor: %d\n", anchor)}- return anchor+ return &anchor} else {if verboseLogging {fmt.Printf("no anchor found in anchorMap: pLineNum: %d\n", pLineNum)@@ -147,37 +349,12 @@ func ApplyChanges(return nil}- proposedUpdatesHaveLine := func(line string, afterLine int) bool {- for idx, pLine := range proposedLines {- pLineNum := idx + 1-- // if verboseLogging {- // fmt.Printf("proposedUpdatesHaveLine - lineNum: %d, line: %s, pLine: %s, afterLine: %d\n", pLineNum, line, pLine, afterLine)- // }-- if pLineNum > afterLine && pLine == line {- return true- }- }- return false- }-var oLineNum int = 0var refOpen boolvar refStart int- var refOriginalParent *tree_sitter.Nodevar postRefBuffers []strings.Builder- closingLinesByPLineNum := map[int]int{}- var noMatchUntilStructureClose string- depth := 0-- var currentPNode *tree_sitter.Node- var currentPNodeEndsAtIdx int- var currentPNodeMatches bool-lastLineMatched := true- foundAnyAnchor := falsesetOLineNum := func(n int) {if n < 1 {@@ -218,21 +395,7 @@ func ApplyChanges(}}- incDepth := func() {- depth++- if verboseLogging {- fmt.Printf("incrementing depth: %d\n", depth)- }- }-- decDepth := func() {- depth--- if verboseLogging {- fmt.Printf("decrementing depth: %d\n", depth)- }- }-- writeRefs := func(eof bool) error {+ writeRefs := func(eof bool) bool {numRefs := len(postRefBuffers)if numRefs == 1 {if verboseLogging {@@ -255,7 +418,6 @@ func ApplyChanges(fmt.Printf("fullRef refStart: %d\n", refStart)fmt.Printf("originalLines[refStart]: %q\n", originalLines[refStart])fmt.Printf("writing eof fullRef: %q\n", strings.Join(fullRef, "\n"))- fmt.Printf("depth: %d\n", depth)}} else {start := refStart - 1@@ -278,7 +440,6 @@ func ApplyChanges(fmt.Printf("\n=== INVALID SLICE BOUNDS DIAGNOSTIC INFO ===\n")fmt.Printf("start: %d, end: %d\n", start, end)fmt.Printf("refStart: %d, oLineNum: %d\n", refStart, oLineNum)- fmt.Printf("depth: %d, refOpen: %v\n", depth, refOpen)// Log relevant lines for contextfmt.Printf("\nOriginal lines context:\n")@@ -291,7 +452,9 @@ func ApplyChanges(fmt.Printf("\nProposed lines context:\n")fmt.Printf("=====================================\n\n")- return fmt.Errorf("invalid slice bounds: start(%d) > end(%d)", start, end)+ res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonAmbiguousLocation)++ return true}if verboseLogging {@@ -304,7 +467,6 @@ func ApplyChanges(fmt.Printf("fullRef refStart: %d, oLineNum-1: %d\n", refStart, oLineNum-1)fmt.Printf("originalLines[start]: %q\n", originalLines[start])fmt.Printf("originalLines[end]: %q\n", originalLines[end])- fmt.Printf("depth: %d\n", depth)}}@@ -320,41 +482,15 @@ func ApplyChanges(}} else {if verboseLogging {- fmt.Printf("numRefs > 1, refOriginalParent: %s, eof: %v\n", refOriginalParent.Type(), eof)- fmt.Printf("refOriginalParent.Content(originalBytes):\n%q\n", refOriginalParent.Content(originalBytes))- fmt.Printf("numRefs: %d, oLineNum: %d\n", numRefs, oLineNum)- }-- var upToLine int- if eof {- upToLine = len(originalLines)- } else {- upToLine = oLineNum+ fmt.Printf("writeRefs - ambiguous location - numRefs: %d\n", numRefs)}- sections := getSections(- refOriginalParent,- originalBytes,- numRefs,- refStart,- upToLine,- foundAnyAnchor,- )+ res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonAmbiguousLocation)- for i, section := range sections {- if verboseLogging {- fmt.Printf("writing section i: %d\n", i)- // fmt.Printf("writing i: %d, section:\n%q\n ", i, section.String(originalLines, originalBytes))- }- write(section.String(originalLines, originalBytes), false)- if verboseLogging {- fmt.Println("writing postRefBuffer")- }- write(postRefBuffers[i].String(), false)- }+ return true}- return nil+ return false}for idx, pLine := range proposedLines {@@ -377,11 +513,7 @@ func ApplyChanges(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)fmt.Printf("lastLineMatched: %v\n", lastLineMatched)- fmt.Printf("depth: %d\n", depth)}if isRemoval {@@ -395,9 +527,6 @@ func ApplyChanges(if !refOpen {if verboseLogging {fmt.Println("isRef - opening ref")- pnode := proposedNodesByLineIndex[idx]- fmt.Printf("pnode.Type(): %s\n", pnode.Type())- // fmt.Printf("pnode.Content(proposedBytes):\n%q\n", pnode.Content(proposedBytes))}refOpen = true@@ -407,39 +536,6 @@ func ApplyChanges(if verboseLogging {fmt.Printf("setting refStart: %d\n", refStart)}-- if depth > 0 {- refNode := originalNodesByLineIndex[refStart-1]-- if verboseLogging {- fmt.Printf("refNode.Type(): %s\n", refNode.Type())- // fmt.Printf("refNode.Content(originalBytes): %q\n", refNode.Content(originalBytes))-- current := refNode- depth := 0- for current != nil {- fmt.Printf("parent depth %d type: %s content: %q\n",- depth,- current.Type(),- current.Content(originalBytes))- current = current.Parent()- depth++- }- }-- refOriginalParent = originalParentsByLineIndex[refStart-1]-- if verboseLogging {- fmt.Printf("setting refOriginalParent | refNode.Parent() | Type(): %s\n", refOriginalParent.Type())- }- } else {- refOriginalParent = originalTree.RootNode()-- if verboseLogging {- fmt.Printf("setting refOriginalParent | originalTree.RootNode() | Type(): %s\n", refOriginalParent.Type())- }- }-}addNewPostRefBuffer()@@ -451,7 +547,7 @@ func ApplyChanges(continue}- if !refOpen && lastLineMatched && !currentPNodeMatches {+ if !refOpen && lastLineMatched {if strings.TrimSpace(pLine) == "" {if verboseLogging {fmt.Printf("pLine is blank\n")@@ -484,86 +580,12 @@ func ApplyChanges(}}- pNode := proposedNodesByLineIndex[idx]- pNodeStartsThisLine := pNode.StartPoint().Row == uint32(idx)- pNodeEndsAtIdx := int(pNode.EndPoint().Row)- pNodeMultiline := pNodeEndsAtIdx > idx-var matching bool- isClosingAnchor := closingLinesByPLineNum[pLineNum] != 0-- if verboseLogging {- fmt.Printf("currentPNode != nil: %v\n", currentPNode != nil)- fmt.Printf("currentPNodeMatches: %v\n", currentPNodeMatches)- }- if isClosingAnchor {- if verboseLogging {- fmt.Printf("isClosingAnchor: %v\n", isClosingAnchor)- }+ anchor := findAnchor(pLineNum)+ if anchor != nil {matching = true- setOLineNum(closingLinesByPLineNum[pLineNum])- noMatchUntilStructureClose = ""- } else if noMatchUntilStructureClose != pLine && !(currentPNode != nil && !currentPNodeMatches) {- // find next line in original that matches- anchor := findAnchor(pLineNum)- if anchor != nil {- foundAnyAnchor = true- if verboseLogging {- fmt.Println("anchor found")- fmt.Printf("anchor.Close: %d, anchor.Open: %d\n", anchor.Close, anchor.Open)- }- matching = true- setOLineNum(anchor.Open)-- if pNodeStartsThisLine && pNodeMultiline && currentPNode != nil {- currentPNodeMatches = true- }-- if anchor.Close != 0 && anchor.Close != anchor.Open {-- originalClosingLine := originalLines[anchor.Close-1]-- if verboseLogging {- fmt.Printf("originalClosingLine: %s\n", originalClosingLine)- }-- if proposedUpdatesHaveLine(originalClosingLine, idx) {- // if verboseLogging {- // fmt.Printf("proposedUpdatesHaveLine: %v\n", proposedUpdatesHaveLine(originalClosingLine, anchor.Open))- // }- if verboseLogging {- fmt.Printf("proposedUpdatesHaveLine: %v\n", proposedUpdatesHaveLine(originalClosingLine, anchor.Open))- }- closingPLineNum := int(pNode.EndPoint().Row) + 1- closingLinesByPLineNum[closingPLineNum] = anchor.Close- noMatchUntilStructureClose = originalClosingLine- incDepth()- if verboseLogging {- fmt.Printf("anchor.Close: %d\n", anchor.Close)- fmt.Printf("closingPLineNum: %d\n", closingPLineNum)- fmt.Printf("noMatchUntilStructureClose: %s\n", noMatchUntilStructureClose)- }- } else {- if verboseLogging {- fmt.Println("proposed updates do not have originalClosingLine")- }- }- }- }- }-- if pNodeStartsThisLine && pNodeMultiline && (currentPNode == nil || matching != currentPNodeMatches) {- if verboseLogging {- fmt.Printf("setting currentPNode: %s\n", pNode.Type())- fmt.Printf("pNodeEndsAtIdx: %d\n", pNodeEndsAtIdx)- // fmt.Printf("content: %q\n", pNode.Content(proposedBytes))- // fmt.Println(pNode)- }- currentPNode = pNode- currentPNodeEndsAtIdx = pNodeEndsAtIdx- currentPNodeMatches = matching-+ setOLineNum(int(*anchor))}wroteRefs := false@@ -578,12 +600,12 @@ func ApplyChanges(fmt.Printf("closing ref, oLineNum: %d\n", oLineNum)}refOpen = false- err := writeRefs(false)- if err != nil {- return "", fmt.Errorf("error writing refs: %w", err)- }+ willAbort := writeRefs(false)write(pLine, !finalLine)wroteRefs = true+ if willAbort {+ return res+ }}} else {if verboseLogging {@@ -591,18 +613,6 @@ func ApplyChanges(}}- lastLineMatched = matching-- if currentPNodeEndsAtIdx == idx {- currentPNode = nil- currentPNodeEndsAtIdx = 0- currentPNodeMatches = false- }-- if isClosingAnchor {- decDepth()- }-if wroteRefs {// reset buffersresetPostRefBuffers()@@ -624,9 +634,9 @@ func ApplyChanges(}if refOpen {- err := writeRefs(true)- if err != nil {- return "", fmt.Errorf("error writing refs: %w", err)+ willAbort := writeRefs(true)+ if willAbort {+ return res}}@@ -634,370 +644,157 @@ func ApplyChanges(// fmt.Printf("final result:\n%s\n", b.String())}- return b.String(), nil+ res.NewFile = b.String()++ return res}-func getSections(parent *tree_sitter.Node, bytes []byte, numSections, fromLine, upToLine int, foundAnyAnchor bool) []TreeSitterSection {- sections := make([]TreeSitterSection, numSections)- structures := [][]*tree_sitter.Node{}- latestStructure := []*tree_sitter.Node{}+func buildAnchorMap(+ originalLines []string,+ proposedLines []string,+ refsByLine map[Reference]bool,+ removalsByLine map[Removal]bool,+) AnchorMap {+ result := AnchorMap{}- cursor := tree_sitter.NewTreeCursor(parent)- defer cursor.Close()+ setAnchor := func(pLine, oLine int) {+ result[pLine] = oLine- parentFirstLineNum := int(parent.StartPoint().Row) + 1- parentEndLineNum := int(parent.EndPoint().Row) + 1- if verboseLogging {- fmt.Printf("parent.Type(): %s\n", parent.Type())- fmt.Printf("parent.Content(bytes):\n%q\n", parent.Content(bytes))- fmt.Printf("parentFirstLineNum: %d\n", parentFirstLineNum)- fmt.Printf("parentEndLineNum: %d\n", parentEndLineNum)+ if verboseLogging {+ fmt.Printf("setAnchor - pLine: %d, oLine: %d\n", pLine, oLine)+ }}- if cursor.GoToFirstChild() {- for {- node := cursor.CurrentNode()-- startLineNum := int(node.StartPoint().Row) + 1- endLineNum := int(node.EndPoint().Row) + 1- if verboseLogging {- fmt.Printf("startLineNum: %d, endLineNum: %d\n", startLineNum, endLineNum)- fmt.Println(node.Type())- fmt.Printf("node.Content(bytes):\n%q\n", node.Content(bytes))- }-- if startLineNum < fromLine {- if verboseLogging {- fmt.Println("startLineNum < fromLine, skipping")- fmt.Printf("skipping lineNum: %d | before fromLine: %d\n", startLineNum, fromLine)- }- if !cursor.GoToNextSibling() {- if verboseLogging {- fmt.Println("no next sibling, breaking")- }- break- }- continue- }-- if startLineNum == parentFirstLineNum && foundAnyAnchor {- if verboseLogging {- fmt.Printf("skipping first line\n")- }- if !cursor.GoToNextSibling() {- if verboseLogging {- fmt.Println("no next sibling, breaking")- }- break- }- continue- }-- if endLineNum > upToLine {- if verboseLogging {- fmt.Println("endLineNum > upToLine, breaking")- fmt.Printf("upToLine: %d, endLineNum: %d, node.Type(): %s\n", upToLine, endLineNum, node.Type())-- toLog := node.Content(bytes)- if len(toLog) > 200 {- toLog = toLog[:100] + "\n...\n" + toLog[len(toLog)-100:]- }- fmt.Println(toLog)- }-- break- }-- if endLineNum == parentEndLineNum && !isStructuralNode(node) {- if verboseLogging {- fmt.Println("endLineNum == parentEndLineNum && !isStructuralNode(node), breaking")- }- break- }-- if isStructuralNode(node) {- if verboseLogging {- fmt.Printf("found structural node: %s\n", node.Type())- fmt.Printf("starting new group\n")- }- if len(latestStructure) > 0 {- structures = append(structures, latestStructure)- }- latestStructure = []*tree_sitter.Node{node}- } else {- if verboseLogging {- fmt.Printf("not structural: %s\n", node.Type())- }- latestStructure = append(latestStructure, node)- }+ referenceBlocks := []ReferenceBlock{}- if !cursor.GoToNextSibling() {- if verboseLogging {- fmt.Println("no next sibling, breaking")- }- break- }- }- if len(latestStructure) > 0 {- if verboseLogging {- fmt.Println("appending latestStructure to structures")+ // Helper to check if a line is in a reference block+ isInReference := func(lineNum int) bool {+ for _, block := range referenceBlocks {+ if lineNum >= block.start && lineNum <= block.end {+ return true}- structures = append(structures, latestStructure)}+ return false}- if verboseLogging {- fmt.Printf("structures:\n")- spew.Dump(structures)+ allRefsByLine := map[int]bool{}+ for ref := range refsByLine {+ allRefsByLine[int(ref)] = true}-- numStructural := len(structures)- baseSize := numStructural / numSections- remainder := numStructural % numSections-- if verboseLogging {- fmt.Printf("baseSize: %d, remainder: %d\n", baseSize, remainder)+ for removal := range removalsByLine {+ allRefsByLine[int(removal)] = true}- startIndex := 0-- for i := 0; i < numSections; i++ {- size := baseSize- if i < remainder {- size++- }-- endIndex := startIndex + size- if endIndex > len(structures) {- endIndex = len(structures)- }-- var section TreeSitterSection- group := structures[startIndex:endIndex]-- for _, s := range group {- section = append(section, s...)- }- sections[i] = section- startIndex = endIndex+ // Keep track of definitely new code lines+ newCodeLines := make(map[int]bool)+ originalLinesSet := map[string]bool{}+ for _, line := range originalLines {+ originalLinesSet[line] = true}-- if verboseLogging {- fmt.Printf("sections:\n")- spew.Dump(sections)-- fmt.Println("Sections content:")- for i, section := range sections {- fmt.Printf("section %d:\n", i)- for j, node := range section {- fmt.Printf("node %d:\n", j)- toLog := node.Content(bytes)- if len(toLog) > 200 {- toLog = toLog[:100] + "\n...\n" + toLog[len(toLog)-100:]- }- fmt.Println(toLog)- }+ for idx, line := range proposedLines {+ if _, inOriginal := originalLinesSet[line]; !inOriginal {+ newCodeLines[idx+1] = true}}- return sections-}--func isStructuralNode(node *tree_sitter.Node) bool {- nodeType := node.Type()-- if strings.Contains(nodeType, "comment") {- return false- }+ foundRefBounds := map[int]bool{}- if strings.HasSuffix(nodeType, "space") {- return false- }+ // When we establish an anchor match, check if we can determine reference bounds+ tryEstablishReferenceBounds := func() {+ if verboseLogging {+ fmt.Println("\n\ntryEstablishReferenceBounds")+ }+ for lineNum := range allRefsByLine {+ if !foundRefBounds[lineNum] {+ if verboseLogging {+ fmt.Printf("tryEstablishReferenceBounds - lineNum: %d\n", lineNum)+ }- switch nodeType {- case "ws", "newline", "indent", "dedent":- return false- }+ prevSignificantLineNum := lineNum - 1+ linesBack := 1+ for prevSignificantLineNum > 0 && proposedLines[prevSignificantLineNum-1] == "" {+ prevSignificantLineNum--+ linesBack+++ }- if node.IsNamed() {- return true- }+ nextSignificantLineNum := lineNum + 1+ linesForward := 1+ for nextSignificantLineNum <= len(proposedLines) && proposedLines[nextSignificantLineNum-1] == "" {+ nextSignificantLineNum+++ linesForward+++ }- if node.ChildCount() > 0 {- return true- }+ if verboseLogging {+ fmt.Printf("prevSignificantLineNum: %d, nextSignificantLineNum: %d\n", prevSignificantLineNum, nextSignificantLineNum)+ }- return false-}+ var top, bottom int-type nodeIndex struct {- nodesByLine map[int]*tree_sitter.Node- parentsByLine map[int]*tree_sitter.Node-}+ if prevSignificantLineNum <= 1 {+ if verboseLogging {+ fmt.Printf("prevSignificantLineNum <= 1 - setting top to 1\n")+ }+ top = 1+ } else if _, isAnchor := result[prevSignificantLineNum]; isAnchor {+ if verboseLogging {+ fmt.Printf("prevSignificantLineNum is anchor - setting top to %d\n", result[prevSignificantLineNum])+ }+ top = result[prevSignificantLineNum] + linesBack+ }-func buildNodeIndex(tree *tree_sitter.Tree) *nodeIndex {- nodesByLine := make(map[int]*tree_sitter.Node)- parentsByLine := make(map[int]*tree_sitter.Node)-- root := tree.RootNode()-- // First pass - index direct nodes- var indexNodes func(node *tree_sitter.Node, depth int)- indexNodes = func(node *tree_sitter.Node, depth int) {- // if verboseLogging {- // fmt.Printf("node: %s, depth: %d, childCount: %d\n", node.Type(), depth, node.ChildCount())- // // spew.Dump(node.StartPoint())- // }-- if node.Type() != root.Type() {- line := int(node.StartPoint().Row)- existing, exists := nodesByLine[line]- if !exists ||- node.StartPoint().Column < existing.StartPoint().Column ||- (node.StartPoint().Column == existing.StartPoint().Column && depth < getNodeDepth(existing)) {- nodesByLine[line] = node- parentsByLine[line] = node.Parent()- }- }+ if nextSignificantLineNum >= len(proposedLines) {+ if verboseLogging {+ fmt.Printf("nextSignificantLineNum >= len(proposedLines) - setting bottom to len(originalLines)\n")+ }+ bottom = len(originalLines)+ } else if _, isAnchor := result[nextSignificantLineNum]; isAnchor {+ if verboseLogging {+ fmt.Printf("nextSignificantLineNum is anchor - setting bottom to %d\n", result[nextSignificantLineNum])+ }+ bottom = result[nextSignificantLineNum] - linesForward+ } else if newCodeLines[nextSignificantLineNum] {- for i := 0; i < int(node.ChildCount()); i++ {- child := node.Child(i)- indexNodes(child, depth+1)- }- }+ if verboseLogging {+ fmt.Printf("nextSignificantLineNum is new code - finding next anchor\n")+ }- indexNodes(root, 0)-- // Second pass - fill in missing lines with parent nodes- endLine := int(root.EndPoint().Row)- for line := 0; line <= endLine; line++ {- if _, exists := nodesByLine[line]; !exists {- // Find containing parent node- var findParent func(node *tree_sitter.Node) *tree_sitter.Node- findParent = func(node *tree_sitter.Node) *tree_sitter.Node {- nodeStart := int(node.StartPoint().Row)- nodeEnd := int(node.EndPoint().Row)-- if nodeStart <= line && line <= nodeEnd {- // Check children first for more specific containment- for i := 0; i < int(node.ChildCount()); i++ {- child := node.Child(i)- if found := findParent(child); found != nil {- return found+ // go forward from here to find the next anchor (or eof)+ foundAnchor := false+ for i := nextSignificantLineNum; i < len(proposedLines)+1; i++ {+ if _, isAnchor := result[i]; isAnchor {+ bottom = result[i] - 1+ foundAnchor = true+ if verboseLogging {+ fmt.Printf("found anchor at %d\n", i)+ }+ break+ }+ }+ if !foundAnchor {+ bottom = len(originalLines)+ if verboseLogging {+ fmt.Printf("no anchor found - setting bottom to len(originalLines)\n")}}- return node}- return nil- }- parent := findParent(root)-- if parent == nil {- nodesByLine[line] = root- } else {- nodesByLine[line] = parent- parentsByLine[line] = parent+ if top != 0 && bottom != 0 {+ foundRefBounds[lineNum] = true+ referenceBlocks = append(referenceBlocks, ReferenceBlock{start: top, end: bottom})+ if verboseLogging {+ fmt.Printf("found reference bounds: %d-%d\n", top, bottom)+ }+ } else {+ if verboseLogging {+ fmt.Printf("no reference bounds found for lineNum: %d\n", lineNum)+ }+ }}}}- // spew.Dump(nodesByLine)- return &nodeIndex{- nodesByLine: nodesByLine,- parentsByLine: parentsByLine,- }-}--func getNodeDepth(node *tree_sitter.Node) int {- depth := 0- current := node- for current.Parent() != nil {- depth++- current = current.Parent()- }- return depth-}--func (s TreeSitterSection) String(sourceLines []string, bytes []byte) string {- if len(s) == 0 {- return ""- }-- // Find first structural node- var firstNode *tree_sitter.Node- for _, n := range s {- if isStructuralNode(n) {- firstNode = n- break- }- }-- // Find last structural node- var lastNode *tree_sitter.Node- for i := len(s) - 1; i >= 0; i-- {- if isStructuralNode(s[i]) {- lastNode = s[i]- break- }- }-- startIdx := int(firstNode.StartPoint().Row)- endIdx := int(lastNode.EndPoint().Row)-- if verboseLogging {- for _, node := range s {- fmt.Printf("node.Type(): %s\n", node.Type())- fmt.Printf("StartPoint().Row: %d\n", node.StartPoint().Row)- fmt.Printf("EndPoint().Row: %d\n", node.EndPoint().Row)- }-- fmt.Printf("section.String startIdx: %d, endIdx: %d\n", startIdx, endIdx)- }-- parent := lastNode.Parent()- parentEndNode := parent.Child(int(parent.ChildCount()) - 1)- lastLine := sourceLines[endIdx]- if lastLine == parentEndNode.Content(bytes) {- endIdx--- }-- result := strings.Join(sourceLines[startIdx:endIdx+1], "\n") + "\n"-- if verboseLogging {- toLog := result- if len(toLog) > 200 {- toLog = toLog[:100] + "\n...\n" + toLog[len(toLog)-100:]- }- fmt.Printf("section.String result: %s\n", toLog)- }-- 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)@@ -1017,15 +814,13 @@ func buildAnchorMap(// Build frequency maps for this sectionfor i := oStart; i < oEnd; i++ {- if content := strings.TrimSpace(originalLines[i]); content != "" {- sectionOriginal[content] = append(sectionOriginal[content], i)- }+ content := originalLines[i]+ 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)- }+ content := proposedLines[i]+ sectionProposed[content] = append(sectionProposed[content], i)}// Handle unique matches first@@ -1035,17 +830,21 @@ func buildAnchorMap(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])- }+ if _, exists := result[pIdx+1]; !exists {+ setAnchor(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])}}}+ // after finding unique anchors, try to establish reference bounds+ tryEstablishReferenceBounds()+// Get ordered anchors to establish subsectionsvar orderedAnchors []struct {pLine int@@ -1094,15 +893,13 @@ func buildAnchorMap(// Build frequency maps for this subsectionfor i := lastOLine; i < nextOLine; i++ {- if content := strings.TrimSpace(originalLines[i]); content != "" {- subSectionOriginal[content] = append(subSectionOriginal[content], i)- }+ content := originalLines[i]+ 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)- }+ content := proposedLines[i]+ subSectionProposed[content] = append(subSectionProposed[content], i)}// Match duplicates from outside-in@@ -1112,6 +909,7 @@ func buildAnchorMap(if verboseLogging {fmt.Printf("New code found: %q at proposed lines %v\n", content, pIndices)}+continue // This is new code to be inserted}@@ -1133,28 +931,52 @@ func buildAnchorMap(// Match from outside in until we run out of original occurrencesmatched := 0for matched < len(oIndices) && matched*2 < len(pIndices) {++ if isInReference(oIndices[matched] + 1) {+ if verboseLogging {+ fmt.Printf("Skipping reference line %d\n", oIndices[matched]+1)+ }+ matched+++ continue+ }+// Match first unmatched occurrence- if oNode := originalNodesByLineIndex[oIndices[matched]]; oNode != nil {- result[pIndices[matched]+1] = oIndices[matched] + 1+ if _, exists := result[pIndices[matched]+1]; !exists {+ setAnchor(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]])}++ // after finding anchor, try to establish reference bounds+ tryEstablishReferenceBounds()}// Match last unmatched occurrence if we have more to matchif 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 isInReference(lastOrigIdx + 1) {+ if verboseLogging {+ fmt.Printf("Skipping reference line %d\n", lastOrigIdx+1)+ }+ matched+++ continue+ }+ if _, exists := result[lastPropIdx+1]; !exists {+ setAnchor(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])}++ // after finding anchor, try to establish reference bounds+ tryEstablishReferenceBounds()}+}matched++}diff --git a/app/server/syntax/structural_edits_sections.go b/app/server/syntax/structural_edits_sections.gonew file mode 100644index 00000000..066504dd--- /dev/null+++ b/app/server/syntax/structural_edits_sections.go@@ -0,0 +1,347 @@+package syntax++import (+ "fmt"+ "strings"++ "github.com/davecgh/go-spew/spew"+ tree_sitter "github.com/smacker/go-tree-sitter"+)++func getSections(parent *tree_sitter.Node, bytes []byte, numSections, fromLine, upToLine int, foundAnyAnchor bool) []TreeSitterSection {+ sections := make([]TreeSitterSection, numSections)+ structures := [][]*tree_sitter.Node{}+ latestStructure := []*tree_sitter.Node{}++ cursor := tree_sitter.NewTreeCursor(parent)+ defer cursor.Close()++ parentFirstLineNum := int(parent.StartPoint().Row) + 1+ parentEndLineNum := int(parent.EndPoint().Row) + 1+ if verboseLogging {+ fmt.Printf("parent.Type(): %s\n", parent.Type())+ fmt.Printf("parent.Content(bytes):\n%q\n", parent.Content(bytes))+ fmt.Printf("parentFirstLineNum: %d\n", parentFirstLineNum)+ fmt.Printf("parentEndLineNum: %d\n", parentEndLineNum)+ }++ if cursor.GoToFirstChild() {+ for {+ node := cursor.CurrentNode()++ startLineNum := int(node.StartPoint().Row) + 1+ endLineNum := int(node.EndPoint().Row) + 1+ if verboseLogging {+ fmt.Printf("startLineNum: %d, endLineNum: %d\n", startLineNum, endLineNum)+ fmt.Println(node.Type())+ fmt.Printf("node.Content(bytes):\n%q\n", node.Content(bytes))+ }++ if startLineNum < fromLine {+ if verboseLogging {+ fmt.Println("startLineNum < fromLine, skipping")+ fmt.Printf("skipping lineNum: %d | before fromLine: %d\n", startLineNum, fromLine)+ }+ if !cursor.GoToNextSibling() {+ if verboseLogging {+ fmt.Println("no next sibling, breaking")+ }+ break+ }+ continue+ }++ if startLineNum == parentFirstLineNum && foundAnyAnchor {+ if verboseLogging {+ fmt.Printf("skipping first line\n")+ }+ if !cursor.GoToNextSibling() {+ if verboseLogging {+ fmt.Println("no next sibling, breaking")+ }+ break+ }+ continue+ }++ if endLineNum > upToLine {+ if verboseLogging {+ fmt.Println("endLineNum > upToLine, breaking")+ fmt.Printf("upToLine: %d, endLineNum: %d, node.Type(): %s\n", upToLine, endLineNum, node.Type())++ toLog := node.Content(bytes)+ if len(toLog) > 200 {+ toLog = toLog[:100] + "\n...\n" + toLog[len(toLog)-100:]+ }+ fmt.Println(toLog)+ }++ break+ }++ if endLineNum == parentEndLineNum && !isStructuralNode(node) {+ if verboseLogging {+ fmt.Println("endLineNum == parentEndLineNum && !isStructuralNode(node), breaking")+ }+ break+ }++ if isStructuralNode(node) {+ if verboseLogging {+ fmt.Printf("found structural node: %s\n", node.Type())+ fmt.Printf("starting new group\n")+ }+ if len(latestStructure) > 0 {+ structures = append(structures, latestStructure)+ }+ latestStructure = []*tree_sitter.Node{node}+ } else {+ if verboseLogging {+ fmt.Printf("not structural: %s\n", node.Type())+ }+ latestStructure = append(latestStructure, node)+ }++ if !cursor.GoToNextSibling() {+ if verboseLogging {+ fmt.Println("no next sibling, breaking")+ }+ break+ }+ }+ if len(latestStructure) > 0 {+ if verboseLogging {+ fmt.Println("appending latestStructure to structures")+ }+ structures = append(structures, latestStructure)+ }+ }++ if verboseLogging {+ fmt.Printf("structures:\n")+ spew.Dump(structures)+ }++ numStructural := len(structures)+ baseSize := numStructural / numSections+ remainder := numStructural % numSections++ if verboseLogging {+ fmt.Printf("baseSize: %d, remainder: %d\n", baseSize, remainder)+ }++ startIndex := 0++ for i := 0; i < numSections; i++ {+ size := baseSize+ if i < remainder {+ size+++ }++ endIndex := startIndex + size+ if endIndex > len(structures) {+ endIndex = len(structures)+ }++ var section TreeSitterSection+ group := structures[startIndex:endIndex]++ for _, s := range group {+ section = append(section, s...)+ }+ sections[i] = section+ startIndex = endIndex+ }++ if verboseLogging {+ fmt.Printf("sections:\n")+ spew.Dump(sections)++ fmt.Println("Sections content:")+ for i, section := range sections {+ fmt.Printf("section %d:\n", i)+ for j, node := range section {+ fmt.Printf("node %d:\n", j)+ toLog := node.Content(bytes)+ if len(toLog) > 200 {+ toLog = toLog[:100] + "\n...\n" + toLog[len(toLog)-100:]+ }+ fmt.Println(toLog)+ }+ }+ }++ return sections+}++func isStructuralNode(node *tree_sitter.Node) bool {+ nodeType := node.Type()++ if strings.Contains(nodeType, "comment") {+ return false+ }++ if strings.HasSuffix(nodeType, "space") {+ return false+ }++ switch nodeType {+ case "ws", "newline", "indent", "dedent":+ return false+ }++ if node.IsNamed() {+ return true+ }++ if node.ChildCount() > 0 {+ return true+ }++ return false+}++type nodeIndex struct {+ nodesByLine map[int]*tree_sitter.Node+ parentsByLine map[int]*tree_sitter.Node+}++func buildNodeIndex(tree *tree_sitter.Tree) *nodeIndex {+ nodesByLine := make(map[int]*tree_sitter.Node)+ parentsByLine := make(map[int]*tree_sitter.Node)++ root := tree.RootNode()++ // First pass - index direct nodes+ var indexNodes func(node *tree_sitter.Node, depth int)+ indexNodes = func(node *tree_sitter.Node, depth int) {+ // if verboseLogging {+ // fmt.Printf("node: %s, depth: %d, childCount: %d\n", node.Type(), depth, node.ChildCount())+ // // spew.Dump(node.StartPoint())+ // }++ if node.Type() != root.Type() {+ line := int(node.StartPoint().Row)+ existing, exists := nodesByLine[line]+ if !exists ||+ node.StartPoint().Column < existing.StartPoint().Column ||+ (node.StartPoint().Column == existing.StartPoint().Column && depth < getNodeDepth(existing)) {+ nodesByLine[line] = node+ parentsByLine[line] = node.Parent()+ }+ }++ for i := 0; i < int(node.ChildCount()); i++ {+ child := node.Child(i)+ indexNodes(child, depth+1)+ }+ }++ indexNodes(root, 0)++ // Second pass - fill in missing lines with parent nodes+ endLine := int(root.EndPoint().Row)+ for line := 0; line <= endLine; line++ {+ if _, exists := nodesByLine[line]; !exists {+ // Find containing parent node+ var findParent func(node *tree_sitter.Node) *tree_sitter.Node+ findParent = func(node *tree_sitter.Node) *tree_sitter.Node {+ nodeStart := int(node.StartPoint().Row)+ nodeEnd := int(node.EndPoint().Row)++ if nodeStart <= line && line <= nodeEnd {+ // Check children first for more specific containment+ for i := 0; i < int(node.ChildCount()); i++ {+ child := node.Child(i)+ if found := findParent(child); found != nil {+ return found+ }+ }+ return node+ }+ return nil+ }++ parent := findParent(root)++ if parent == nil {+ nodesByLine[line] = root+ } else {+ nodesByLine[line] = parent+ parentsByLine[line] = parent+ }+ }+ }++ // spew.Dump(nodesByLine)+ return &nodeIndex{+ nodesByLine: nodesByLine,+ parentsByLine: parentsByLine,+ }+}++func getNodeDepth(node *tree_sitter.Node) int {+ depth := 0+ current := node+ for current.Parent() != nil {+ depth+++ current = current.Parent()+ }+ return depth+}++func (s TreeSitterSection) String(sourceLines []string, bytes []byte) string {+ if len(s) == 0 {+ return ""+ }++ // Find first structural node+ var firstNode *tree_sitter.Node+ for _, n := range s {+ if isStructuralNode(n) {+ firstNode = n+ break+ }+ }++ // Find last structural node+ var lastNode *tree_sitter.Node+ for i := len(s) - 1; i >= 0; i-- {+ if isStructuralNode(s[i]) {+ lastNode = s[i]+ break+ }+ }++ startIdx := int(firstNode.StartPoint().Row)+ endIdx := int(lastNode.EndPoint().Row)++ if verboseLogging {+ for _, node := range s {+ fmt.Printf("node.Type(): %s\n", node.Type())+ fmt.Printf("StartPoint().Row: %d\n", node.StartPoint().Row)+ fmt.Printf("EndPoint().Row: %d\n", node.EndPoint().Row)+ }++ fmt.Printf("section.String startIdx: %d, endIdx: %d\n", startIdx, endIdx)+ }++ parent := lastNode.Parent()+ parentEndNode := parent.Child(int(parent.ChildCount()) - 1)+ lastLine := sourceLines[endIdx]+ if lastLine == parentEndNode.Content(bytes) {+ endIdx--+ }++ result := strings.Join(sourceLines[startIdx:endIdx+1], "\n") + "\n"++ if verboseLogging {+ toLog := result+ if len(toLog) > 200 {+ toLog = toLog[:100] + "\n...\n" + toLog[len(toLog)-100:]+ }+ fmt.Printf("section.String result: %s\n", toLog)+ }++ return result+}diff --git a/app/server/syntax/structural_edits_test.go b/app/server/syntax/structural_edits_test.goindex 4bd784c4..7dbe2111 100644--- a/app/server/syntax/structural_edits_test.go+++ b/app/server/syntax/structural_edits_test.go@@ -1,27 +1,19 @@package syntaximport (- "context""fmt"- "strings""testing"- "github.com/plandex/plandex/shared"- tree_sitter "github.com/smacker/go-tree-sitter""github.com/stretchr/testify/assert")func TestStructuralReplacements(t *testing.T) {tests := []struct {- name string- original string- proposed string- references []Reference- removals []Removal- want string- language shared.TreeSitterLanguage- parser *tree_sitter.Parser- anchorLines map[int]int+ name string+ original string+ proposed string+ want string+ ext string}{{name: "single reference in function",@@ -39,9 +31,7 @@ func TestStructuralReplacements(t *testing.T) {log.Info("processing user")return nil}`,- references: []Reference{- 3,- },+want: `func processUser(id int) error {validate(id)@@ -51,8 +41,7 @@ func TestStructuralReplacements(t *testing.T) {log.Info("processing user")return nil}`,- language: shared.TreeSitterLanguageGo,- parser: GetParserForLanguage("go"),+ ext: "go",},{name: "bad formatting",@@ -71,9 +60,7 @@ func TestStructuralReplacements(t *testing.T) {}return update(id)}`,- references: []Reference{- 3,- },+want: `func processUser(id int) error {validate(id)@@ -84,99 +71,96 @@ func TestStructuralReplacements(t *testing.T) {}return update(id)}`,- language: shared.TreeSitterLanguageGo,- parser: GetParserForLanguage("go"),+ ext: "go",},- {- name: "multiple refs in class/nested structures",- original: `- package main-- import "log"+ // {+ // name: "multiple refs in class/nested structures",+ // original: `+ // package main- func init() {- log.Println("init")- }+ // import "log"- type UserService struct {- db *DB- cache *Cache- }+ // func init() {+ // log.Println("init")+ // }- func (s *UserService) Process() {- s.validate()- s.update()- s.notify()- }+ // type UserService struct {+ // db *DB+ // cache *Cache+ // }- func (s *UserService) Update() {- s.db.begin()- s.db.exec()- s.db.commit()- }+ // func (s *UserService) Process() {+ // s.validate()+ // s.update()+ // s.notify()+ // }- func (s *UserService) Record() {- log.Println("record")- }- `,- proposed: `- // ... existing code ...+ // func (s *UserService) Update() {+ // s.db.begin()+ // s.db.exec()+ // s.db.commit()+ // }- type UserService struct {- // ... existing code ...- metrics *Metrics- }+ // func (s *UserService) Record() {+ // log.Println("record")+ // }+ // `,+ // proposed: `+ // // ... existing code ...- func (s *UserService) Process() {- // ... existing code ...- s.metrics.Record()- // ... existing code ...- }+ // type UserService struct {+ // // ... existing code ...+ // metrics *Metrics+ // }- func (s *UserService) Update() {- // ... existing code ...- }+ // func (s *UserService) Process() {+ // // ... existing code ...+ // s.metrics.Record()+ // // ... existing code ...+ // }- // ... existing code ...- `,- references: []Reference{- 2, 5, 10, 12, 16, 19,- },- want: `- package main-- import "log"+ // func (s *UserService) Update() {+ // // ... existing code ...+ // }- func init() {- log.Println("init")- }+ // // ... existing code ...+ // `,+ //+ // want: `+ // package main- type UserService struct {- db *DB- cache *Cache- metrics *Metrics- }+ // import "log"- func (s *UserService) Process() {- s.validate()- s.update()- s.metrics.Record()- s.notify()- }+ // func init() {+ // log.Println("init")+ // }- func (s *UserService) Update() {- s.db.begin()- s.db.exec()- s.db.commit()- }+ // type UserService struct {+ // db *DB+ // cache *Cache+ // metrics *Metrics+ // }- func (s *UserService) Record() {- log.Println("record")- }- `,- language: shared.TreeSitterLanguageGo,- parser: GetParserForLanguage("go"),- },+ // func (s *UserService) Process() {+ // s.validate()+ // s.update()+ // s.metrics.Record()+ // s.notify()+ // }++ // func (s *UserService) Update() {+ // s.db.begin()+ // s.db.exec()+ // s.db.commit()+ // }++ // func (s *UserService) Record() {+ // log.Println("record")+ // }+ // `,+ //+ // parser: GetParserForLanguage("go"),+ // },{name: "code removal comment",original: `@@ -198,9 +182,6 @@ func TestStructuralReplacements(t *testing.T) {commit()return nil}`,- removals: []Removal{- 4,- },want: `func processUser(id int) error {validate(id)@@ -209,8 +190,7 @@ func TestStructuralReplacements(t *testing.T) {commit()return nil}`,- language: shared.TreeSitterLanguageGo,- parser: GetParserForLanguage("go"),+ ext: "go",},{name: "multiple code removal comments",@@ -235,9 +215,6 @@ func TestStructuralReplacements(t *testing.T) {commit()return nil}`,- removals: []Removal{- 4, 6,- },want: `func processUser(id int) error {validate(id)@@ -246,8 +223,7 @@ func TestStructuralReplacements(t *testing.T) {commit()return nil}`,- language: shared.TreeSitterLanguageGo,- parser: GetParserForLanguage("go"),+ ext: "go",},{name: "json update with reference comments",@@ -276,9 +252,7 @@ func TestStructuralReplacements(t *testing.T) {},// ... existing code ...}`,- references: []Reference{- 2, 10,- },+want: `{"name": "test-app","version": "1.0.0",@@ -295,8 +269,7 @@ func TestStructuralReplacements(t *testing.T) {"build": "webpack"}}`,- language: shared.TreeSitterLanguageJson,- parser: GetParserForLanguage("json"),+ ext: "json",},{name: "method replacement with context",@@ -336,9 +309,7 @@ func TestStructuralReplacements(t *testing.T) {// ... existing code ...}`,- references: []Reference{- 3, 14,- },+want: `class UserService {constructor() {@@ -354,7 +325,6 @@ func TestStructuralReplacements(t *testing.T) {return user}-async updateUser(id, data) {await db.update(id, data)this.cache.clear()@@ -365,8 +335,7 @@ func TestStructuralReplacements(t *testing.T) {this.cache.clear()}}`,- language: shared.TreeSitterLanguageJavascript,- parser: GetParserForLanguage("javascript"),+ ext: "js",},{name: "nested class methods update",@@ -406,9 +375,7 @@ func TestStructuralReplacements(t *testing.T) {// ... existing code ...}}`,- references: []Reference{- 5, 15,- },+want: `namespace Database {class Transaction {@@ -424,15 +391,13 @@ func TestStructuralReplacements(t *testing.T) {notifyCommit()}-rollback() {log.Info("rolling back transaction")rollbackTx()}}}`,- language: shared.TreeSitterLanguageTypescript,- parser: GetParserForLanguage("typescript"),+ ext: "ts",},{name: "update with trailing commas",@@ -457,9 +422,7 @@ func TestStructuralReplacements(t *testing.T) {},// ... existing code ...}`,- references: []Reference{- 3, 8,- },+want: `const handlers = {onStart: () => {@@ -473,8 +436,7 @@ func TestStructuralReplacements(t *testing.T) {console.log("finishing")},}`,- language: shared.TreeSitterLanguageJavascript,- parser: GetParserForLanguage("javascript"),+ ext: "js",},{name: "multiple structural updates",@@ -520,9 +482,7 @@ func TestStructuralReplacements(t *testing.T) {process.exit(1)}}`,- references: []Reference{- 11, 18,- },+want: `class Logger {constructor(prefix) {@@ -551,36 +511,7 @@ func TestStructuralReplacements(t *testing.T) {process.exit(1)}}`,- language: shared.TreeSitterLanguageJavascript,- parser: GetParserForLanguage("javascript"),- },- {- name: "updated variable assignment",- original: `- import logger from "logger";-- const a = 1;- const b = 2;- const c = 3;- `,- proposed: `- // ... existing code ...-- const a = 10;- const b = 2;- // ... existing code ...- `,- references: []Reference{2, 6},- anchorLines: map[int]int{4: 4},- want: `- import logger from "logger";-- const a = 10;- const b = 2;- const c = 3;- `,- language: shared.TreeSitterLanguageJavascript,- parser: GetParserForLanguage("javascript"),+ ext: "js",},{@@ -749,11 +680,7 @@ func TestStructuralReplacements(t *testing.T) {}}`,- language: shared.TreeSitterLanguageJson,- parser: GetParserForLanguage("json"),- references: []Reference{- 3, 24, 26,- },+ ext: "json",},{@@ -921,7 +848,6 @@ func TestStructuralReplacements(t *testing.T) {}}],-"keybindings": [{"command": "plandex.showFilePicker",@@ -939,7 +865,6 @@ func TestStructuralReplacements(t *testing.T) {]}},-"scripts": {"vscode:prepublish": "npm run package","compile": "webpack",@@ -971,212 +896,204 @@ func TestStructuralReplacements(t *testing.T) {}}`,- language: shared.TreeSitterLanguageJson,- parser: GetParserForLanguage("json"),- references: []Reference{- 3, 6, 19, 22,- },+ ext: "json",},- {- name: "scala complex structures",- original: `-package domain.service--import java.time.format.DateTimeFormatter--class MetricsService(- client: Client,- service: Service,- automation: Automation-)(- implicit context: Context-) extends LazyLogging- with BaseImplicits {-- def metrics(- ids: Seq[Id],- channels: Option[Seq[Channel]],- ): Future[Metrics] = {-- getMetrics(- ids,- channels,- Endpoint.Metrics- )- }-- def metrics2(- ids: Seq[Id],- channels: Option[Seq[Channel]],- ): Future[Metrics] = {-- getMetrics2(- ids,- channels,- Endpoint.Metrics- )- }- }-`,-- proposed: `-package domain.service--// ... existing code ...--class MetricsService(- // ... existing code ...-)(- implicit context: Context-) extends LazyLogging- with BaseImplicits {-- // ... existing code ...-- def update(authContext: AuthContext, id: String): Future[Done] = {- fallbacks.stub- .update(- updateRequest(- authContext = Some(authContext),- id = id- )- )- .map(_ => Done)- }-- // ... existing code ...- }-`,-- want: `-package domain.service--import java.time.format.DateTimeFormatter--class MetricsService(- client: Client,- service: Service,- automation: Automation-)(- implicit context: Context-) extends LazyLogging- with BaseImplicits {-- def metrics(- ids: Seq[Id],- channels: Option[Seq[Channel]],- ): Future[Metrics] = {-- getMetrics(- ids,- channels,- Endpoint.Metrics- )- }-- def update(authContext: AuthContext, id: String): Future[Done] = {- fallbacks.stub- .update(- updateRequest(- authContext = Some(authContext),- id = id- )- )- .map(_ => Done)- }-- def metrics2(- ids: Seq[Id],- channels: Option[Seq[Channel]],- ): Future[Metrics] = {-- getMetrics2(- ids,- channels,- Endpoint.Metrics- )- }- }-`,- references: []Reference{- 4, 7, 13, 26,- },- parser: GetParserForLanguage("scala"),- },-- {- name: "top-level ambiguous",- original: `- function someFunction() {- console.log("someFunction")- const res = await fetch("https://example.com")- processResponse(res)- return res- }-- function processResponse(res) {- console.log("processing response")- callSomeOtherFunction(res)- return res- }+ // {+ // name: "scala complex structures",+ // original: `+ // package domain.service++ // import java.time.format.DateTimeFormatter++ // class MetricsService(+ // client: Client,+ // service: Service,+ // automation: Automation+ // )(+ // implicit context: Context+ // ) extends LazyLogging+ // with BaseImplicits {++ // def metrics(+ // ids: Seq[Id],+ // channels: Option[Seq[Channel]],+ // ): Future[Metrics] = {++ // getMetrics(+ // ids,+ // channels,+ // Endpoint.Metrics+ // )+ // }++ // def metrics2(+ // ids: Seq[Id],+ // channels: Option[Seq[Channel]],+ // ): Future[Metrics] = {++ // getMetrics2(+ // ids,+ // channels,+ // Endpoint.Metrics+ // )+ // }+ // }+ // `,++ // proposed: `+ // package domain.service++ // // ... existing code ...++ // class MetricsService(+ // // ... existing code ...+ // )(+ // implicit context: Context+ // ) extends LazyLogging+ // with BaseImplicits {++ // // ... existing code ...++ // def update(authContext: AuthContext, id: String): Future[Done] = {+ // fallbacks.stub+ // .update(+ // updateRequest(+ // authContext = Some(authContext),+ // id = id+ // )+ // )+ // .map(_ => Done)+ // }++ // // ... existing code ...+ // }+ // `,++ // want: `+ // package domain.service++ // import java.time.format.DateTimeFormatter++ // class MetricsService(+ // client: Client,+ // service: Service,+ // automation: Automation+ // )(+ // implicit context: Context+ // ) extends LazyLogging+ // with BaseImplicits {++ // def metrics(+ // ids: Seq[Id],+ // channels: Option[Seq[Channel]],+ // ): Future[Metrics] = {++ // getMetrics(+ // ids,+ // channels,+ // Endpoint.Metrics+ // )+ // }++ // def update(authContext: AuthContext, id: String): Future[Done] = {+ // fallbacks.stub+ // .update(+ // updateRequest(+ // authContext = Some(authContext),+ // id = id+ // )+ // )+ // .map(_ => Done)+ // }++ // def metrics2(+ // ids: Seq[Id],+ // channels: Option[Seq[Channel]],+ // ): Future[Metrics] = {++ // getMetrics2(+ // ids,+ // channels,+ // Endpoint.Metrics+ // )+ // }+ // }+ // `,+ //+ // ext: "scala",+ // },++ // {+ // name: "top-level ambiguous",+ // original: `+ // function someFunction() {+ // console.log("someFunction")+ // const res = await fetch("https://example.com")+ // processResponse(res)+ // return res+ // }- function yetAnotherFunction() {- console.log("yetAnotherFunction")- }+ // function processResponse(res) {+ // console.log("processing response")+ // callSomeOtherFunction(res)+ // return res+ // }- function callSomething() {- console.log("callSomething")- await logSomething()- return "something"- }- `,- proposed: `- // ... existing code ...+ // function yetAnotherFunction() {+ // console.log("yetAnotherFunction")+ // }- function newFunction() {- console.log("newFunction")- const res = await callSomething()- return res- }+ // function callSomething() {+ // console.log("callSomething")+ // await logSomething()+ // return "something"+ // }+ // `,+ // proposed: `+ // // ... existing code ...++ // function newFunction() {+ // console.log("newFunction")+ // const res = await callSomething()+ // return res+ // }- // ... existing code ...- `,- references: []Reference{- 2, 10,- },- want: `- function someFunction() {- console.log("someFunction")- const res = await fetch("https://example.com")- processResponse(res)- return res- }+ // // ... existing code ...+ // `,+ //+ // want: `+ // function someFunction() {+ // console.log("someFunction")+ // const res = await fetch("https://example.com")+ // processResponse(res)+ // return res+ // }- function processResponse(res) {- console.log("processing response")- callSomeOtherFunction(res)- return res- }+ // function processResponse(res) {+ // console.log("processing response")+ // callSomeOtherFunction(res)+ // return res+ // }- function newFunction() {- console.log("newFunction")- const res = await callSomething()- return res- }+ // function newFunction() {+ // console.log("newFunction")+ // const res = await callSomething()+ // return res+ // }- function yetAnotherFunction() {- console.log("yetAnotherFunction")- }+ // function yetAnotherFunction() {+ // console.log("yetAnotherFunction")+ // }- function callSomething() {- console.log("callSomething")- await logSomething()- return "something"- }- `,- parser: GetParserForLanguage("javascript"),- },+ // function callSomething() {+ // console.log("callSomething")+ // await logSomething()+ // return "something"+ // }+ // `,+ // ext: "js",+ // },{name: "top-level with anchors",@@ -1224,9 +1141,7 @@ class MetricsService(// ... existing code ...`,- references: []Reference{- 2, 5, 15, 18,- },+want: `function someFunction() {console.log("someFunction")@@ -1258,7 +1173,7 @@ class MetricsService(return "something"}`,- parser: GetParserForLanguage("javascript"),+ ext: "js",},{@@ -1294,9 +1209,6 @@ class MetricsService(// ... existing code ...`,- references: []Reference{- 2, 5, 14,- },want: `func func1 () {fmt.Println("func1")@@ -1317,993 +1229,370 @@ class MetricsService(fmt.Println("func3")}`,- parser: GetParserForLanguage("go"),+ ext: "go",},-{- name: "slice error",- original: `package cmd+ name: "ambiguous location",+ original: `package routesimport (- "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"+ "fmt"+ "log"+ "net/http"+ "os"+ "path/filepath"+ "plandex-server/handlers"+ "plandex-server/hooks"++ "github.com/gorilla/mux")-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)+func AddHealthRoutes(r *mux.Router) {+ r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {+ _, apiErr := hooks.ExecHook(hooks.HealthCheck, hooks.HookParams{})+ if apiErr != nil {+ log.Printf("Error in health check hook: %v\n", apiErr)+ http.Error(w, apiErr.Msg, apiErr.Status)+ return+ }+ fmt.Fprint(w, "OK")+ })- 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")+ r.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {+ // Log the host+ host := r.Host+ log.Printf("Host header: %s", host)- 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")+ execPath, err := os.Executable()+ if err != nil {+ log.Fatal("Error getting current directory: ", err)+ }+ currentDir := filepath.Dir(execPath)- tellCmd.Flags().BoolVar(&tellAutoContext, "auto-context", false, "Load and manage context automatically")+ // get version from version.txt+ bytes, err := os.ReadFile(filepath.Join(currentDir, "..", "version.txt"))- 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")+ if err != nil {+ http.Error(w, "Error getting version", http.StatusInternalServerError)+ return+ }- 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)+ fmt.Fprint(w, string(bytes))+ })}-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- }+func AddApiRoutes(r *mux.Router) {+ addApiRoutes(r, "")+}- 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 AddApiRoutesWithPrefix(r *mux.Router, prefix string) {+ addApiRoutes(r, prefix)}-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)- }+func addApiRoutes(r *mux.Router, prefix string) {+ r.HandleFunc(prefix+"/accounts/email_verifications", handlers.CreateEmailVerificationHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/email_verifications/check_pin", handlers.CheckEmailPinHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/sign_in_codes", handlers.CreateSignInCodeHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/sign_in", handlers.SignInHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/sign_out", handlers.SignOutHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts", handlers.CreateAccountHandler).Methods("POST")- // Check if there's piped input- fileInfo, err := os.Stdin.Stat()- if err != nil {- term.OutputErrorAndExit("Failed to stat stdin: %v", err)- }+ r.HandleFunc(prefix+"/orgs/session", handlers.GetOrgSessionHandler).Methods("GET")+ r.HandleFunc(prefix+"/orgs", handlers.ListOrgsHandler).Methods("GET")+ r.HandleFunc(prefix+"/orgs", handlers.CreateOrgHandler).Methods("POST")- 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)- }+ r.HandleFunc(prefix+"/users", handlers.ListUsersHandler).Methods("GET")+ r.HandleFunc(prefix+"/orgs/users/{userId}", handlers.DeleteOrgUserHandler).Methods("DELETE")+ r.HandleFunc(prefix+"/orgs/roles", handlers.ListOrgRolesHandler).Methods("GET")- if prompt == "" && pipedData == "" {- prompt = getEditorPrompt()- } else if pipedData != "" {- if prompt != "" {- prompt = fmt.Sprintf("%s\n\n---\n\n%s", prompt, pipedData)- } else {- prompt = pipedData- }- }+ r.HandleFunc(prefix+"/invites", handlers.InviteUserHandler).Methods("POST")+ r.HandleFunc(prefix+"/invites/pending", handlers.ListPendingInvitesHandler).Methods("GET")+ r.HandleFunc(prefix+"/invites/accepted", handlers.ListAcceptedInvitesHandler).Methods("GET")+ r.HandleFunc(prefix+"/invites/all", handlers.ListAllInvitesHandler).Methods("GET")+ r.HandleFunc(prefix+"/invites/{inviteId}", handlers.DeleteInviteHandler).Methods("DELETE")- return prompt-}+ r.HandleFunc(prefix+"/projects", handlers.CreateProjectHandler).Methods("POST")+ r.HandleFunc(prefix+"/projects", handlers.ListProjectsHandler).Methods("GET")+ r.HandleFunc(prefix+"/projects/{projectId}/set_plan", handlers.ProjectSetPlanHandler).Methods("PUT")+ r.HandleFunc(prefix+"/projects/{projectId}/rename", handlers.RenameProjectHandler).Methods("PUT")-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)- }-}+ r.HandleFunc(prefix+"/projects/{projectId}/plans/current_branches", handlers.GetCurrentBranchByPlanIdHandler).Methods("POST")-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"-}+ r.HandleFunc(prefix+"/plans", handlers.ListPlansHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/archive", handlers.ListArchivedPlansHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/ps", handlers.ListPlansRunningHandler).Methods("GET")-func getEditorPrompt() string {- editor := os.Getenv("EDITOR")- if editor == "" {- editor = os.Getenv("VISUAL")- if editor == "" {- editor = defaultEditor- }- }+ r.HandleFunc(prefix+"/projects/{projectId}/plans", handlers.CreatePlanHandler).Methods("POST")- tempFile, err := os.CreateTemp(os.TempDir(), "plandex_prompt_*")- if err != nil {- term.OutputErrorAndExit("Failed to create temporary file: %v", err)- }+ r.HandleFunc(prefix+"/projects/{projectId}/plans", handlers.CreatePlanHandler).Methods("DELETE")- 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)- }+ r.HandleFunc(prefix+"/plans/{planId}", handlers.GetPlanHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}", handlers.DeletePlanHandler).Methods("DELETE")- 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)- }+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/tell", handlers.TellPlanHandler).Methods("POST")- bytes, err := os.ReadFile(tempFile.Name())- if err != nil {- term.OutputErrorAndExit("Error reading temporary file: %v", err)- }+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/respond_missing_file", handlers.RespondMissingFileHandler).Methods("POST")- prompt := string(bytes)+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/build", handlers.BuildPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/connect", handlers.ConnectPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/stop", handlers.StopPlanHandler).Methods("DELETE")- err = os.Remove(tempFile.Name())- if err != nil {- term.OutputErrorAndExit("Error removing temporary file: %v", err)- }+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/current_plan", handlers.CurrentPlanHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/apply", handlers.ApplyPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/archive", handlers.ArchivePlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/unarchive", handlers.UnarchivePlanHandler).Methods("PATCH")- prompt = strings.TrimPrefix(prompt, strings.TrimSpace(instructions))- prompt = strings.TrimSpace(prompt)+ r.HandleFunc(prefix+"/plans/{planId}/rename", handlers.RenamePlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/reject_all", handlers.RejectAllChangesHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/reject_file", handlers.RejectFileHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/reject_files", handlers.RejectFilesHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/diffs", handlers.GetPlanDiffsHandler).Methods("GET")- return prompt+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.ListContextHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.LoadContextHandler).Methods("POST")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context/{contextId}/body", handlers.GetContextBodyHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.UpdateContextHandler).Methods("PUT")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.DeleteContextHandler).Methods("DELETE")-}+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/convo", handlers.ListConvoHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/rewind", handlers.RewindPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/logs", handlers.ListLogsHandler).Methods("GET")-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")- }-}+ r.HandleFunc(prefix+"/plans/{planId}/branches", handlers.ListBranchesHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/branches/{branch}", handlers.DeleteBranchHandler).Methods("DELETE")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/branches", handlers.CreateBranchHandler).Methods("POST")-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- }+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/settings", handlers.GetSettingsHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/settings", handlers.UpdateSettingsHandler).Methods("PUT")- 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)- }-}+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/status", handlers.GetPlanStatusHandler).Methods("GET")-// AutoDebugValue implements the flag.Value interface-type autoDebugValue struct {- value *int-}+ r.HandleFunc(prefix+"/custom_models", handlers.ListCustomModelsHandler).Methods("GET")+ r.HandleFunc(prefix+"/custom_models", handlers.CreateCustomModelHandler).Methods("POST")+ r.HandleFunc(prefix+"/custom_models/{modelId}", handlers.DeleteAvailableModelHandler).Methods("DELETE")-func newAutoDebugValue(p *int) *autoDebugValue {- *p = 0 // Default to 0 (disabled)- return &autoDebugValue{p}-}+ r.HandleFunc(prefix+"/model_sets", handlers.ListModelPacksHandler).Methods("GET")+ r.HandleFunc(prefix+"/model_sets", handlers.CreateModelPackHandler).Methods("POST")+ r.HandleFunc(prefix+"/model_sets/{setId}", handlers.DeleteModelPackHandler).Methods("DELETE")-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-}+ r.HandleFunc(prefix+"/default_settings", handlers.GetDefaultSettingsHandler).Methods("GET")+ r.HandleFunc(prefix+"/default_settings", handlers.UpdateDefaultSettingsHandler).Methods("PUT")-func (f *autoDebugValue) String() string {- if f.value == nil {- return "0"- }- return strconv.Itoa(*f.value)-}+ r.HandleFunc(prefix+"/file_map", handlers.GetFileMapHandler).Methods("POST")-func (f *autoDebugValue) Type() string {- return "int"+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/auto_load_context", handlers.AutoLoadContextHandler).Methods("POST")}`,- proposed: `-// ... existing code ...+ proposed: `// ... existing code ...-func doTell(cmd *cobra.Command, args []string) {- auth.MustResolveAuthWithOrg()- lib.MustResolveProject()+r.HandleFunc(prefix+"/plans/{planId}/rename", handlers.RenamePlanHandler).Methods("PATCH")- if lib.CurrentPlanId == "" {- term.OutputNoCurrentPlanErrorAndExit()- }+r.HandleFunc(prefix+"/plans/{planId}/config", handlers.GetPlanConfigHandler).Methods("GET")+r.HandleFunc(prefix+"/plans/{planId}/config", handlers.UpdatePlanConfigHandler).Methods("PUT")+r.HandleFunc(prefix+"/config/default", handlers.GetDefaultConfigHandler).Methods("GET")+r.HandleFunc(prefix+"/config/default", handlers.UpdateDefaultConfigHandler).Methods("PUT")- var apiKeys map[string]string- if !auth.Current.IntegratedModelsMode {- apiKeys = lib.MustVerifyApiKeys()- }+r.HandleFunc(prefix+"/plans/{planId}/{branch}/reject_all", handlers.RejectAllChangesHandler).Methods("PATCH")+// Plandex: removed code+r.HandleFunc(prefix+"/plans/{planId}/{branch}/diffs", handlers.GetPlanDiffsHandler).Methods("GET")- plan, err := api.Client.GetPlan(lib.CurrentPlanId)- if err != nil {- term.HandleApiError(err)- return- }+r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.ListContextHandler).Methods("GET")- // 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- }- }+// ... existing code ...- prompt := getTellPrompt(args)+ r.HandleFunc(prefix+"/plans/{planId}/rename", handlers.RenamePlanHandler).Methods("PATCH")- // ... existing code ...-}--// ... existing code ...`,- references: []Reference{2, 48, 51},- want: `package cmd++ want: `package routesimport (- "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"+ "fmt"+ "log"+ "net/http"+ "os"+ "path/filepath"+ "plandex-server/handlers"+ "plandex-server/hooks"++ "github.com/gorilla/mux")-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 AddHealthRoutes(r *mux.Router) {+ r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {+ _, apiErr := hooks.ExecHook(hooks.HealthCheck, hooks.HookParams{})+ if apiErr != nil {+ log.Printf("Error in health check hook: %v\n", apiErr)+ http.Error(w, apiErr.Msg, apiErr.Status)+ return+ }+ fmt.Fprint(w, "OK")+ })-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- }+ r.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {+ // Log the host+ host := r.Host+ log.Printf("Host header: %s", host)- 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)- }-}+ execPath, err := os.Executable()+ if err != nil {+ log.Fatal("Error getting current directory: ", err)+ }+ currentDir := filepath.Dir(execPath)-// AutoDebugValue implements the flag.Value interface-type autoDebugValue struct {- value *int-}+ // get version from version.txt+ bytes, err := os.ReadFile(filepath.Join(currentDir, "..", "version.txt"))-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-}+ if err != nil {+ http.Error(w, "Error getting version", http.StatusInternalServerError)+ return+ }-func (f *autoDebugValue) String() string {- if f.value == nil {- return "0"- }- return strconv.Itoa(*f.value)+ fmt.Fprint(w, string(bytes))+ })}-func (f *autoDebugValue) Type() string {- return "int"+func AddApiRoutes(r *mux.Router) {+ addApiRoutes(r, "")}-`,- 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)- },+func AddApiRoutesWithPrefix(r *mux.Router, prefix string) {+ addApiRoutes(r, prefix)}-// 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)+func addApiRoutes(r *mux.Router, prefix string) {+ r.HandleFunc(prefix+"/accounts/email_verifications", handlers.CreateEmailVerificationHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/email_verifications/check_pin", handlers.CheckEmailPinHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/sign_in_codes", handlers.CreateSignInCodeHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/sign_in", handlers.SignInHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts/sign_out", handlers.SignOutHandler).Methods("POST")+ r.HandleFunc(prefix+"/accounts", handlers.CreateAccountHandler).Methods("POST")- // output the error message to stderr- term.OutputSimpleError("Error: %v", err)+ r.HandleFunc(prefix+"/orgs/session", handlers.GetOrgSessionHandler).Methods("GET")+ r.HandleFunc(prefix+"/orgs", handlers.ListOrgsHandler).Methods("GET")+ r.HandleFunc(prefix+"/orgs", handlers.CreateOrgHandler).Methods("POST")- fmt.Println()+ r.HandleFunc(prefix+"/users", handlers.ListUsersHandler).Methods("GET")+ r.HandleFunc(prefix+"/orgs/users/{userId}", handlers.DeleteOrgUserHandler).Methods("DELETE")+ r.HandleFunc(prefix+"/orgs/roles", handlers.ListOrgRolesHandler).Methods("GET")- 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()+ r.HandleFunc(prefix+"/invites", handlers.InviteUserHandler).Methods("POST")+ r.HandleFunc(prefix+"/invites/pending", handlers.ListPendingInvitesHandler).Methods("GET")+ r.HandleFunc(prefix+"/invites/accepted", handlers.ListAcceptedInvitesHandler).Methods("GET")+ r.HandleFunc(prefix+"/invites/all", handlers.ListAllInvitesHandler).Methods("GET")+ r.HandleFunc(prefix+"/invites/{inviteId}", handlers.DeleteInviteHandler).Methods("DELETE")- 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)- },- }+ r.HandleFunc(prefix+"/projects", handlers.CreateProjectHandler).Methods("POST")+ r.HandleFunc(prefix+"/projects", handlers.ListProjectsHandler).Methods("GET")+ r.HandleFunc(prefix+"/projects/{projectId}/set_plan", handlers.ProjectSetPlanHandler).Methods("PUT")+ r.HandleFunc(prefix+"/projects/{projectId}/rename", handlers.RenameProjectHandler).Methods("PUT")- RootCmd.AddCommand(helpCmd)+ r.HandleFunc(prefix+"/projects/{projectId}/plans/current_branches", handlers.GetCurrentBranchByPlanIdHandler).Methods("POST")- // add an --all/-a flag- helpCmd.Flags().BoolVarP(&helpShowAll, "all", "a", false, "Show all commands")-}-`,- proposed: `// ... existing code ...+ r.HandleFunc(prefix+"/plans", handlers.ListPlansHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/archive", handlers.ListArchivedPlansHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/ps", handlers.ListPlansRunningHandler).Methods("GET")-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)- },- }+ r.HandleFunc(prefix+"/projects/{projectId}/plans", handlers.CreatePlanHandler).Methods("POST")- RootCmd.AddCommand(helpCmd)+ r.HandleFunc(prefix+"/projects/{projectId}/plans", handlers.CreatePlanHandler).Methods("DELETE")- // add an --all/-a flag- helpCmd.Flags().BoolVarP(&helpShowAll, "all", "a", false, "Show all commands")-}+ r.HandleFunc(prefix+"/plans/{planId}", handlers.GetPlanHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}", handlers.DeletePlanHandler).Methods("DELETE")-// ... existing code ...-`,- references: []Reference{1, 20},- want: `package cmd+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/tell", handlers.TellPlanHandler).Methods("POST")-import (- "fmt"- "os"- "plandex/term"+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/respond_missing_file", handlers.RespondMissingFileHandler).Methods("POST")- "github.com/fatih/color"- "github.com/spf13/cobra"-)+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/build", handlers.BuildPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/connect", handlers.ConnectPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/stop", handlers.StopPlanHandler).Methods("DELETE")-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)- },-}+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/current_plan", handlers.CurrentPlanHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/apply", handlers.ApplyPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/archive", handlers.ArchivePlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/unarchive", handlers.UnarchivePlanHandler).Methods("PATCH")-// 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)+ r.HandleFunc(prefix+"/plans/{planId}/rename", handlers.RenamePlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/reject_all", handlers.RejectAllChangesHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/reject_file", handlers.RejectFileHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/reject_files", handlers.RejectFilesHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/diffs", handlers.GetPlanDiffsHandler).Methods("GET")- // output the error message to stderr- term.OutputSimpleError("Error: %v", err)+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.ListContextHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.LoadContextHandler).Methods("POST")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context/{contextId}/body", handlers.GetContextBodyHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.UpdateContextHandler).Methods("PUT")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/context", handlers.DeleteContextHandler).Methods("DELETE")- fmt.Println()+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/convo", handlers.ListConvoHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/rewind", handlers.RewindPlanHandler).Methods("PATCH")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/logs", handlers.ListLogsHandler).Methods("GET")- 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()+ r.HandleFunc(prefix+"/plans/{planId}/branches", handlers.ListBranchesHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/branches/{branch}", handlers.DeleteBranchHandler).Methods("DELETE")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/branches", handlers.CreateBranchHandler).Methods("POST")- 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()+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/settings", handlers.GetSettingsHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/settings", handlers.UpdateSettingsHandler).Methods("PUT")- os.Exit(1)+ r.HandleFunc(prefix+"/plans/{planId}/config", handlers.GetConfigHandler).Methods("GET")+ r.HandleFunc(prefix+"/plans/{planId}/config", handlers.UpdateConfigHandler).Methods("PUT")+ r.HandleFunc(prefix+"/default_config", handlers.GetDefaultConfigHandler).Methods("GET")+ r.HandleFunc(prefix+"/default_config", handlers.UpdateDefaultConfigHandler).Methods("PUT")++ r.HandleFunc(prefix+"/plans/{planId}/{branch}/status", handlers.GetPlanStatusHandler).Methods("GET")- }-}+ r.HandleFunc(prefix+"/custom_models", handlers.ListCustomModelsHandler).Methods("GET")+ r.HandleFunc(prefix+"/custom_models", handlers.CreateCustomModelHandler).Methods("POST")+ r.HandleFunc(prefix+"/custom_models/{modelId}", handlers.DeleteAvailableModelHandler).Methods("DELETE")-func run(cmd *cobra.Command, args []string) {-}+ r.HandleFunc(prefix+"/model_sets", handlers.ListModelPacksHandler).Methods("GET")+ r.HandleFunc(prefix+"/model_sets", handlers.CreateModelPackHandler).Methods("POST")+ r.HandleFunc(prefix+"/model_sets/{setId}", handlers.DeleteModelPackHandler).Methods("DELETE")-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)- },- }+ r.HandleFunc(prefix+"/default_settings", handlers.GetDefaultSettingsHandler).Methods("GET")+ r.HandleFunc(prefix+"/default_settings", handlers.UpdateDefaultSettingsHandler).Methods("PUT")- RootCmd.AddCommand(helpCmd)+ r.HandleFunc(prefix+"/file_map", handlers.GetFileMapHandler).Methods("POST")- // add an --all/-a flag- helpCmd.Flags().BoolVarP(&helpShowAll, "all", "a", false, "Show all commands")+ r.HandleFunc(prefix+"/plans/{planId}/{branch}/auto_load_context", handlers.AutoLoadContextHandler).Methods("POST")}`,- parser: GetParserForLanguage("go"),},}for _, tt := range tests {- // if tt.name != "extraneous references, no change" {- // continue- // }+t.Run(tt.name, func(t *testing.T) {- anchorLines := map[int]int{}- if tt.anchorLines != nil {- anchorLines = tt.anchorLines- }-- got, err := ApplyChanges(- context.Background(),- tt.language,- tt.parser,+ // got, err := ApplyChanges(+ // context.Background(),+ // tt.language,+ // tt.parser,+ // tt.original,+ // tt.proposed,+ // tt.references,+ // tt.removals,+ // anchorLines,+ // )++ // originalLines := strings.Split(tt.original, "\n")+ // proposedLines := strings.Split(tt.proposed, "\n")++ res := ApplyChanges(tt.original,tt.proposed,- tt.references,- tt.removals,- anchorLines,+ "",+ false,)+fmt.Println()- fmt.Println(tt.name)- fmt.Println(got)+ fmt.Println("NAME:", tt.name)+ fmt.Println(res.NewFile)fmt.Println()- assert.NoError(t, err)-- // 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)+ assert.Empty(t, res.NeedsVerifyReasons)+ assert.Equal(t, tt.want, res.NewFile)})}}diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.goindex be60cc86..5f15a401 100644--- a/app/server/types/active_plan.go+++ b/app/server/types/active_plan.go@@ -260,9 +260,9 @@ func (ap *ActivePlan) Stream(msg shared.StreamMessage) {if now.After(ap.lastStreamMessageSent) {ap.lastStreamMessageSent = now}- ap.streamMu.Unlock()if msg.Type == shared.StreamMessageFinished {+ ap.streamMu.Unlock()// wait for the finish message to be sent then send the done signallog.Println("ActivePlan.Stream: waiting 50ms before sending done signal")time.Sleep(50 * time.Millisecond)diff --git a/app/server/types/anchors.go b/app/server/types/anchors.godeleted file mode 100644index 000df8f0..00000000--- a/app/server/types/anchors.go+++ /dev/null@@ -1,39 +0,0 @@-package types--import "encoding/xml"--type SemanticAnchorsTag struct {- XMLName xml.Name `xml:"PlandexSemanticAnchors"`- Anchors []AnchorTag `xml:"Anchor"`-}--type AnchorTag struct {- // Reasoning string `xml:"reasoning,attr"` // better to leave this out since it can cause problems with unmarshalling and isn't used after parsing- ProposedLine string `xml:"proposedLine,attr"`- OriginalLine string `xml:"originalLine,attr"`-}--type SummaryTag struct {- XMLName xml.Name `xml:"PlandexSummary"`- Content string `xml:",chardata"`-}--type ReferencesTag struct {- XMLName xml.Name `xml:"PlandexReferences"`- References []ReferenceTag `xml:"Reference"`-}--type ReferenceTag struct {- Comment string `xml:"comment,attr"`- ProposedLine string `xml:"proposedLine,attr"`-}--type RemovalsTag struct {- XMLName xml.Name `xml:"PlandexRemovals"`- Removals []RemovalTag `xml:"Removal"`-}--type RemovalTag struct {- Comment string `xml:"comment,attr"`- ProposedLine string `xml:"proposedLine,attr"`-}diff --git a/app/server/types/reply.go b/app/server/types/reply.goindex f6c97608..337d91b2 100644--- a/app/server/types/reply.go+++ b/app/server/types/reply.go@@ -161,7 +161,7 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {// log.Println("Current file path is empty--checking for possible file path...")var gotPath string- if lineMaybeHasFilePath(prevFullLineTrimmed) {+ if LineMaybeHasFilePath(prevFullLineTrimmed) {gotPath = extractFilePath(prevFullLineTrimmed)} else {// log.Println("No possible file path detected.", strconv.Quote(prevFullLineTrimmed))@@ -222,7 +222,7 @@ func (r *ReplyParser) GetReplyBeforePath(path string) string {var idx intfor i := len(r.lines) - 1; i >= 0; i-- {line := r.lines[i]- if lineMaybeHasFilePath(line) && path == extractFilePath(line) {+ if LineMaybeHasFilePath(line) && path == extractFilePath(line) {idx = ibreak}@@ -237,7 +237,7 @@ func (r *ReplyParser) GetReplyForMissingFile() string {var idx intfor i := len(r.lines) - 1; i >= 0; i-- {line := r.lines[i]- if lineMaybeHasFilePath(line) && path == extractFilePath(line) {+ if LineMaybeHasFilePath(line) && path == extractFilePath(line) {idx = ibreak}@@ -256,7 +256,7 @@ func (r *ReplyParser) GetReplyForMissingFile() string {return strings.Join(r.lines[:idx], "\n") + "\n"}-func lineMaybeHasFilePath(line string) bool {+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, ":"))if couldBe {