Skip to content

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

Improvements to code block explanation format. Remove rewriting of explanation in build validation/correction step. Fix for context outdated/updated inconsistency with file maps. Don't show context outdated table when auto-confirming, just show updated context.

Dane Schneider0ca9f8417edf
diff --git a/app/cli/cmd/build.go b/app/cli/cmd/build.go
index a17034ab..afff1354 100644
--- a/app/cli/cmd/build.go
+++ b/app/cli/cmd/build.go
@@ -47,7 +47,8 @@ func build(cmd *cobra.Command, args []string) {
CurrentBranch: lib.CurrentBranch,
ApiKeys: apiKeys,
CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
- return lib.CheckOutdatedContextWithOutput(false, tellAutoContext, maybeContexts)
+ auto := autoConfirm || tellAutoApply || tellAutoContext
+ return lib.CheckOutdatedContextWithOutput(auto, auto, maybeContexts)
},
}, tellBg)
diff --git a/app/cli/cmd/chat.go b/app/cli/cmd/chat.go
index 7521a5c7..71d99346 100644
--- a/app/cli/cmd/chat.go
+++ b/app/cli/cmd/chat.go
@@ -54,7 +54,8 @@ func doChat(cmd *cobra.Command, args []string) {
CurrentBranch: lib.CurrentBranch,
ApiKeys: apiKeys,
CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
- return lib.CheckOutdatedContextWithOutput(false, tellAutoContext, maybeContexts)
+ auto := autoConfirm || tellAutoApply || tellAutoContext
+ return lib.CheckOutdatedContextWithOutput(auto, auto, maybeContexts)
},
}, prompt, plan_exec.TellFlags{
IsChatOnly: true,
diff --git a/app/cli/cmd/continue.go b/app/cli/cmd/continue.go
index 1c39a5b1..7680a27b 100644
--- a/app/cli/cmd/continue.go
+++ b/app/cli/cmd/continue.go
@@ -40,7 +40,9 @@ func doContinue(cmd *cobra.Command, args []string) {
CurrentBranch: lib.CurrentBranch,
ApiKeys: apiKeys,
CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
- return lib.CheckOutdatedContextWithOutput(false, tellAutoContext, maybeContexts)
+ auto := autoConfirm || tellAutoApply || tellAutoContext
+
+ return lib.CheckOutdatedContextWithOutput(auto, auto, maybeContexts)
},
}, "", plan_exec.TellFlags{
TellBg: tellBg,
diff --git a/app/cli/cmd/debug.go b/app/cli/cmd/debug.go
index 0277adbb..5fbaf2eb 100644
--- a/app/cli/cmd/debug.go
+++ b/app/cli/cmd/debug.go
@@ -123,7 +123,7 @@ func doDebug(cmd *cobra.Command, args []string) {
CurrentBranch: lib.CurrentBranch,
ApiKeys: apiKeys,
CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
- return lib.CheckOutdatedContextWithOutput(false, true, maybeContexts)
+ return lib.CheckOutdatedContextWithOutput(true, true, maybeContexts)
},
}, prompt, plan_exec.TellFlags{IsUserDebug: true})
diff --git a/app/cli/cmd/diffs.go b/app/cli/cmd/diffs.go
index 5f1199b0..dbcb4ffe 100644
--- a/app/cli/cmd/diffs.go
+++ b/app/cli/cmd/diffs.go
@@ -18,7 +18,8 @@ import (
// var plainTextOutput bool
var showDiffUi bool
-var diffUiSideBySide bool
+var diffUiSideBySide = true
+var diffUiLineByLine bool
var diffsCmd = &cobra.Command{
Use: "diff",
@@ -32,7 +33,8 @@ func init() {
diffsCmd.Flags().BoolVarP(&plainTextOutput, "plain", "p", false, "Output diffs in plain text with no ANSI codes")
diffsCmd.Flags().BoolVar(&showDiffUi, "ui", false, "Show diffs in a browser UI")
- diffsCmd.Flags().BoolVarP(&diffUiSideBySide, "side-by-side", "s", false, "Show diffs UI in side-by-side view")
+ diffsCmd.Flags().BoolVarP(&diffUiSideBySide, "side", "s", true, "Show diffs UI in side-by-side view")
+ diffsCmd.Flags().BoolVarP(&diffUiLineByLine, "line", "l", false, "Show diffs UI in line-by-line view")
}
func diffs(cmd *cobra.Command, args []string) {
@@ -53,6 +55,8 @@ func diffs(cmd *cobra.Command, args []string) {
outputFormat := "line-by-line"
if diffUiSideBySide {
outputFormat = "side-by-side"
+ } else if diffUiLineByLine {
+ outputFormat = "line-by-line"
}
// Properly escape the diff content for JavaScript
diff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.go
index 0e4e0142..725d778a 100644
--- a/app/cli/cmd/tell.go
+++ b/app/cli/cmd/tell.go
@@ -61,7 +61,8 @@ func doTell(cmd *cobra.Command, args []string) {
CurrentBranch: lib.CurrentBranch,
ApiKeys: apiKeys,
CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
- return lib.CheckOutdatedContextWithOutput(autoConfirm, autoConfirm || tellAutoApply || tellAutoContext, maybeContexts)
+ auto := autoConfirm || tellAutoApply || tellAutoContext
+ return lib.CheckOutdatedContextWithOutput(auto, auto, maybeContexts)
},
}, prompt, plan_exec.TellFlags{
TellBg: tellBg,
diff --git a/app/cli/lib/context_update.go b/app/cli/lib/context_update.go
index 3a7098ea..ddb9840b 100644
--- a/app/cli/lib/context_update.go
+++ b/app/cli/lib/context_update.go
@@ -93,12 +93,14 @@ func CheckOutdatedContextWithOutput(quiet, autoConfirm bool, maybeContexts []*sh
phrase = "has been"
}
- term.StopSpinner()
+ if !quiet {
+ term.StopSpinner()
- color.New(term.ColorHiCyan, color.Bold).Printf("%s in context %s modified 👇\n\n", msg, phrase)
+ color.New(term.ColorHiCyan, color.Bold).Printf("%s in context %s modified 👇\n\n", msg, phrase)
- tableString := tableForContextOutdated(outdatedRes.UpdatedContexts, outdatedRes.TokenDiffsById)
- fmt.Println(tableString)
+ tableString := tableForContextOutdated(outdatedRes.UpdatedContexts, outdatedRes.TokenDiffsById)
+ fmt.Println(tableString)
+ }
}
if len(outdatedRes.RemovedContexts) > 0 {
@@ -138,12 +140,14 @@ func CheckOutdatedContextWithOutput(quiet, autoConfirm bool, maybeContexts []*sh
phrase = "has been"
}
- term.StopSpinner()
+ if !quiet {
+ term.StopSpinner()
- color.New(term.ColorHiCyan, color.Bold).Printf("%s in context %s removed 👇\n\n", msg, phrase)
+ color.New(term.ColorHiCyan, color.Bold).Printf("%s in context %s removed 👇\n\n", msg, phrase)
- tableString := tableForContextOutdated(outdatedRes.RemovedContexts, outdatedRes.TokenDiffsById)
- fmt.Println(tableString)
+ tableString := tableForContextOutdated(outdatedRes.RemovedContexts, outdatedRes.TokenDiffsById)
+ fmt.Println(tableString)
+ }
}
confirmed := autoConfirm
@@ -465,19 +469,18 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool, maybeContexts []*shared.C
updatedParts[path] = body
prevTokens := context.MapTokens[path]
- numTokens := shared.GetNumTokensEstimate(body)
-
- // fmt.Println("path", path, "numTokens", numTokens, "prevTokens", prevTokens)
+ numTokens := mapRes.MapBodies.TokenEstimateForPath(path)
- tokenDiffsById[context.Id] += numTokens - prevTokens
+ if numTokens != prevTokens {
+ tokenDiffsById[context.Id] += numTokens - prevTokens
+ }
}
-
- // test this
}
if len(removedMapPaths) > 0 {
for _, path := range removedMapPaths {
delete(updatedParts, path)
+ tokenDiffsById[context.Id] -= context.MapTokens[path]
}
}
@@ -644,7 +647,6 @@ func tableForContextOutdated(updatedContexts []*shared.Context, tokenDiffsById m
for _, context := range updatedContexts {
t, icon := context.TypeAndIcon()
diff := tokenDiffsById[context.Id]
-
diffStr := "+" + strconv.Itoa(diff)
tableColor := tablewriter.FgHiGreenColor
diff --git a/app/cli/plan_exec/apply_exec.go b/app/cli/plan_exec/apply_exec.go
index abd4df75..8812d631 100644
--- a/app/cli/plan_exec/apply_exec.go
+++ b/app/cli/plan_exec/apply_exec.go
@@ -56,7 +56,7 @@ func GetOnApplyExecFail(flags lib.ApplyFlags) types.OnApplyExecFailFn {
CurrentBranch: lib.CurrentBranch,
ApiKeys: apiKeys,
CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool, error) {
- return lib.CheckOutdatedContextWithOutput(false, true, maybeContexts)
+ return lib.CheckOutdatedContextWithOutput(true, true, maybeContexts)
},
}, prompt, TellFlags{IsApplyDebug: true, ExecEnabled: true})
diff --git a/app/cli/term/format.go b/app/cli/term/format.go
index 88416e27..b97b821f 100644
--- a/app/cli/term/format.go
+++ b/app/cli/term/format.go
@@ -18,6 +18,7 @@ func GetMarkdown(input string) (string, error) {
// detect background color and pick either the default dark or light theme
glamour.WithAutoStyle(),
glamour.WithWordWrap(min(width, 80)),
+ glamour.WithPreservedNewLines(),
)
out, err := r.RenderBytes(inputBytes)
diff --git a/app/server/db/context_helpers.go b/app/server/db/context_helpers.go
index f533f0ee..5ea352f3 100644
--- a/app/server/db/context_helpers.go
+++ b/app/server/db/context_helpers.go
@@ -686,7 +686,7 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
maxTokens := settings.GetPlannerEffectiveMaxTokens()
totalTokens := branch.ContextTokens
- tokensDiff := 0
+ aggregateTokensDiff := 0
tokenDiffsById := make(map[string]int)
var contextsById map[string]*Context
@@ -731,8 +731,6 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
var mu sync.Mutex
errCh := make(chan error)
- log.Printf("updating %d outdated contexts\n", len(*req))
-
for id, params := range *req {
go func(id string, params *shared.UpdateContextParams) {
@@ -776,7 +774,7 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
tokenDiff := updateNumTokens - context.NumTokens
tokenDiffsById[id] = tokenDiff
- tokensDiff += tokenDiff
+ aggregateTokensDiff += tokenDiff
totalTokens += tokenDiff
context.NumTokens = updateNumTokens
}
@@ -803,21 +801,9 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
}
}
- updateRes := &shared.ContextUpdateResult{
- UpdatedContexts: updatedContexts,
- TokenDiffsById: tokenDiffsById,
- TokensDiff: tokensDiff,
- TotalTokens: totalTokens,
- NumFiles: numFiles,
- NumUrls: numUrls,
- NumTrees: numTrees,
- NumMaps: numMaps,
- MaxTokens: maxTokens,
- }
-
if totalTokens > maxTokens {
return &shared.UpdateContextResponse{
- TokensAdded: tokensDiff,
+ TokensAdded: aggregateTokensDiff,
TotalTokens: totalTokens,
MaxTokens: maxTokens,
MaxTokensExceeded: true,
@@ -850,9 +836,6 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
if context.ContextType == shared.ContextMapType {
oldNumTokens := context.NumTokens
-
- // log.Println("Updating map context", id, "oldNumTokens", oldNumTokens)
-
for path, part := range params.MapBodies {
if context.MapParts == nil {
context.MapParts = make(shared.FileMapBodies)
@@ -864,10 +847,12 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
context.MapTokens = make(map[string]int)
}
+ // prevNumTokens := context.MapTokens[path]
+
context.MapParts[path] = part
context.MapShas[path] = params.InputShas[path]
- numTokens := shared.GetNumTokensEstimate(part)
+ numTokens := params.MapBodies.TokenEstimateForPath(path)
context.MapTokens[path] = numTokens
}
@@ -879,21 +864,13 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
context.Body = context.MapParts.CombinedMap()
newNumTokens := shared.GetNumTokensEstimate(context.Body)
-
- // log.Println("Updated map context", id, "newNumTokens", newNumTokens)
-
tokenDiff := newNumTokens - oldNumTokens
- // log.Println("Updated map context", id, "tokenDiff", tokenDiff)
-
+ mu.Lock()
tokenDiffsById[id] = tokenDiff
- tokensDiff += tokenDiff
-
- // log.Println("Updated map context", id, "tokensDiff", tokensDiff)
-
+ aggregateTokensDiff += tokenDiff
totalTokens += tokenDiff
-
- // log.Println("Updated map context", id, "totalTokens", totalTokens)
+ mu.Unlock()
context.NumTokens = newNumTokens
} else {
@@ -915,10 +892,6 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
// log.Println("stored context", id)
// log.Println()
- if context.ContextType == shared.ContextMapType {
-
- }
-
errCh <- nil
}(id, params)
}
@@ -930,7 +903,19 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
}
}
- err = AddPlanContextTokens(planId, branchName, tokensDiff)
+ updateRes := &shared.ContextUpdateResult{
+ UpdatedContexts: updatedContexts,
+ TokenDiffsById: tokenDiffsById,
+ TokensDiff: aggregateTokensDiff,
+ TotalTokens: totalTokens,
+ NumFiles: numFiles,
+ NumUrls: numUrls,
+ NumTrees: numTrees,
+ NumMaps: numMaps,
+ MaxTokens: maxTokens,
+ }
+
+ err = AddPlanContextTokens(planId, branchName, aggregateTokensDiff)
if err != nil {
return nil, fmt.Errorf("error adding plan context tokens: %v", err)
}
@@ -938,7 +923,7 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
commitMsg := shared.SummaryForUpdateContext(updateRes) + "\n\n" + shared.TableForContextUpdate(updateRes)
return &shared.LoadContextResponse{
- TokensAdded: tokensDiff,
+ TokensAdded: aggregateTokensDiff,
TotalTokens: totalTokens,
Msg: commitMsg,
}, nil
diff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.go
index 5587b61b..b4945829 100644
--- a/app/server/model/plan/build_structured_edits.go
+++ b/app/server/model/plan/build_structured_edits.go
@@ -89,6 +89,8 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
isValid := len(applyRes.NeedsVerifyReasons) == 0 && len(syntaxErrors) == 0
+ log.Printf("buildStructuredEdits - %s - initial isValid: %t\n", filePath, isValid)
+
var buildStage BuildStage = BuildStageInitial
for !isValid && int(buildStage) <= int(BuildStageValidateAndCorrect) {
diff --git a/app/server/model/prompts/build.go b/app/server/model/prompts/build.go
index d0c82e62..3ad2ec9f 100644
--- a/app/server/model/prompts/build.go
+++ b/app/server/model/prompts/build.go
@@ -32,15 +32,15 @@ func GetValidateEditsPrompt(params ValidateEditsPromptParams) string {
var s string
- if fullCorrection {
- s += fmt.Sprintf(`
-[Explanation Rules]
+ // if fullCorrection {
+ // s += fmt.Sprintf(`
+ // [Explanation Rules]
-%s
+ // %s
-[End Explanation Rules]
-`, ChangeExplanationPrompt)
- }
+ // [End Explanation Rules]
+ // `, ChangeExplanationPrompt)
+ // }
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)
@@ -69,7 +69,7 @@ func GetValidateEditsPrompt(params ValidateEditsPromptParams) string {
s += EditsValidateFullPrompt
}
- s += EditsExplanationPrompt
+ // s += EditsExplanationPrompt
s += ReferencesPrompt
s += EditsFindReplacePrompt
}
@@ -124,21 +124,21 @@ const EditsValidateFullPrompt = EditsValidateBasePrompt + EditsValidateCorrectPa
const EditsValidateOnlyPrompt = EditsValidateBasePrompt + EditsValidateCorrectPattern + EditsValidateOnlyIncorrectPattern
-const EditsExplanationPrompt = `
-- Next, in an '## Evaluate Explanation' section, examine the proposed changes explanation and determine whether it is correct. Did it follow all the rules in the [Explanation 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).
+// const EditsExplanationPrompt = `
+// - Next, in an '## Evaluate Explanation' section, examine the proposed changes explanation and determine whether it is correct. Did it follow all the rules in the [Explanation Rules] section?
-- If the proposed changes explanation is *not* correct, you must rewrite the proposed changes explanation to correctly follow *all* the rules in the [Explanation 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.
+// 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?
- - When rewriting the proposed changes explanation, DO NOT change 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'.
+// 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 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 a *correct* find/replace operation can be generated.
+// - If the proposed changes explanation is *not* correct, you must rewrite the proposed changes explanation to correctly follow *all* the rules in the [Explanation 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.
- - You MUST NOT make *functional* changes when rewriting the explanation. Your job is *NOT* to change functionality or add additional code in *any way*. It is *only* to ensure that the explanation follows all the rules correctly. Stick as close to the original explanation as possible when rewriting. Fix the problems, but keep everything else *exactly the same*.
-`
+// - When rewriting the proposed changes explanation, DO NOT change 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 a *correct* find/replace operation can be generated.
+
+// - You MUST NOT make *functional* changes when rewriting the explanation. Your job is *NOT* to change functionality or add additional code in *any way*. It is *only* to ensure that the explanation follows all the rules correctly. Stick as close to the original explanation as possible when rewriting. Fix the problems, but keep everything else *exactly the same*.
+// `
const EditsFindReplacePrompt = `
- Next, convert the proposed updates into a find/replace operation. You MUST output EXACTLY ONE <PlandexReplacement> element. Multiple replacements are NOT allowed under any circumstances.
@@ -316,21 +316,17 @@ The new function 'someFunction' was incorrectly added to the end of the file. It
<PlandexIncorrect/>
-## Evaluate Explanation
-
-The explanation correctly describes how the new function should be added to the file.
-
## Comments
-pdx-new-2: // ... existing code ...
+// ... existing code ...
Evaluation: refers to the initialization code at the start of the 'existingFunction1' function
Reference: true
-pdx-new-3: // loop 15 times
+// loop 15 times
Evaluation: explains the for loop below the comment
Reference: false
-pdx-new-5: // ... existing function calls ...
+// ... existing function calls ...
Evaluation: refers to the function calls at the start of the for loop
Reference: true
@@ -382,17 +378,17 @@ The proposed changes incorrectly removed the 'existingFunction2' function.
`
}
- s += `
-## Evaluate Explanation
+ // 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.
-`
+ // 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>
+ // <PlandexProposedUpdatesExplanation>
+ // **Updating some/path/to/file.js:** I'll add 'someFunction' at the end of the file, immediately after 'existingFunction2'.
+ // </PlandexProposedUpdatesExplanation>
+ // `
+ s += `
<PlandexReplacement>
<PlandexOld>
function existingFunction1() {
@@ -485,15 +481,15 @@ var ReferencesPrompt = ExampleReferences + `
---
Comments:
- pdx-new-2: // ... existing code to start transaction ...
+ // ... existing code to start transaction ...
Evaluation: refers the code at the beginning of the 'update' function that starts the database transaction.
Reference: true
- pdx-new-4: // verify user permission before performing update
+ // verify user permission before performing update
Evaluation: describes the change being made. Does not refer to any code in the *original file*.
Reference: false
- pdx-new-6: // ... existing update code ...
+ // ... existing update code ...
Evaluation: refers the code inside the 'update' function that updates the user.
Reference: true
diff --git a/app/server/model/prompts/context_load.go b/app/server/model/prompts/context_load.go
index d0d55e0b..124e0404 100644
--- a/app/server/model/prompts/context_load.go
+++ b/app/server/model/prompts/context_load.go
@@ -15,7 +15,7 @@ In response to the user's latest prompt, do the following:
- In a section titled '### Context Categories', list one or more categories of context that are relevant to the user's task, question, or message. For example, if the user is asking you to implement an API endpoint, you might list 'API endpoints', 'database operations', 'frontend code', 'utilities', and so on. Make sure any and all relevant categories are included, but don't include more categories than necessary—if only a single category is relevant, then only list that one. Do not include file paths, symbols, or explanations—only the categories.
- - Using the map and file tree in context, output a '### Relevant Symbols' list of potentially relevant *symbols* (like functions, methods, types, variables, etc.) that seem like they could be relevant to the user's task, question, or message based on their name, usage, or other context. Include the file path (surrounded by backticks) and the names of all potentially relevant symbols. File paths *absolutely must* be surrounded by backticks like this: ` + "`" + "path/to/file.go" + "`" + `. Any symbols that are referred to in the user's prompt must be included. You MUST organize the list by category using the categories from the '### Context Categories' section—ensure each category is represented in the list.
+ - Using the map and file tree in context, output a '### Relevant Symbols' list of potentially relevant *symbols* (like functions, methods, types, variables, etc.) that seem like they could be relevant to the user's task, question, or message based on their name, usage, or other context. Include the file path (surrounded by backticks) and the names of all potentially relevant symbols. File paths *absolutely must* be surrounded by backticks like this: ` + "`" + "path/to/file.go" + "`" + `. Any symbols that are referred to in the user's prompt must be included. You MUST organize the list by category using the categories from the '### Context Categories' section—ensure each category is represented in the list. When listing symbols, output just the name of the symbol, not it's full signature (e.g. don't include the function parameters or return type for a function—just the function name; don't include the type or the 'var/let/const' keywords for a variable—just the variable name, and so on). Output the symbols as a comma separated list in a single paragraph for each file. You MUST include relevant symbols (and associated file paths) for each category from the '### Context Categories' section.
` + ContextLoadingRules + `
diff --git a/app/server/model/prompts/explanation_format.go b/app/server/model/prompts/explanation_format.go
new file mode 100644
index 00000000..7e0f082c
--- /dev/null
+++ b/app/server/model/prompts/explanation_format.go
@@ -0,0 +1,252 @@
+package prompts
+
+const ChangeExplanationPrompt = `
+### Action Explanation Format
+
+Prior to any code block that is *updating* an existing file in context, you MUST explain the change in the following format EXACTLY:
+
+---
+**Updating ` + "`[file path]`" + `**
+Type: [type]
+Summary: [brief description, symbols/sections being changed]
+Context: [describe surrounding code that helps locate the change unambiguously]
+Preserve: [symbols/structures/sections to preserve when overwriting entire file]
+---
+
+Include a line break after the initial '**Updating ` + "`[file path]`" + `**' line as well as each of the following fields. Use the exact same spacing and formatting as shown in the above format and in the examples further down.
+
+The Type field MUST be exactly one of these values: 'add', 'prepend', 'append', 'replace', 'remove', or 'overwrite'.
+
+- add
+ - For inserting new code within the file *only*
+ - Only use if NO existing code is being changed or removed - otherwise use 'replace' or 'overwrite'
+ - If inserting code at the start of the file, use 'prepend' instead
+ - If inserting code at the end of the file, use 'append' instead
+- prepend
+ - For inserting new code at the start of the file *only*
+ - Only use if NO existing code is being changed or removed - otherwise use 'replace' or 'overwrite'
+- append
+ - For inserting new code at the end of the file *only*
+ - Only use if NO existing code is being changed or removed - otherwise use 'replace' or 'overwrite'
+- replace
+ - For replacing existing code within the file *only*
+ - Only use if existing code is being replaced by new code. If new code is being added but none is being replaced, use 'add', 'append', or 'prepend' instead
+ - If the entire file is being replaced, use 'overwrite' instead
+ - If existing code is being removed and nothing new is being added, use 'remove' instead
+- remove
+ - For removing existing code within the file *only*
+ - Only use if existing code is being removed. If new code is being added but none is being removed, use 'add', 'append', or 'prepend' instead
+- overwrite
+ - For replacing the entire file *only*
+ - Only use if the *entire file* is being replaced. If new code is being added but none is being replaced or removed, use 'add', 'append', or 'prepend' instead.
+
+For each Type, follow these validation rules:
+
+- For 'add':
+ - Summary MUST briefly describe the new code being added and where it will be inserted
+ - Context MUST describe the surrounding code structures that help locate where the new code will be inserted
+ - Preserve field must be omitted
+
+- For 'prepend':
+ - Summary MUST briefly describe the new code being added and where it will be inserted
+ - Context MUST describe the first code structure in the original file
+ - Preserve field must be omitted
+
+- For 'append':
+ - Summary MUST briefly describe the new code being appended to the end of the file
+ - Context MUST describe the last code structure in the original file
+ - Preserve field must be omitted
+
+- For 'replace':
+ - Summary MUST briefly describe the change and list the specific symbols/sections being changed or replaced
+ - Context MUST describe the surrounding code structures that help locate what is being replaced
+ - Preserve field must be omitted
+
+- For 'remove':
+ - Summary MUST briefly describe the change and list the specific symbols/sections being changed or removed
+ - Context MUST describe the surrounding code structures that help locate what is being removed
+ - Preserve field must be omitted
+
+- For 'overwrite':
+ - Summary MUST briefly describe the change and list the specific symbols/sections being changed or replaced
+ - Context field must be omitted
+ - Preserve MUST *exhaustively* list all symbols/sections in the original file that should be included in the final result
+
+In the Context, Summary, and Preserve fields, when listing code symbols, list them in a comma-separated list and surround them with backticks. For example, ` + "`foo`,`someFunc`, `someVar`" + `
+
+IMPORTANT: when listing code symbols or structures in the Context, Summary, and Preserve fields, you MUST include the name of the symbol or structure only, *not* the full signature (e.g. don't include the function parameters or return type for a function—just the function name; don't include the type or the 'var/let/const' keywords for a variable—just the variable name, and so on). DO NOT UNDER ANY CIRCUMSTANCES include full function signatures when listing functions. Include *only* the function name.
+
+For example, instead of ` + "`func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescription, error)`" + `, you should use ` + "`genPlanDescription`" + `. Instead of ` + "`var foo int`" + `, you should use ` + "`foo`" + `.
+
+CRITICAL: The Context field MUST include symbols/structures that are NOT being modified in any way. They must be completely outside of and untouched by the change. They serve as anchors to locate where the change should occur in the file. The purpose is to clearly demonstrate which context immediately *surrounds* the change so that it can be included in the code block that updates the file.
+
+ INCORRECT - symbols in Context are part of the change:
+ Summary: Replace implementations of ` + "`foo`, `bar`, and `baz`" + `
+ Context: Located between ` + "`foo`" + ` and ` + "`baz`" + ` # Wrong - these are being changed!
+
+ CORRECT - symbols in Context are outside the change:
+ Summary: Replace implementations of ` + "`foo`, `bar`, and `baz`" + `
+ Context: Located between ` + "`setup`" + ` and ` + "`cleanup`" + ` functions # Correct - these aren't being changed
+
+Keep the explanation as succinct as possible while still following all of the above rules.
+
+You ABSOLUTELY MUST use this template EXACTLY as described above. DO NOT CHANGE THE FORMATTING OR WORDING IN ANY WAY! DO NOT OMIT ANY FIELDS FROM THE EXPLANATION AS DESCRIBED ABOVE.
+
+Example explanations:
+
+**Updating ` + "`server/api/client.go`" + `**
+Type: add
+Summary: Add new ` + "`doRequest`" + ` method to ` + "`Client`" + ` struct after the constructor method
+Context: Located between ` + "`NewClient`" + ` constructor and ` + "`getUser`" + ` method
+
+**Updating ` + "`server/types/api.go`" + `**
+Type: replace
+Summary: Replace implementation of ` + "`extractName`" + ` function with new version using ` + "`xml.Decoder`" + `
+Context: Located between ` + "`validateName`" + ` and ` + "`formatName`" + ` functions
+
+**Updating ` + "`cli/cmd/update.go`" + `**
+Type: overwrite
+Summary: Replace implementations of ` + "`updateCmd`" + `, ` + "`runUpdate`" + `, and ` + "`validateUpdate`" + ` functions with new versions
+Preserve: ` + "`updateFlags`" + ` struct and ` + "`defaultTimeout`" + ` constant
+
+**Updating ` + "`server/config/init.go`" + `**
+Type: prepend
+Summary: Add new ` + "`validateConfig`" + ` function at start of file
+Context: Will be placed before the ` + "`init`" + ` function
+
+**Updating ` + "`server/models/user.go`" + `**
+Type: append
+Summary: Add new ` + "`cleanupUserData`" + ` function at end of file
+Context: Will be placed after the ` + "`validateUser`" + ` function
+
+**Updating ` + "`server/handlers/auth.go`" + `**
+Type: remove
+Summary: Remove unused ` + "`validateLegacyTokens`" + ` function and its helper ` + "`checkTokenFormat`" + `
+Context: Located between ` + "`parseAuthHeader`" + ` and ` + "`validateJWT`" + ` functions
+
+*
+
+ALL code structures that are mentioned in the Context field MUST be included as *anchors* in the code block that updates the file. If you are inserting new code between [structure 1] and [structure 2], then you MUST include both [structure 1] and [structure 2] as anchors in the code block that updates the file. Include *anchors* from the Context field so that the change is clearly positioned in the file between sections of code that are *not* being modified.
+
+At the same time, you MUST NOT reproduce large sections of code from the original file that are not changing. You MUST use reference comments "// ... existing code ..." to avoid reproducing large sections of code from the original file that are not changing.
+
+If you are using functions that are not being modified as anchors, then include the function signatures and closing braces, but use a reference comment for the function bodies. Here is an example:
+
+If you are using functions that are not being modified as anchors, then include the function signatures and closing braces, but use a reference comment for the function bodies. Here is an example:
+
+If your change description is:
+
+**Updating ` + "`server/api/users.go`" + `**
+Type: replace
+Summary: Replace implementation of ` + "`validateUser`" + ` function to add role and permission validation
+Context: Located between ` + "`parseUser`" + ` and ` + "`updateUser`" + ` functions
+
+Then your code block should look like:
+
+---
+// ... existing code ...
+
+func (api *API) parseUser(input []byte) (*User, error) {
+ // ... existing code ...
+}
+
+// Replacing existing validateUser implementation
+func (api *API) validateUser(user *User) error {
+ // Validate basic fields
+ if user.ID == "" {
+ return errors.New("user ID is required")
+ }
+ if user.Email == "" {
+ return errors.New("email is required")
+ }
+
+ // New validation for roles
+ if len(user.Roles) == 0 {
+ return errors.New("user must have at least one role")
+ }
+ for _, role := range user.Roles {
+ if !isValidRole(role) {
+ return fmt.Errorf("invalid role: %s", role)
+ }
+ }
+
+ // New validation for permissions
+ for _, permission := range user.Permissions {
+ if !isValidPermission(permission) {
+ return fmt.Errorf("invalid permission: %s", permission)
+ }
+ }
+
+ return nil
+}
+
+func (api *API) updateUser(user *User) error {
+ // ... existing code ...
+}
+
+// ... existing code ...
+---
+
+Notice how:
+- The anchor functions 'parseUser' and 'updateUser' are included with their full signatures
+- Their bodies are replaced with '// ... existing code ...' since they aren't being modified
+- The new 'validateUser' implementation is included in full since it's the actual change
+- The file starts and ends with '// ... existing code ...' comments since this change is in the middle of the file
+- There's a comment indicating we're replacing the existing implementation
+
+*
+
+If a file is being *updated* and the above explanation does *not* indicate that the file is being *overwritten* or that the change is being prepended to the *start* of the file, then the code block ABSOLUTELY ALWAYS MUST begin with an "... existing code ..." comment to account for all the code before the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
+
+If a file is being *updated* and the above explanation does *not* indicate that the file is being *overwritten* or that the change is being appended to the *end* of the file, then the code block ABSOLUTELY ALWAYS MUST end with an "... existing code ..." comment to account for all the code after the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
+
+Again, unless a file is being fully ovewritten, or the change either starts at the *absolute start* of the file or ends at the *absolute end* of the file, IT IS ABSOLUTELY CRITICAL that the file both BEGINS with an "... existing code ..." comment and ENDS with an "... existing code ..." comment.
+
+If a file must begin with an "... existing code ..." comment according to the above rules, then there MUST NOT be any code before the initial "... existing code ..." comment.
+
+If a file must end with an "... existing code ..." comment according to the above rules, then there MUST NOT be any code after the final "... existing code ..." comment.
+
+Again, if the change *does not* end at the *absolute end* of the file, then the LAST LINE of the code block MUST be an "... existing code ..." comment. Ending the code block like this:
+
+---
+// ... existing code ...
+
+func (a *Api) NewMethod() {
+ callExistingMethod()
+}
+
+func (a *Api) LoadContext(planId, branch string, req
+ shared.LoadContextRequest) (*shared.LoadContextResponse, *shared.ApiError) {
+ // ... existing code ...
+}
+---
+
+is NOT CORRECT, because the last line is not an "... existing code ..." comment—it is rather the '}' closing bracket of the function. Instead, it must be:
+
+---
+// ... existing code ...
+
+func (a *Api) NewMethod() {
+ callExistingMethod()
+}
+
+func (a *Api) LoadContext(planId, branch string, req
+ shared.LoadContextRequest) (*shared.LoadContextResponse, *shared.ApiError) {
+ // ... existing code ...
+}
+
+// ... existing code ...
+---
+
+Now the final line is an "... existing code ..." comment, which is correct.
+
+*
+
+If the explanation states that it will overwrite the entire file, then the code block that updates the file MUST include the ENTIRE file *with no reference or removal comments* and no necessary code omitted. Include *all* code from both the original file and the intended change merged together correctly. Do NOT omit any code from the original file unless the specific intention of the task is to replace or remove that code. Ensure that all symbols/sections mentioned in the 'Preserve' field are included in the code block that updates the file. *MAKE THE CODE BLOCK AS LONG AS NECESSARY TO INCLUDE THE **ENTIRE** FILE.* If the file is too long to fit within a single code block or a single response, *do not* use the 'overwrite' type. Use another type to make a more specific change.
+
+Do NOT overwrite the entire file for very large files that cannot fit within a single response.
+
+*
+
+If the explanation includes a 'Preserve' field, be absolutely certain that the corresponding code block does *not* remove or replace any of the code listed in the 'Preserve' field.
+`
diff --git a/app/server/model/prompts/update_format.go b/app/server/model/prompts/update_format.go
index 31399cab..512fd233 100644
--- a/app/server/model/prompts/update_format.go
+++ b/app/server/model/prompts/update_format.go
@@ -932,155 +932,3 @@ Key principles demonstrated in these examples:
4. Use "... existing code ..." comments properly to indicate preserved sections
5. Show enough context to understand the code structure
`
-
-const ChangeExplanationPrompt = `
-### Action Explanation Format
-
-Prior to any code block that is *updating* an existing file in context, you MUST explain the change in the following format EXACTLY:
-
----
-**Updating ` + "`[file path]`:**" + ` I'll [action explanation].
----
-
-'action explanation' MUST ALWAYS take one of the following forms:
-
-- '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, 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 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 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 code 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!
-
-Do *NOT* UNDER ANY CIRCUMSTANCES use an explanation that does not match one of the above formats like "I'll update the code to...". You ABSOLUTELY MUST ALWAYS *exactly* match one of the above formats.
-
-If you are inserting code between two existing code blocks, do NOT use the start or end of the file to describe the position of the change; instead, use the code or structure that is *immediately before* and *immediately after* the point where the new code will be inserted.
-
-When inserting or replacing code, the "specific code or structure" or "specific *adjacent* code or structure" must be the code or structure that is *immediately before* and *immediately after* the point where the new code will be inserted. These two code structures ABSOLUTELY MUST be *adjacent* in the original file. DO NOT insert code between two code structures that aren't *immediately adjacent* in the original file. You must be precise in pinpointing the exact location of the change.
-
-Do NOT leave off any part of the explanation as described above. Do NOT output something like: 'I'll add the doRequest method to the class' or 'I'll add the types for making the api call'. These do NOT exactly match one of the above formats. Instead, you MUST output the full explanation as described above like:
-
-- **Updating ` + "`server/api/client.go`**" + `: I'll add the ` + "`doRequest`" + ` method between the constructor method and the ` + "`getUser`" + ` method.
-- **Updating ` + "`server/types/api.go`**" + `: I'll add the types for making the api call between the imports and the ` + "`init`" + ` method.
-- **Updating ` + "`cli/cmd/update.go`**" + `: I'll overwrite the entire file with new code for the ` + "`update`" + ` CLI command.
-- **Updating ` + "`server/db/user.go`**" + `: I'll add the ` + "`update`" + ` function between the ` + "`get`" + ` and the end of the file.
-- **Updating ` + "`server/db/user.go`**" + `: I'll append a new ` + "`update`" + ` function to the end of the file.
-- **Updating ` + "`server/db/user.go`**" + `: I'll prepend a new ` + "`update`" + ` function to the start of the file.
-- **Updating ` + "`server/db/user.go`**" + `: I'll add a new ` + "`update`" + ` function immediately after the ` + "`get`" + ` method.
-- **Updating ` + "`server/db/user.go`**" + `: I'll add a new ` + "`update`" + ` function immediately before the ` + "`getUser`" + ` method.
-- **Updating ` + "`server/db/user.go`**" + `: I'll remove the ` + "`getUser`" + ` method.
-
-You ABSOLUTELY MUST use this template EXACTLY as described above. DO NOT CHANGE THE FORMATTING OR WORDING IN ANY WAY! DO NOT OMIT ANY PART OF THE EXPLANATION AS DESCRIBED ABOVE. AND ABSOLUTELY DO NOT EVEN THINK ABOUT LEAVING OUT THIS MESSAGE! It is EXTREMELY IMPORTANT that you include this message in every code block that updates an existing file.
-
-When creating a *new* file, do NOT include this explanation. Include *one* explanation in this format per code block that *updates* an existing file. Do NOT include multiple explanations in the same code block.
-
-When describing two specific code structures in the original file that new code will be inserted between, the two code structures MUST be *immediately adjacent* in the original file. DO NOT insert code between two code structures that aren't *immediately adjacent* in the original file. For example, if the original file is:
-
----
-package something
-
-type T struct {}
-
-func Add(t *T) {
- t.doSomething()
- t.doSomethingElse()
- t.doSomethingElseAgain()
- t.doSomethingOnceMore()
-}
----
-
-DO NOT output an explanation like this:
-
----
-**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call between the 'doSomethingElse' method and the 'doSomethingOnceMore' method.
----
-
-The problem is the 'doSomethingElse' function call and the 'doSomethingOnceMore' method are not *immediately adjacent* in the original file. Instead, output an explanation like this:
-
----
-**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call between the 'doSomethingElse' method and the 'doSomethingElseAgain' method.
----
-
-The 'doSomethingElse' method and the 'doSomethingElseAgain' method are *immediately adjacent* in the original file, so the explanation is correct.
-
-You can also output an explanation like this:
-
----
-**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call immediately after the 'doSomethingOnceMore' method.
----
-
-The explanation MUST ALWAYS WITHOUT EXCPETION be immediately followed by the code block that updates the file in the EXACT format specified in section 2a of your instructions. DO NOT omit the code block label.
-
-Example:
-
----
-**Updating ` + "`server/something/something.go`**" + `: I'll add the new 'anotherThing' function call immediately after the 'doSomethingElse' method.
-
-- server/something/something.go:
-<PlandexBlock lang="go">
-// ... existing code ...
-
-func Add(t *T) {
- // ... existing code ...
- doSomethingElse()
- anotherThing()
- doSomethingElseAgain()
- // ... existing code ...
-}
-</PlandexBlock>
-
-*
-
-ALL code structures that are mentioned in the explanation MUST be included as *anchors* in the code block that updates the file. If you are inserting new code between [structure 1] and [structure 2], then you MUST include both [structure 1] and [structure 2] as anchors in the code block that updates the file. You do not need to include the full structures—use "... existing code ..." reference comments if necessary to avoid outputting full structures, but you MUST include the anchors to make it clear and unambiguous where the new code is being inserted. The same applies if you are replacing code between [structure 1] and [structure 2] or removing code between [structure 1] and [structure 2].
-
-*
-
-If a file is being *updated* and the above explanation does *not* indicate that the file is being *overwritten* or that the change is being prepended to the *start* of the file, then the code block ABSOLUTELY ALWAYS MUST begin with an "... existing code ..." comment to account for all the code before the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
-
-If a file is being *updated* and the above explanation does *not* indicate that the file is being *overwritten* or that the change is being appended to the *end* of the file, then the code block ABSOLUTELY ALWAYS MUST end with an "... existing code ..." comment to account for all the code after the change. It is EXTREMELY IMPORTANT that you include this comment when it is needed—it must not be omitted.
-
-Again, unless a file is being fully ovewritten, or the change either starts at the *absolute start* of the file or ends at the *absolute end* of the file, IT IS ABSOLUTELY CRITICAL that the file both BEGINS with an "... existing code ..." comment and ENDS with an "... existing code ..." comment.
-
-If a file must begin with an "... existing code ..." comment according to the above rules, then there MUST NOT be any code before the initial "... existing code ..." comment.
-
-If a file must end with an "... existing code ..." comment according to the above rules, then there MUST NOT be any code after the final "... existing code ..." comment.
-
-Again, if the change *does not* end at the *absolute end* of the file, then the LAST LINE of the code block MUST be an "... existing code ..." comment. Ending the code block like this:
-
----
-// ... existing code ...
-
-func (a *Api) NewMethod() {
- callExistingMethod()
-}
-
-func (a *Api) LoadContext(planId, branch string, req
- shared.LoadContextRequest) (*shared.LoadContextResponse, *shared.ApiError) {
- // ... existing code ...
-}
----
-
-is NOT CORRECT, because the last line is not an "... existing code ..." comment—it is rather the '}' closing bracket of the function. Instead, it must be:
-
----
-// ... existing code ...
-
-func (a *Api) NewMethod() {
- callExistingMethod()
-}
-
-func (a *Api) LoadContext(planId, branch string, req
- shared.LoadContextRequest) (*shared.LoadContextResponse, *shared.ApiError) {
- // ... existing code ...
-}
-
-// ... existing code ...
----
-
-Now the final line is an "... existing code ..." comment, which is correct.
-`
diff --git a/app/server/syntax/structured_edits_apply.go b/app/server/syntax/structured_edits_apply.go
index 52136c6f..4a4b8cd9 100644
--- a/app/server/syntax/structured_edits_apply.go
+++ b/app/server/syntax/structured_edits_apply.go
@@ -98,206 +98,238 @@ func ApplyChanges(
}
}
- if addMissingStartEndRefs {
- var beginsWithRef bool = false
- var endsWithRef bool = false
- var foundNonRefLine bool = false
+ desc = strings.ToLower(desc)
+ desc = strings.TrimSpace(desc)
+ desc = strings.ReplaceAll(desc, "*", "")
+ desc = strings.ReplaceAll(desc, "`", "")
+ desc = strings.ReplaceAll(desc, "'", "")
+ desc = strings.ReplaceAll(desc, `"`, "")
+
+ isEntireFileUpdate := strings.Contains(desc, "type: overwrite")
+ isReplace := strings.Contains(desc, "type: replace")
+ isRemove := strings.Contains(desc, "type: remove")
+ isAdd := strings.Contains(desc, "type: add")
+ isPrepend := strings.Contains(desc, "type: prepend")
+ isAppend := strings.Contains(desc, "type: append")
+
+ var res *ApplyChangesResult
+
+ if isEntireFileUpdate {
+ hasRefsOrRemovals := len(references) > 0 || len(removals) > 0
+
+ if !hasRefsOrRemovals {
+ // shortcut to just return the full updated file and skip verification
+ // if any references were included or the first and last lines of the proposed file don't match the first and last lines of the original file
+ res = &ApplyChangesResult{
+ NewFile: proposed,
+ Proposed: proposed,
+ }
+ }
+ }
- for i, line := range proposedLines {
- hasRef := hasRefByLine[i+1] || hasRemovalByLine[i+1]
+ if res == nil {
+ if addMissingStartEndRefs {
+ var beginsWithRef bool = false
+ var endsWithRef bool = false
+ var foundNonRefLine bool = false
- if hasRef {
- if !foundNonRefLine {
- beginsWithRef = true
+ 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
}
- endsWithRef = true
- } else if line != "" {
- foundNonRefLine = true
- endsWithRef = false
}
- }
- if !beginsWithRef &&
- !strings.Contains(desc, "entire file") &&
- !((strings.Contains(desc, "replace code") ||
- strings.Contains(desc, "remove code")) &&
- strings.Contains(desc, "start of the file")) {
+ if !beginsWithRef &&
+ !isEntireFileUpdate &&
+ !((isReplace || isRemove) &&
+ strings.Contains(desc, "start of the file")) {
- if verboseLogging {
- fmt.Println("adding ... existing code ... to start of 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...)
}
- proposedLines = append([]string{""}, proposedLines...)
+ if !endsWithRef &&
+ !isEntireFileUpdate &&
+ !((isReplace || isRemove) &&
+ strings.Contains(desc, "end of the file")) {
- // bump all existing references up by 1
- for i, ref := range references {
- references[i] = Reference(int(ref) + 1)
+ if verboseLogging {
+ fmt.Println("adding ... existing code ... to end of file")
+ }
+
+ proposedLines = append(proposedLines, "")
+ references = append(references, Reference(len(proposedLines)))
}
- references = append([]Reference{Reference(1)}, references...)
}
- if !endsWithRef &&
- !strings.Contains(desc, "entire file") &&
- !((strings.Contains(desc, " replace ") ||
- strings.Contains(desc, " remove ")) &&
- strings.Contains(desc, "end of the file")) {
+ proposed = strings.Join(proposedLines, "\n")
+
+ isInsert := !isEntireFileUpdate && (isAdd || isPrepend || isAppend)
+ if isInsert {
if verboseLogging {
- fmt.Println("adding ... existing code ... to end of file")
+ fmt.Println("isInsert")
}
-
- proposedLines = append(proposedLines, "")
- references = append(references, Reference(len(proposedLines)))
}
- }
- proposed = strings.Join(proposedLines, "\n")
+ // if verboseLogging {
+ // fmt.Println("proposed:")
+ // fmt.Println(proposed)
+ // log.Println("ApplyChanges - references:")
+ // spew.Dump(references)
+ // log.Println("ApplyChanges - removals:")
+ // spew.Dump(removals)
+ // }
+
+ res = ExecApplyGeneric(
+ execApplyGenericParams{
+ original: original,
+ proposed: proposed,
+ originalLines: originalLines,
+ proposedLines: proposedLines,
+ references: references,
+ removals: removals,
+ isInsert: isInsert,
+ },
+ )
+
+ res.Proposed = proposed
+
+ if len(res.NeedsVerifyReasons) > 0 {
+ if verboseLogging {
+ log.Println("ApplyChanges - needs verify reasons:")
+ log.Println(spew.Sdump(res.NeedsVerifyReasons))
- regex := regexp.MustCompile(`(I'll|I will) (add|insert|append|prepend)`)
- isInsert := regex.MatchString(desc)
+ log.Println("ApplyChanges - proposed:")
+ log.Println(proposedInitial)
+ log.Println("--------------------------------")
- if isInsert {
- if verboseLogging {
- fmt.Println("isInsert")
- }
- }
+ // log.Println("ApplyChanges - original:")
+ // log.Println(original)
+ // log.Println("--------------------------------")
+ }
- // if verboseLogging {
- // fmt.Println("proposed:")
- // fmt.Println(proposed)
- // log.Println("ApplyChanges - references:")
- // spew.Dump(references)
- // log.Println("ApplyChanges - removals:")
- // spew.Dump(removals)
- // }
-
- res := ExecApplyGeneric(
- execApplyGenericParams{
- original: original,
- proposed: proposed,
- originalLines: originalLines,
- proposedLines: proposedLines,
- references: references,
- removals: removals,
- isInsert: isInsert,
- },
- )
-
- res.Proposed = proposed
-
- if len(res.NeedsVerifyReasons) > 0 {
- log.Println("ApplyChanges - needs verify reasons:")
- log.Println(spew.Sdump(res.NeedsVerifyReasons))
-
- log.Println("ApplyChanges - proposed:")
- log.Println(proposedInitial)
- log.Println("--------------------------------")
-
- // log.Println("ApplyChanges - original:")
- // log.Println(original)
- // log.Println("--------------------------------")
-
- if len(res.NeedsVerifyReasons) == 1 && res.NeedsVerifyReasons[0] == NeedsVerifyReasonAmbiguousLocation {
- var err error
- prevRes := res
- res, err = ExecApplyTreeSitter(
- execApplyTreeSitterParams{
- original: original,
- proposed: proposed,
- references: references,
- removals: removals,
- language: language,
- parser: parser,
- ctx: ctx,
- },
- )
-
- if err != nil {
- log.Printf("ApplyChanges - error applying tree-sitter: %v", err)
- // since we got an error, give up and go back to the previous result
- res = prevRes
- } else if len(res.NeedsVerifyReasons) > 0 {
- return res
+ if len(res.NeedsVerifyReasons) == 1 && res.NeedsVerifyReasons[0] == NeedsVerifyReasonAmbiguousLocation {
+ var err error
+ prevRes := res
+ res, err = ExecApplyTreeSitter(
+ execApplyTreeSitterParams{
+ original: original,
+ proposed: proposed,
+ references: references,
+ removals: removals,
+ language: language,
+ parser: parser,
+ ctx: ctx,
+ },
+ )
+
+ if err != nil {
+ log.Printf("ApplyChanges - error applying tree-sitter: %v", err)
+ // since we got an error, give up and go back to the previous result
+ res = prevRes
+ } else if len(res.NeedsVerifyReasons) > 0 {
+ return res
+ }
}
}
}
- if !strings.Contains(desc, "entire file") {
- if verboseLogging {
- log.Println("ApplyChanges - checking for removed lines")
- }
+ if verboseLogging {
+ log.Println("ApplyChanges - checking for removed lines")
+ }
- originalLineMap := make(map[string]bool)
- for _, line := range originalLines {
- originalLineMap[strings.TrimSpace(line)] = true
- }
+ 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
- }
+ 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] {
+ // Check for removed lines (lines in original that are not in new)
+ for line := range originalLineMap {
+ if !newLineMap[line] {
+ if verboseLogging {
log.Println("ApplyChanges - code removed")
log.Println("line:")
log.Println(line)
- res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonCodeRemoved)
- break
}
+ res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonCodeRemoved)
+ break
}
+ }
- if strings.Contains(desc, " replace ") {
- if verboseLogging {
- log.Println("ApplyChanges - checking for duplicated lines")
- }
+ if strings.Contains(desc, " replace ") {
+ if verboseLogging {
+ log.Println("ApplyChanges - checking for duplicated lines")
+ }
- // Check for lines in proposed updates that are duplicated in new file
- newLineFreq := make(map[string]int)
- originalLineFreq := make(map[string]int)
- proposedLineFreq := make(map[string]int)
+ // Check for lines in proposed updates that are duplicated in new file
+ newLineFreq := make(map[string]int)
+ originalLineFreq := make(map[string]int)
+ proposedLineFreq := make(map[string]int)
- // First count frequencies in original file
- for _, line := range originalLines {
- trimmed := strings.TrimSpace(line)
- if len(trimmed) > duplicationThreshold {
- originalLineFreq[line]++
- }
+ // First count frequencies in original file
+ for _, line := range originalLines {
+ trimmed := strings.TrimSpace(line)
+ if len(trimmed) > duplicationThreshold {
+ originalLineFreq[line]++
}
+ }
- // Count frequencies in proposed file
- for _, line := range proposedLines {
- trimmed := strings.TrimSpace(line)
- if len(trimmed) > duplicationThreshold {
- proposedLineFreq[line]++
- }
+ // Count frequencies in proposed file
+ for _, line := range proposedLines {
+ trimmed := strings.TrimSpace(line)
+ if len(trimmed) > duplicationThreshold {
+ proposedLineFreq[line]++
}
+ }
- // Count frequencies in new file
- for _, line := range newLines {
- trimmed := strings.TrimSpace(line)
- if len(trimmed) > duplicationThreshold {
- newLineFreq[line]++
- }
+ // Count frequencies in new file
+ for _, line := range newLines {
+ trimmed := strings.TrimSpace(line)
+ if len(trimmed) > duplicationThreshold {
+ newLineFreq[line]++
}
+ }
- // Check proposed lines against new frequencies, accounting for original duplicates
- for _, line := range proposedLines {
- trimmed := strings.TrimSpace(line)
- if len(trimmed) > duplicationThreshold {
- originalCount := originalLineFreq[line]
- proposedCount := proposedLineFreq[line]
- newCount := newLineFreq[line]
- if newCount > originalCount && newCount > proposedCount {
+ // Check proposed lines against new frequencies, accounting for original duplicates
+ for _, line := range proposedLines {
+ trimmed := strings.TrimSpace(line)
+ if len(trimmed) > duplicationThreshold {
+ originalCount := originalLineFreq[line]
+ proposedCount := proposedLineFreq[line]
+ newCount := newLineFreq[line]
+ if newCount > originalCount && newCount > proposedCount {
+ if verboseLogging {
log.Println("ApplyChanges - code duplicated")
log.Println("line:")
log.Println(line)
log.Printf("original occurrences: %d, new occurrences: %d", originalCount, newLineFreq[line])
- res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonCodeDuplicated)
- break
}
+ res.NeedsVerifyReasons = append(res.NeedsVerifyReasons, NeedsVerifyReasonCodeDuplicated)
+ break
}
}
}
diff --git a/app/server/syntax/structured_edits_test.go b/app/server/syntax/structured_edits_test.go
index b36a5384..c13b37a6 100644
--- a/app/server/syntax/structured_edits_test.go
+++ b/app/server/syntax/structured_edits_test.go
@@ -1339,7 +1339,7 @@ func TestStructuredReplacements(t *testing.T) {
desc := ""
if tt.isInsert {
- desc = "I'll add"
+ desc = "Type: add"
}
parser, lang, _, _ := GetParserForExt("." + tt.ext)
diff --git a/app/shared/file_maps.go b/app/shared/file_maps.go
index b8de6fda..dddb895b 100644
--- a/app/shared/file_maps.go
+++ b/app/shared/file_maps.go
@@ -16,13 +16,24 @@ func (m FileMapBodies) CombinedMap() string {
for _, path := range paths {
body := m[path]
body = strings.TrimSpace(body)
- if body == "" {
- continue
- }
- fileHeading := fmt.Sprintf("\n### %s\n", path)
+ fileHeading := MapFileHeading(path)
combinedMap.WriteString(fileHeading)
- combinedMap.WriteString(body)
+ if body != "" {
+ combinedMap.WriteString(body)
+ }
combinedMap.WriteString("\n")
}
return combinedMap.String()
}
+
+func (m FileMapBodies) TokenEstimateForPath(path string) int {
+ heading := MapFileHeading(path)
+ body := m[path]
+ body = strings.TrimSpace(body)
+ combined := heading + body + "\n"
+ return GetNumTokensEstimate(combined)
+}
+
+func MapFileHeading(path string) string {
+ return fmt.Sprintf("\n### %s\n", path)
+}