Skip to content

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

feat: implement fast apply and enhance build validation

Dane Schneider1418d08edfe0
diff --git a/app/server/model/plan/build_race.go b/app/server/model/plan/build_race.go
index 775ad03d..810641b9 100644
--- a/app/server/model/plan/build_race.go
+++ b/app/server/model/plan/build_race.go
@@ -2,6 +2,7 @@ package plan
import (
"context"
+ "errors"
"fmt"
"log"
"plandex-server/syntax"
@@ -20,11 +21,9 @@ type buildRaceParams struct {
desc string
reasons []syntax.NeedsVerifyReason
syntaxErrors []string
- blocksRemoved []struct {
- Start int
- End int
- Content string
- }
+
+ didCallFastApply bool
+ fastApplyCh chan string
}
func (fileState *activeBuildStreamFileState) buildRace(
@@ -42,39 +41,148 @@ func (fileState *activeBuildStreamFileState) buildRace(
desc := params.desc
reasons := params.reasons
syntaxErrors := params.syntaxErrors
+ fastApplyCh := params.fastApplyCh
log.Printf("buildRace - original file length: %d, updated length: %d", len(originalFile), len(updated))
log.Printf("buildRace - has %d syntax errors and %d verify reasons", len(syntaxErrors), len(reasons))
+ maxErrs := 3
+
resCh := make(chan raceResult, 1)
- errCh := make(chan error, 2)
+ errCh := make(chan error, maxErrs)
+
+ sendRes := func(res raceResult) {
+ select {
+ case resCh <- res:
+ case <-buildCtx.Done():
+ log.Printf("buildRace - context canceled, skipping sendRes")
+ }
+ }
+
+ sendErr := func(err error) {
+ select {
+ case errCh <- err:
+ case <-buildCtx.Done():
+ log.Printf("buildRace - context canceled, skipping sendErr")
+ }
+ }
- startedWholeFileBuild := false
+ startedFallbacks := false
startWholeFileBuild := func(comments string) {
log.Printf("buildRace - starting whole file fallback build")
- startedWholeFileBuild = true
go func() {
content, err := fileState.buildWholeFileFallback(buildCtx, proposedContent, desc, comments)
if err != nil {
+ if errors.Is(err, context.Canceled) {
+ log.Printf("Context canceled during whole file build")
+ return
+ }
+
log.Printf("buildRace - whole file build failed: %v", err)
- errCh <- fmt.Errorf("error building whole file: %w", err)
+ sendErr(fmt.Errorf("error building whole file: %w", err))
} else {
log.Printf("buildRace - whole file build succeeded")
- resCh <- raceResult{content: content, valid: true}
+ sendRes(raceResult{content: content, valid: true})
+ }
+ }()
+ }
+
+ maybeStartFastApply := func(onFail func()) {
+ log.Printf("buildRace - starting fast apply")
+ if !params.didCallFastApply {
+ log.Printf("buildRace - fast apply isn't defined, skipping")
+ sendErr(nil) // no error, just no fast apply
+ onFail()
+ return
+ }
+
+ go func() {
+ var fastApplyRes string
+
+ select {
+ case fastApplyRes = <-fastApplyCh:
+ case <-buildCtx.Done():
+ log.Printf("buildRace - context canceled, skipping fast apply")
+ sendErr(nil) // no error, just no fast apply
+ onFail()
+ return
+ }
+
+ if fastApplyRes == "" {
+ log.Printf("buildRace - fast apply isn't defined or failed to run")
+ sendErr(nil) // no error, just no fast apply
+ onFail()
+ return
+ }
+
+ fastApplySyntaxErrors := fileState.validateSyntax(buildCtx, fastApplyRes)
+ fileState.builderRun.FastApplySyntaxErrors = fastApplySyntaxErrors
+
+ if len(fastApplySyntaxErrors) > 0 {
+ log.Printf("buildRace - fast apply succeeded, but has %d syntax errors", len(fastApplySyntaxErrors))
+ sendErr(fmt.Errorf("fast apply succeeded, but has %d syntax errors", len(fastApplySyntaxErrors)))
+ onFail()
+ return
+ }
+
+ log.Printf("buildRace - fast apply succeeded, validating... ")
+ validateResult, err := fileState.buildValidateLoop(buildCtx, buildValidateLoopParams{
+ originalFile: originalFile,
+ updated: fastApplyRes,
+ proposedContent: proposedContent,
+ desc: desc,
+ reasons: reasons,
+
+ // just validate since we're already building replacements in parallel
+ maxAttempts: 1,
+ validateOnlyOnFinalAttempt: true,
+ })
+
+ if err != nil {
+ if errors.Is(err, context.Canceled) {
+ log.Printf("Context canceled during fast apply validation")
+ return
+ }
+
+ log.Printf("buildRace - fast apply validation failed with error: %v", err)
+ sendErr(fmt.Errorf("fast apply validation failed: %w", err))
+ onFail()
+ return
+ }
+
+ if validateResult.valid {
+ log.Printf("buildRace - fast apply validation succeeded")
+ fileState.builderRun.FastApplySuccess = true
+ sendRes(raceResult{content: validateResult.updated, valid: validateResult.valid})
+ } else {
+ log.Printf("buildRace - fast apply validation failed with problem: %s", validateResult.problem)
+ fileState.builderRun.FastApplyFailureResponse = validateResult.problem
+ sendErr(fmt.Errorf("fast apply validation failed: %s", validateResult.problem))
+ onFail()
+ return
}
}()
}
+ startFallbacks := func(comments string) {
+ startedFallbacks = true
+ // try fast apply + validation first if it's defined
+ // if it's undefined or fails, start the whole file build fallback
+ maybeStartFastApply(func() {
+ startWholeFileBuild(comments)
+ })
+ }
+
// If we get an incorrect marker, start the whole file build in the background while the validation/replacement loop continues
onInitialStream := func(chunk string, buffer string) bool {
- if !startedWholeFileBuild && strings.Contains(buffer, "<PlandexIncorrect/>") && strings.Contains(buffer, "<PlandexComments>") {
+ if !startedFallbacks && strings.Contains(buffer, "<PlandexIncorrect/>") && strings.Contains(buffer, "<PlandexComments>") {
log.Printf("buildRace - detected incorrect marker, triggering whole file build")
comments := utils.GetXMLContent(buffer, "PlandexComments")
- startWholeFileBuild(comments)
+ startFallbacks(comments)
}
// keep streaming
return false
@@ -93,30 +201,49 @@ func (fileState *activeBuildStreamFileState) buildRace(
})
if err != nil {
+ if errors.Is(err, context.Canceled) {
+ log.Printf("Context canceled during buildValidate")
+ return
+ }
+
log.Printf("buildRace - validation loop failed: %v", err)
- errCh <- fmt.Errorf("error building validate loop: %w", err)
+ sendErr(fmt.Errorf("error building validate loop: %w", err))
} else {
- log.Printf("buildRace - validation loop succeeded, valid: %v", validateResult.valid)
- resCh <- raceResult{content: validateResult.updated, valid: validateResult.valid}
+ log.Printf("buildRace - validation loop finished, valid: %v", validateResult.valid)
+ if validateResult.valid {
+ log.Printf("buildRace - validation loop succeeded, valid: %v", validateResult.valid)
+ sendRes(raceResult{content: validateResult.updated, valid: validateResult.valid})
+ } else {
+ log.Printf("buildRace - validation loop failed, valid: %v", validateResult.valid)
+ sendErr(fmt.Errorf("validation loop failed: %s", validateResult.problem))
+ }
}
}()
errs := []error{}
+ errChNumReceived := 0
for {
select {
+ case <-buildCtx.Done():
+ log.Printf("buildRace - context canceled")
+ return raceResult{}, buildCtx.Err()
case err := <-errCh:
- log.Printf("buildRace - error %d: %v\n", len(errs), err)
- errs = append(errs, err)
+ errChNumReceived++
+ log.Printf("buildRace - error channel received %d: %v\n", errChNumReceived, err)
+
+ if err != nil {
+ errs = append(errs, err)
+ }
- if len(errs) > 1 {
+ if errChNumReceived >= maxErrs {
log.Printf("buildRace - all attempts failed with %d errors", len(errs))
return raceResult{}, fmt.Errorf("all build attempts failed: %v", errs)
}
- if !startedWholeFileBuild {
- log.Printf("buildRace - starting whole file build")
- startWholeFileBuild("") // since replacements failed, pass an empty string for comments -- this causes whole file build to classify comments first
+ if !startedFallbacks {
+ log.Printf("buildRace - starting build fallbacks")
+ startFallbacks("") // since replacements failed, pass an empty string for comments -- this causes whole file build to classify comments first
}
case res := <-resCh:
log.Printf("buildRace - got successful result")
diff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.go
index 94449678..8522b038 100644
--- a/app/server/model/plan/build_structured_edits.go
+++ b/app/server/model/plan/build_structured_edits.go
@@ -6,6 +6,7 @@ import (
"log"
"plandex-server/db"
diff_pkg "plandex-server/diff"
+ "plandex-server/hooks"
"plandex-server/syntax"
"strings"
"time"
@@ -37,11 +38,62 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
proposedContent := activeBuild.FileContent
desc := activeBuild.FileDescription
- log.Printf("buildStructuredEdits - %s - applying changes\n", filePath)
+ descLower := strings.ToLower(desc)
+ isReplaceOrRemove := strings.Contains(descLower, "type: replace") || strings.Contains(descLower, "type: remove") || strings.Contains(descLower, "type: overwrite")
+
+ var autoApplyRes *syntax.ApplyChangesResult
+ var autoApplySyntaxErrors []string
+
+ calledFastApply := false
+ var fastApplyRes string
+ fastApplyCh := make(chan string, 1)
+
+ callFastApply := func() {
+ log.Printf("buildStructuredEdits - %s - calling fast apply hook\n", filePath)
+ fileState.builderRun.DidFastApply = true
+ fileState.builderRun.FastApplyStartedAt = time.Now()
+ calledFastApply = true
+
+ go func() {
+ res, err := hooks.ExecHook(hooks.CallFastApply, hooks.HookParams{
+ FastApplyParams: &hooks.FastApplyParams{
+ InitialCode: originalFile,
+ EditSnippet: proposedContent,
+ Language: fileState.language,
+ Ctx: buildCtx,
+ },
+ })
+
+ if err != nil {
+ log.Printf("buildStructuredEdits - error executing fast apply hook: %v\n", err)
+ // empty string acts as a no-op
+ fastApplyCh <- ""
+ return
+ } else if res.FastApplyResult == nil {
+ log.Printf("buildStructuredEdits - fast apply hook returned nil result\n")
+ // empty string acts as a no-op
+ fastApplyCh <- ""
+ return
+ }
+
+ fastApplyRes = res.FastApplyResult.MergedCode
+ log.Printf("buildStructuredEdits - %s - got fast apply hook result\n", filePath)
+ // fmt.Printf("buildStructuredEdits - fastApplyRes:\n%s", fastApplyRes)
+
+ fileState.builderRun.FastApplyFinishedAt = time.Now()
+ fastApplyCh <- fastApplyRes
+ }()
+ }
+
+ if isReplaceOrRemove {
+ callFastApply()
+ }
+
+ log.Printf("buildStructuredEdits - %s - applying changes\n", filePath)
// Apply plan logic
log.Printf("buildStructuredEdits - %s - calling ApplyChanges\n", filePath)
- applyRes := syntax.ApplyChanges(
+ autoApplyRes = syntax.ApplyChanges(
buildCtx,
syntax.ApplyChangesParams{
Original: originalFile,
@@ -54,71 +106,46 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
)
log.Printf("buildStructuredEdits - %s - got ApplyChanges result\n", filePath)
// log.Println("buildStructuredEdits - applyRes.NewFile:", applyRes.NewFile)
- log.Println("buildStructuredEdits - applyRes.NeedsVerifyReasons:", applyRes.NeedsVerifyReasons)
+ log.Println("buildStructuredEdits - applyRes.NeedsVerifyReasons:", autoApplyRes.NeedsVerifyReasons)
- // Log information about removed blocks
- if len(applyRes.BlocksRemoved) > 0 {
- log.Printf("buildStructuredEdits - %s - detected %d removed code blocks\n", filePath, len(applyRes.BlocksRemoved))
+ autoApplySyntaxErrors = fileState.validateSyntax(buildCtx, autoApplyRes.NewFile)
- // Log details about each removed block if not too verbose
- if len(applyRes.BlocksRemoved) <= 5 {
- for i, block := range applyRes.BlocksRemoved {
- log.Printf("buildStructuredEdits - removed block %d: lines %d-%d, %d characters\n", i+1, block.Start, block.End, len(block.Content))
- }
- }
+ hasNeedsVerifyReasons := len(autoApplyRes.NeedsVerifyReasons) > 0
- // If we have removed blocks but no CodeRemoved reason, add it
- hasCodeRemovedReason := false
- for _, reason := range applyRes.NeedsVerifyReasons {
- if reason == syntax.NeedsVerifyReasonCodeRemoved {
- hasCodeRemovedReason = true
- break
- }
- }
+ autoApplyHasSyntaxErrors := len(autoApplySyntaxErrors) > 0
+ autoApplyIsValid := !autoApplyHasSyntaxErrors && !hasNeedsVerifyReasons
- if !hasCodeRemovedReason {
- log.Printf("buildStructuredEdits - %s - adding CodeRemoved verification reason due to detected removed blocks\n", filePath)
- applyRes.NeedsVerifyReasons = append(applyRes.NeedsVerifyReasons, syntax.NeedsVerifyReasonCodeRemoved)
- }
+ if !autoApplyIsValid && !calledFastApply {
+ callFastApply()
}
- // Syntax check
- log.Printf("buildStructuredEdits - %s - validating syntax\n", filePath)
- syntaxErrors := fileState.validateSyntax(buildCtx, applyRes.NewFile)
- log.Printf("buildStructuredEdits - %s - got syntax validation result\n", filePath)
- if len(syntaxErrors) > 0 {
- log.Println("buildStructuredEdits - syntax errors:", syntaxErrors)
- }
+ log.Printf("buildStructuredEdits - %s - autoApplyHasSyntaxErrors: %t, hasNeedsVerifyReasons: %t, autoApplyIsValid: %t\n",
+ filePath, autoApplyHasSyntaxErrors, hasNeedsVerifyReasons, autoApplyIsValid)
- hasSyntaxErrors := len(syntaxErrors) > 0
- hasNeedsVerifyReasons := len(applyRes.NeedsVerifyReasons) > 0
- isValid := !hasSyntaxErrors && !hasNeedsVerifyReasons
-
- log.Printf("buildStructuredEdits - %s - hasSyntaxErrors: %t, hasNeedsVerifyReasons: %t, isValid: %t\n",
- filePath, hasSyntaxErrors, hasNeedsVerifyReasons, isValid)
-
- updated := applyRes.NewFile
+ updated := autoApplyRes.NewFile
// If no problems, we trust the direct ApplyChanges result
- if isValid {
+ if autoApplyIsValid {
log.Printf("buildStructuredEdits - %s - changes are valid, using ApplyChanges result\n", filePath)
fileState.builderRun.AutoApplySuccess = true
} else {
-
- log.Printf("buildStructuredEdits - %s - changes need validation/fixing\n", filePath)
- fileState.builderRun.AutoApplyValidationReasons = make([]string, len(applyRes.NeedsVerifyReasons))
- for i, reason := range applyRes.NeedsVerifyReasons {
+ log.Printf("buildStructuredEdits - %s - auto apply has syntax errors or NeedsVerifyReasons", filePath)
+ fileState.builderRun.AutoApplyValidationReasons = make([]string, len(autoApplyRes.NeedsVerifyReasons))
+ for i, reason := range autoApplyRes.NeedsVerifyReasons {
fileState.builderRun.AutoApplyValidationReasons[i] = string(reason)
}
- fileState.builderRun.AutoApplyValidationSyntaxErrors = syntaxErrors
+
+ fileState.builderRun.AutoApplyValidationSyntaxErrors = autoApplySyntaxErrors
buildRaceParams := buildRaceParams{
updated: updated,
proposedContent: proposedContent,
desc: desc,
- reasons: applyRes.NeedsVerifyReasons,
- syntaxErrors: syntaxErrors,
- blocksRemoved: applyRes.BlocksRemoved, // Pass removed blocks to buildRace
+ reasons: autoApplyRes.NeedsVerifyReasons,
+ syntaxErrors: autoApplySyntaxErrors,
+
+ didCallFastApply: calledFastApply,
+ fastApplyCh: fastApplyCh,
}
buildRaceResult, err := fileState.buildRace(buildCtx, cancelBuild, buildRaceParams)
@@ -171,7 +198,6 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
Content: "",
Path: filePath,
Replacements: replacements,
- SyntaxErrors: syntaxErrors,
}
log.Printf("buildStructuredEdits - %s - finishing build file\n", filePath)
diff --git a/app/server/model/plan/build_validate_and_fix.go b/app/server/model/plan/build_validate_and_fix.go
index 64528033..3727450b 100644
--- a/app/server/model/plan/build_validate_and_fix.go
+++ b/app/server/model/plan/build_validate_and_fix.go
@@ -2,6 +2,7 @@ package plan
import (
"context"
+ "errors"
"fmt"
"log"
"math/rand"
@@ -18,21 +19,24 @@ import (
"github.com/sashabaranov/go-openai"
)
-const MaxValidationFixAttempts = 4
+const MaxValidationFixAttempts = 3
type buildValidateLoopParams struct {
- originalFile string
- updated string
- proposedContent string
- desc string
- syntaxErrors []string
- reasons []syntax.NeedsVerifyReason
- initialPhaseOnStream func(chunk string, buffer string) bool
+ originalFile string
+ updated string
+ proposedContent string
+ desc string
+ syntaxErrors []string
+ reasons []syntax.NeedsVerifyReason
+ initialPhaseOnStream func(chunk string, buffer string) bool
+ validateOnlyOnFinalAttempt bool
+ maxAttempts int
}
type buildValidateLoopResult struct {
valid bool
updated string
+ problem string
}
func (fileState *activeBuildStreamFileState) buildValidateLoop(
@@ -49,7 +53,14 @@ func (fileState *activeBuildStreamFileState) buildValidateLoop(
syntaxErrors := params.syntaxErrors
numAttempts := 0
- for numAttempts < MaxValidationFixAttempts {
+ problems := []string{}
+
+ maxAttempts := MaxValidationFixAttempts
+ if params.maxAttempts > 0 {
+ maxAttempts = params.maxAttempts
+ }
+
+ for numAttempts < maxAttempts {
currentAttempt := numAttempts + 1
log.Printf("Starting validation attempt %d/%d", currentAttempt, MaxValidationFixAttempts)
@@ -88,6 +99,8 @@ func (fileState *activeBuildStreamFileState) buildValidateLoop(
modelConfig = *modelConfig.StrongModel
}
+ isLastAttempt := numAttempts == maxAttempts-1
+
// build validate params
validateParams := buildValidateParams{
originalFile: originalFile,
@@ -98,27 +111,36 @@ func (fileState *activeBuildStreamFileState) buildValidateLoop(
syntaxErrors: syntaxErrors,
reasons: reasons,
modelConfig: &modelConfig,
+ validateOnly: isLastAttempt && params.validateOnlyOnFinalAttempt,
}
log.Printf("Calling buildValidate for attempt %d", currentAttempt)
- valid, res, err := fileState.buildValidate(ctx, validateParams)
+ res, err := fileState.buildValidate(ctx, validateParams)
if err != nil {
+ if errors.Is(err, context.Canceled) {
+ log.Printf("Context canceled during buildValidate")
+ return buildValidateLoopResult{}, err
+ }
+
log.Printf("Error in buildValidate during attempt %d: %v", currentAttempt, err)
return buildValidateLoopResult{}, fmt.Errorf("error building validate: %v", err)
}
- updated = res
+ updated = res.updated
- if valid {
+ syntaxErrors = fileState.validateSyntax(ctx, updated)
+ log.Printf("Found %d syntax errors after attempt %d", len(syntaxErrors), currentAttempt)
+
+ if res.valid && len(syntaxErrors) == 0 {
log.Printf("Validation succeeded in attempt %d", currentAttempt)
return buildValidateLoopResult{
- valid: valid,
- updated: updated,
+ valid: res.valid,
+ updated: res.updated,
}, nil
}
+ problems = append(problems, res.problem)
+
log.Printf("Validation failed in attempt %d, preparing for next attempt", currentAttempt)
- syntaxErrors = fileState.validateSyntax(ctx, updated)
- log.Printf("Found %d syntax errors after attempt %d", len(syntaxErrors), currentAttempt)
numAttempts++
}
@@ -127,6 +149,7 @@ func (fileState *activeBuildStreamFileState) buildValidateLoop(
return buildValidateLoopResult{
valid: false,
updated: updated,
+ problem: strings.Join(problems, "\n\n"),
}, nil
}
@@ -140,12 +163,19 @@ type buildValidateParams struct {
onStream func(chunk string, buffer string) bool
phase int
modelConfig *shared.ModelRoleConfig
+ validateOnly bool
+}
+
+type buildValidateResult struct {
+ valid bool
+ updated string
+ problem string
}
func (fileState *activeBuildStreamFileState) buildValidate(
ctx context.Context,
params buildValidateParams,
-) (bool, string, error) {
+) (buildValidateResult, error) {
log.Printf("Starting buildValidate for phase %d", params.phase)
auth := fileState.auth
@@ -166,7 +196,7 @@ func (fileState *activeBuildStreamFileState) buildValidate(
diff, err := diff_pkg.GetDiffs(originalFile, updated)
if err != nil {
log.Printf("Error getting diffs: %v", err)
- return false, "", fmt.Errorf("error getting diffs: %v", err)
+ return buildValidateResult{}, fmt.Errorf("error getting diffs: %v", err)
}
// Choose prompt and tools based on preferred format
@@ -199,9 +229,18 @@ func (fileState *activeBuildStreamFileState) buildValidate(
reqStarted := time.Now()
fileState.builderRun.ReplacementStartedAt = reqStarted
- log.Printf("Making validation-replacements model request")
+ if params.validateOnly {
+ log.Printf("Making validation-only model request")
+ } else {
+ log.Printf("Making validation-replacements model request")
+ }
// log.Printf("Messages: %v", messages)
+ stop := []string{"<PlandexFinish/>"}
+ if params.validateOnly {
+ stop = []string{"<PlandexComments>", "<PlandexReplacements>"}
+ }
+
// Use ModelRequest for both formats
res, err := model.ModelRequest(ctx, model.ModelRequestParams{
Clients: clients,
@@ -214,7 +253,7 @@ func (fileState *activeBuildStreamFileState) buildValidate(
ConvoMessageId: fileState.convoMessageId,
BuildId: fileState.build.Id,
ModelPackName: fileState.settings.ModelPack.Name,
- Stop: []string{"<PlandexFinish/>"},
+ Stop: stop,
BeforeReq: func() {
log.Printf("Starting model request")
fileState.builderRun.ReplacementStartedAt = time.Now()
@@ -227,6 +266,11 @@ func (fileState *activeBuildStreamFileState) buildValidate(
})
if err != nil {
+ if errors.Is(err, context.Canceled) {
+ log.Printf("Context canceled during model request")
+ return buildValidateResult{}, err
+ }
+
log.Printf("Error calling model: %v", err)
return fileState.validationRetryOrError(ctx, params, err)
}
@@ -235,19 +279,16 @@ func (fileState *activeBuildStreamFileState) buildValidate(
log.Printf("Added generation ID: %s", res.GenerationId)
// Handle response based on format
- valid, updated, err := handleXMLResponse(fileState, res.Content, originalFile, updated)
+ parseRes, err := handleXMLResponse(fileState, res.Content, originalFile, updated, params.validateOnly)
if err != nil {
log.Printf("Error handling response: %v", err)
return fileState.validationRetryOrError(ctx, params, err)
}
- if valid {
- log.Printf("Validation result: valid=%v", valid)
- return valid, updated, nil
- }
+ log.Printf("Validation result: valid=%v", parseRes.valid)
- return false, updated, nil
+ return parseRes, nil
}
func handleXMLResponse(
@@ -255,16 +296,28 @@ func handleXMLResponse(
content string,
originalFile string,
updated string,
-) (bool, string, error) {
+ validateOnly bool,
+) (buildValidateResult, error) {
log.Printf("Handling XML response for file: %s", fileState.filePath)
if strings.Contains(content, "<PlandexCorrect/>") {
log.Printf("XML response indicates changes are correct")
fileState.builderRun.ReplacementSuccess = true
- return true, updated, nil
+ return buildValidateResult{
+ valid: true,
+ updated: updated,
+ }, nil
+ }
+
+ if validateOnly {
+ log.Printf("Validation-only mode, skipping replacements")
+ return buildValidateResult{
+ valid: false,
+ updated: updated,
+ }, nil
}
- originalFileLines := strings.Split(originalFile, "\n")
+ originalFileLines := strings.Split(originalFile, "\n")
updated = originalFile
log.Printf("Processing XML replacement blocks")
@@ -273,7 +326,11 @@ func handleXMLResponse(
if replacementsOuter == "" {
log.Printf("No replacements found in XML response")
- return false, updated, fmt.Errorf("no replacements found in XML response")
+ return buildValidateResult{
+ valid: false,
+ updated: updated,
+ problem: "No replacements found in XML response",
+ }, nil
}
replacements := utils.GetAllXMLContent(replacementsOuter, "Replacement")
@@ -286,16 +343,16 @@ func handleXMLResponse(
if old == "" {
log.Printf("No old content found for replacement")
- return false, updated, fmt.Errorf("no old content found for replacement")
+ return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("no old content found for replacement")
}
old = strings.TrimSpace(old)
- log.Printf("Old content trimmed:\n\n%s", old)
+ // log.Printf("Old content trimmed:\n\n%s", old)
if !strings.HasPrefix(old, "pdx-") {
log.Printf("Old content does not have a line number prefix for first line")
- return false, updated, fmt.Errorf("old content does not have a line number prefix for first line")
+ return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("old content does not have a line number prefix for first line")
}
oldLines := strings.Split(old, "\n")
@@ -310,14 +367,14 @@ func handleXMLResponse(
firstLineNum, err := shared.ExtractLineNumberWithPrefix(firstLine, "pdx-")
if err != nil {
log.Printf("Error extracting line number from first line: %v", err)
- return false, updated, fmt.Errorf("error extracting line number from first line: %v", err)
+ return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("error extracting line number from first line: %v", err)
}
if lastLine != "" {
lastLineNum, err = shared.ExtractLineNumberWithPrefix(lastLine, "pdx-")
if err != nil {
log.Printf("Error extracting line number from last line: %v", err)
- return false, updated, fmt.Errorf("error extracting line number from last line: %v", err)
+ return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("error extracting line number from last line: %v", err)
}
}
@@ -334,10 +391,20 @@ func handleXMLResponse(
log.Printf("Updated content:\n\n%s", updated)
}
- return false, updated, nil
+ var problem string
+
+ if strings.Contains(content, "<PlandexIncorrect/>") {
+ split := strings.Split(content, "<PlandexIncorrect/>")
+ problem = split[0]
+ } else if strings.Contains(content, "<PlandexReplacements>") {
+ split := strings.Split(content, "<PlandexReplacements>")
+ problem = split[0]
+ }
+
+ return buildValidateResult{valid: false, updated: updated, problem: problem}, nil
}
-func (fileState *activeBuildStreamFileState) validationRetryOrError(buildCtx context.Context, validateParams buildValidateParams, err error) (bool, string, error) {
+func (fileState *activeBuildStreamFileState) validationRetryOrError(buildCtx context.Context, validateParams buildValidateParams, err error) (buildValidateResult, error) {
log.Printf("Handling validation error for file: %s", fileState.filePath)
if fileState.validationNumRetry < MaxBuildErrorRetries {
fileState.validationNumRetry++
@@ -350,14 +417,14 @@ func (fileState *activeBuildStreamFileState) validationRetryOrError(buildCtx con
if activePlan == nil {
log.Printf("Active plan not found for plan ID %s and branch %s",
fileState.plan.Id, fileState.branch)
- return false, "", fmt.Errorf("active plan not found for plan ID %s and branch %s",
+ return buildValidateResult{}, fmt.Errorf("active plan not found for plan ID %s and branch %s",
fileState.plan.Id, fileState.branch)
}
select {
case <-buildCtx.Done():
log.Printf("Context canceled during retry wait")
- return false, "", context.Canceled
+ return buildValidateResult{}, context.Canceled
case <-time.After(time.Duration(fileState.validationNumRetry*fileState.validationNumRetry)*200*time.Millisecond + time.Duration(rand.Intn(500))*time.Millisecond):
log.Printf("Retry wait completed, attempting validation again")
break
@@ -366,6 +433,6 @@ func (fileState *activeBuildStreamFileState) validationRetryOrError(buildCtx con
return fileState.buildValidate(buildCtx, validateParams)
} else {
log.Printf("Max retries (%d) exceeded, returning error", MaxBuildErrorRetries)
- return false, "", err
+ return buildValidateResult{}, err
}
}
diff --git a/app/server/model/plan/build_whole_file.go b/app/server/model/plan/build_whole_file.go
index 544a6f9e..639a2bdf 100644
--- a/app/server/model/plan/build_whole_file.go
+++ b/app/server/model/plan/build_whole_file.go
@@ -2,6 +2,7 @@ package plan
import (
"context"
+ "errors"
"fmt"
"log"
"math/rand"
@@ -59,12 +60,8 @@ func (fileState *activeBuildStreamFileState) buildWholeFileFallback(buildCtx con
var prediction string
- if modelConfig.BaseModelConfig.PredictedOutputEnabled {
+ if modelConfig.BaseModelConfig.PredictedOutputEnabled && comments != "" {
prediction = `
-## Comments
-
-No comments
-
<PlandexWholeFile>
` + originalFile + `
</PlandexWholeFile>
@@ -97,6 +94,11 @@ No comments
})
if err != nil {
+ if errors.Is(err, context.Canceled) {
+ log.Printf("Context canceled during buildWholeFile")
+ return "", err
+ }
+
return "", fmt.Errorf("error calling model: %v", err)
}