Skip to content

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

fixes and improvements for context loading, general stream buffering, db pooling in dev, 'context show' command

Dane Schneider4894b485ef82
diff --git a/app/cli/cmd/context_show.go b/app/cli/cmd/context_show.go
index 795f975f..07d0b3f1 100644
--- a/app/cli/cmd/context_show.go
+++ b/app/cli/cmd/context_show.go
@@ -3,27 +3,61 @@ package cmd
import (
"fmt"
"log"
+ "plandex/api"
+ "plandex/auth"
+ "plandex/lib"
+ "strconv"
"github.com/spf13/cobra"
)
func init() {
- contextCmd.AddCommand(contextShowCmd)
+ RootCmd.AddCommand(contextShowCmd)
}
var contextShowCmd = &cobra.Command{
- Use: "show [context-id]",
- Short: "Show the body of a context",
+ Use: "show [name-or-index]",
+ Short: "Show the body of a context by name or list index",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
- contextId := args[0]
+ auth.MustResolveAuthWithOrg()
+ lib.MustResolveProject()
- planId, branch, err := getCurrentPlanAndBranch()
+ nameOrIndex := args[0]
+
+ // Get list of contexts first
+ contexts, err := api.Client.ListContext(lib.CurrentPlanId, lib.CurrentBranch)
if err != nil {
- return err
+ log.Printf("Error listing contexts: %v\n", err)
+ return fmt.Errorf("error listing contexts: %v", err)
+ }
+
+ var contextId string
+
+ // Try parsing as index first
+ if idx, err := strconv.Atoi(nameOrIndex); err == nil {
+ // Convert to 0-based index
+ idx--
+ if idx < 0 || idx >= len(contexts) {
+ return fmt.Errorf("invalid context index: %s", nameOrIndex)
+ }
+ contextId = contexts[idx].Id
+ } else {
+ // Try finding by name
+ found := false
+ for _, ctx := range contexts {
+ if ctx.Name == nameOrIndex || ctx.FilePath == nameOrIndex {
+ contextId = ctx.Id
+ found = true
+ break
+ }
+ }
+ if !found {
+ return fmt.Errorf("no context found with name: %s", nameOrIndex)
+ }
}
- res, apiErr := api.GetContextBody(planId, branch, contextId)
+ res, apiErr := api.Client.GetContextBody(lib.CurrentPlanId, lib.CurrentBranch, contextId)
if apiErr != nil {
log.Printf("Error getting context body: %v\n", apiErr)
return fmt.Errorf("error getting context body: %v", apiErr)
diff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.go
index 49d3cf04..b90f17b0 100644
--- a/app/cli/cmd/tell.go
+++ b/app/cli/cmd/tell.go
@@ -95,8 +95,6 @@ func doTell(cmd *cobra.Command, args []string) {
AutoContext: tellAutoContext,
})
- maybeShowDiffs()
-
if tellAutoApply {
lib.MustApplyPlan(lib.CurrentPlanId, lib.CurrentBranch, true, autoCommit, !autoCommit)
}
diff --git a/app/cli/lib/context_display.go b/app/cli/lib/context_display.go
index 6b9e0e5a..03a208d6 100644
--- a/app/cli/lib/context_display.go
+++ b/app/cli/lib/context_display.go
@@ -1,3 +1,4 @@
+
package lib
import "github.com/plandex/plandex/shared"
@@ -31,3 +32,21 @@ func GetContextLabelAndIcon(contextType shared.ContextType) (string, string) {
return lbl, icon
}
+
+func FindContextByIndex(contexts []*shared.Context, index int) *shared.Context {
+ // Convert to 0-based index
+ index--
+ if index < 0 || index >= len(contexts) {
+ return nil
+ }
+ return contexts[index]
+}
+
+func FindContextByName(contexts []*shared.Context, name string) *shared.Context {
+ for _, ctx := range contexts {
+ if ctx.Name == name || ctx.FilePath == name {
+ return ctx
+ }
+ }
+ return nil
+}
diff --git a/app/cli/lib/context_load.go b/app/cli/lib/context_load.go
index 2568bc2c..e829980c 100644
--- a/app/cli/lib/context_load.go
+++ b/app/cli/lib/context_load.go
@@ -21,7 +21,13 @@ import (
)
func MustLoadContext(resources []string, params *types.LoadContextParams) {
- term.StartSpinner("📥 Loading context...")
+ if params.DefsOnly {
+ term.StartSpinner("🗺️ Building map...")
+ } else if params.NamesOnly {
+ term.StartSpinner("🌳 Loading directory tree...")
+ } else {
+ term.StartSpinner("📥 Loading context...")
+ }
onErr := func(err error) {
term.StopSpinner()
@@ -396,9 +402,18 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {
if params.DefsOnly {
for _, inputPath := range toLoadMapPaths {
+ var name string
+ if inputPath == "." {
+ name = "cwd"
+ } else if inputPath == ".." {
+ name = "parent"
+ } else {
+ name = inputPath
+ }
+
loadContextReq = append(loadContextReq, &shared.LoadContextParams{
ContextType: shared.ContextMapType,
- Name: inputPath,
+ Name: name,
MapInputs: mapInputsByPath[inputPath],
FilePath: inputPath,
})
diff --git a/app/cli/lib/context_paths.go b/app/cli/lib/context_paths.go
index d443fa52..e930f887 100644
--- a/app/cli/lib/context_paths.go
+++ b/app/cli/lib/context_paths.go
@@ -36,10 +36,10 @@ func ParseInputPaths(fileOrDirPaths []string, params *types.LoadContextParams) (
return filepath.SkipDir
}
- if !(params.Recursive || params.NamesOnly) {
+ if !(params.Recursive || params.NamesOnly || params.DefsOnly) {
// log.Println("path", path, "info.Name()", info.Name())
- return fmt.Errorf("cannot process directory %s: --recursive or --tree flag not set", path)
+ return fmt.Errorf("cannot process directory %s: requires --recursive/-r, --tree, or --map flag", path)
}
// calculate directory depth from base
diff --git a/app/cli/lib/context_update.go b/app/cli/lib/context_update.go
index ba7904ee..5c0f38fd 100644
--- a/app/cli/lib/context_update.go
+++ b/app/cli/lib/context_update.go
@@ -267,6 +267,12 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool, maybeContexts []*shared.C
sha := hex.EncodeToString(hash[:])
if sha != context.Sha {
+ // log.Println()
+ // log.Println("context.FilePath", context.FilePath)
+ // log.Println("context.Sha", context.Sha, "sha", sha)
+ // log.Println("fileContent", string(fileContent))
+ // log.Println()
+
body := string(fileContent)
numTokens, err := shared.GetNumTokens(body)
@@ -576,10 +582,13 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool, maybeContexts []*shared.C
}
if len(req) > 0 {
+ // log.Println("updating context")
res, apiErr := api.Client.UpdateContext(CurrentPlanId, CurrentBranch, req)
if apiErr != nil {
return nil, fmt.Errorf("failed to update context: %v", apiErr)
}
+ // log.Println("updated context")
+ // log.Println("res.Msg", res.Msg)
msg = res.Msg
}
diff --git a/app/cli/stream_tui/update.go b/app/cli/stream_tui/update.go
index e5559d62..dbea9313 100644
--- a/app/cli/stream_tui/update.go
+++ b/app/cli/stream_tui/update.go
@@ -349,6 +349,7 @@ func (m *streamUIModel) streamUpdate(msg *shared.StreamMessage, deferUIUpdate bo
return m, tea.Batch(cmds...)
case shared.StreamMessageLoadContext:
+ log.Println("Stream message auto-load context")
msg, err := lib.AutoLoadContextFiles(msg.LoadContextFiles)
if err != nil {
log.Println("failed to auto load context files:", err)
diff --git a/app/server/db/context_helpers.go b/app/server/db/context_helpers.go
index acbe3fb0..5e58372a 100644
--- a/app/server/db/context_helpers.go
+++ b/app/server/db/context_helpers.go
@@ -283,15 +283,21 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextRespon
}
existingContextsByName := make(map[string]bool)
for _, context := range existingContexts {
- existingContextsByName[context.Name] = true
+ composite := strings.Join([]string{context.Name, string(context.ContextType)}, "|")
+ existingContextsByName[composite] = true
}
+ var filteredReq []*shared.LoadContextParams
for _, context := range *req {
- if existingContextsByName[context.Name] {
- log.Println("Context already loaded:", context.Name)
- continue
+ composite := strings.Join([]string{context.Name, string(context.ContextType)}, "|")
+ if !existingContextsByName[composite] {
+ filteredReq = append(filteredReq, context)
}
+ }
+
+ *req = filteredReq
+ for _, context := range *req {
tempId := uuid.New().String()
var numTokens int
@@ -379,6 +385,9 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextRespon
if mapContext, ok := mapContextsByFilePath[params.FilePath]; ok {
context = mapContext
} else {
+ // log.Println("tempId", tempId, "params.FilePath", params.FilePath, "sha", sha)
+ // log.Println("params.Body", params.Body)
+
context = Context{
// Id generated by db layer
OrgId: orgId,
@@ -509,8 +518,6 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
return nil, fmt.Errorf("total context body size exceeds limit (size %.2f MB, limit %d MB)", float64(totalBodySize)/1024/1024, int(shared.MaxContextBodySize)/1024/1024)
}
- //test
-
var updatedContexts []*shared.Context
numFiles := 0
@@ -521,6 +528,8 @@ 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) {
@@ -682,8 +691,13 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
context.NumTokens = newNumTokens
} else {
context.Body = params.Body
+ hash := sha256.Sum256([]byte(context.Body))
+ context.Sha = hex.EncodeToString(hash[:])
}
+ // log.Println("storing context", id)
+ // log.Printf("context name: %s, sha: %s\n", context.Name, context.Sha)
+
err := StoreContext(context)
if err != nil {
@@ -691,6 +705,9 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
return
}
+ // log.Println("stored context", id)
+ // log.Println()
+
errCh <- nil
}(id, params)
}
diff --git a/app/server/db/db.go b/app/server/db/db.go
index 235ef03e..66f8c647 100644
--- a/app/server/db/db.go
+++ b/app/server/db/db.go
@@ -46,6 +46,9 @@ func Connect() error {
if os.Getenv("GOENV") == "production" {
Conn.SetMaxOpenConns(30) // Allow up to 30 concurrent connections per task
Conn.SetMaxIdleConns(30) // Keep up to 30 idle connections for reuse
+ } else {
+ Conn.SetMaxOpenConns(10) // Allow up to 10 concurrent connections per task
+ Conn.SetMaxIdleConns(10) // Keep up to 10 idle connections for reuse
}
_, err = Conn.Exec("SET TIMEZONE='UTC';")
diff --git a/app/server/handlers/plans_context.go b/app/server/handlers/plans_context.go
index 9de9ee07..1f306c8f 100644
--- a/app/server/handlers/plans_context.go
+++ b/app/server/handlers/plans_context.go
@@ -92,7 +92,7 @@ func GetContextBodyHandler(w http.ResponseWriter, r *http.Request) {
}()
}
- dbContexts, err := db.GetPlanContexts(auth.OrgId, planId, false, false)
+ dbContexts, err := db.GetPlanContexts(auth.OrgId, planId, true, false)
if err != nil {
log.Printf("Error getting contexts: %v\n", err)
http.Error(w, "Error getting contexts: "+err.Error(), http.StatusInternalServerError)
diff --git a/app/server/handlers/plans_exec.go b/app/server/handlers/plans_exec.go
index 51f69192..57d0a67c 100644
--- a/app/server/handlers/plans_exec.go
+++ b/app/server/handlers/plans_exec.go
@@ -403,12 +403,15 @@ func AutoLoadContextHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ log.Println("AutoLoadContextHandler - loading contexts")
res, dbContexts := loadContexts(w, r, auth, &requestBody, plan, branch)
if res == nil {
return
}
+ log.Println("AutoLoadContextHandler - updating active plan")
+
modelPlan.UpdateActivePlan(planId, branch, func(activePlan *types.ActivePlan) {
activePlan.Contexts = append(activePlan.Contexts, dbContexts...)
for _, dbContext := range dbContexts {
diff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.go
index e480c375..35f2c1da 100644
--- a/app/server/model/plan/tell_stream.go
+++ b/app/server/model/plan/tell_stream.go
@@ -228,15 +228,18 @@ mainLoop:
errCh <- nil
}()
- if req.IsChatOnly || len(autoLoadContextFiles) > 0 {
- // no auto continue for chat only
- if req.IsChatOnly {
- shouldContinue = false
- }
+ if req.IsChatOnly || len(autoLoadContextFiles) > 0 || active.DidVerifyDiff {
// if we're auto-loading context files, we always want to continue for at least another iteration with the loaded context (even if it's chat only)
if len(autoLoadContextFiles) > 0 {
+ log.Printf("Auto loading context files, so continuing")
shouldContinue = true
+ } else if req.IsChatOnly {
+ log.Printf("Chat only, won't continue")
+ shouldContinue = false
+ } else if active.DidVerifyDiff {
+ log.Printf("Did verify diff, won't continue")
+ shouldContinue = false
}
errCh <- nil
@@ -404,6 +407,7 @@ mainLoop:
ap.CurrentReplyDoneCh = nil
})
+ log.Printf("len(autoLoadContextFiles): %d\n", len(autoLoadContextFiles))
if len(autoLoadContextFiles) > 0 {
log.Println("Sending stream message to load context files")
@@ -413,6 +417,9 @@ mainLoop:
})
active.FlushStreamBuffer()
+ // Force a small delay to ensure message is processed
+ time.Sleep(100 * time.Millisecond)
+
log.Println("Waiting for client to auto load context (30s timeout)")
select {
@@ -429,6 +436,11 @@ mainLoop:
}
// if we're auto-loading context files, we always want to continue for at least another iteration with the loaded context
+ log.Printf("req.AutoContinue: %v\n", req.AutoContinue)
+ log.Printf("shouldContinue: %v\n", shouldContinue)
+ log.Printf("iteration: %d\n", iteration)
+ log.Printf("MaxAutoContinueIterations: %d\n", MaxAutoContinueIterations)
+
if len(autoLoadContextFiles) > 0 || (req.AutoContinue && shouldContinue && iteration < MaxAutoContinueIterations) {
log.Println("Auto continue plan")
// continue plan
@@ -505,17 +517,6 @@ mainLoop:
}
}
- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {
- ap.CurrentReplyContent += content
- ap.NumTokens++
- })
-
- // log.Printf("Sending stream msg: %s", content)
- active.Stream(shared.StreamMessage{
- Type: shared.StreamMessageReply,
- ReplyChunk: content,
- })
-
replyParser.AddChunk(content, true)
parserRes := replyParser.Read()
files := parserRes.Files
@@ -612,6 +613,17 @@ mainLoop:
return
}
+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {
+ ap.CurrentReplyContent += content
+ ap.NumTokens++
+ })
+
+ // log.Printf("Sending stream msg: %s", content)
+ active.Stream(shared.StreamMessage{
+ Type: shared.StreamMessageReply,
+ ReplyChunk: content,
+ })
+
// log.Println("Content:", content)
// log.Println("Current reply content:", active.CurrentReplyContent)
// log.Println("Current file:", currentFile)
diff --git a/app/server/model/prompts/create.go b/app/server/model/prompts/create.go
index fb6d0f10..a4cc89c9 100644
--- a/app/server/model/prompts/create.go
+++ b/app/server/model/prompts/create.go
@@ -2,6 +2,7 @@ package prompts
import (
"fmt"
+ "time"
"github.com/plandex/plandex/shared"
)
@@ -82,7 +83,7 @@ In response to the user's latest prompt, do the following:
- Decide whether you've been given enough information to load necessary context and make a plan (if you've been given a task) or give a helpful response to the user (if you're responding in chat form). In general, do your best with whatever information you've been provided. Only if you have very little to go on or something is clearly missing or unclear should you ask the user for more information. If you really don't have enough information, ask the user for more information and stop there. 'Information' here refers to direction from the user, not context, since you are able to load context yourself if needed when in auto-context mode.
- - 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.
+ - 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."
@@ -90,27 +91,33 @@ In response to the user's latest prompt, do the following:
- For each step in the plan, also note which files will be needed in context to complete the step. This MUST include *all* files that will be updated, but can also include other files that will be helpful, like examples of similar code, documentation, and so on. Be thorough and exhaustive in listing all files that are necessary or helpful to completing the plan effectively.
- - If 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 any code similar to what the user is asking for exists in the project, include at least one example so that you can use it as a reference for how to implement the user's request in a way that is consistent with the existing code. If a user asks you to implement an API endpoint, for example, and you see that there is an existing endpoint in the project that is similar, include it in the context so that you can use it as a reference. Similarly, if a task requires implementing a database migration, and you see that there is an existing migration, include it in the context so that you can use it as a reference. If you are asked to implement a frontend or UI feature, and you see that there are existing UI components, pages, styles, or other related code, include some of it so that you can use it as a reference and keep a consistent style, both in terms of the code and the user experience.
- - For each step in the plan, also note if the necessary files are *already* in context. If so, you MUST NOT load them again—they must be omitted from the 'Load Context' list.
+ - If you'll be using any definitions from a file—calling a function or method, instantiating a class, accessing a variable, using a type, and so on—include that file in the context. Include the file even if it's not being modified. For example, if you are creating an object that uses a type defined in a 'types.ts' file, include the 'types.ts' file in the context even if you're not updating the 'types.ts' file. If you're calling a function from a 'utils.py' file, include the 'utils.py' file in the context even if you're not updating the 'utils.py' file. This will ensure you correctly call functions/methods, use types, use variables and constants, etc. so it is *critical* that you include all files necessary to make a good plan that is well-integrated with the existing code.
- - At the end of your response, list *all* of those files (which are *not* already in context) in this format:
+ - Include any files that the user has mentioned directly or indirectly in the prompt. If a user has mentioned files by name, path, by describing them, by referring to definitions or other code they contain, or by referring to them in any other way, include those files.
+
+ - If you aren't sure whether a file will be helpful or not, but you think it might be, include it in the 'Load Context' list. It's better to load more context than you need than to miss an important or helpful file.
+
+ - For each step in the plan, note if any of the necessary files are *already* in context. If so, you MUST NOT load them again—they must be omitted from the 'Load Context' list.
+
+ - At the end of your response, list *all* of those files (which are *not* already in context) in this EXACT format:
### Load Context
- ` + "`src/main.rs`" + `
- ` + "`lib/term.go`" + `
+
+ You MUST use the exact same format as shown directly above. First the '### Load Context' header, then a blank line, then the list of files, with a '-' and a space before each file path. List files individually—do not list directories. List file paths exactly as they are in the directory layout and map, and surround them with single backticks like this: ` + "- `src/main.rs`." + ` Each file path in the list MUST be on its own line. Use this EXACT format.
- Then immediately *stop there* so these files can be loaded before you continue. 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`." + `
+ After the 'Load Context' list, you MUST ALWAYS immediately *stop there* so these files can be loaded before you continue.
- - If instead you already have enough information from the directory layout, map, and current context to make a detailed plan or respond effectively to the user, then explicitly say "No context needs to be loaded." and continue on to the instructions below.
-
-You ABSOLUTELY MUST do the above steps *BEFORE* continuing to the instructions below. Listing subtasks, responding to the user, or writing code in file blocks MUST come *after* you've done the above steps.
+ - If instead you already have enough information from the directory layout, map, and current context to make a detailed plan or respond effectively to the user and so you won't need to load any additional context, then explicitly say "No context needs to be loaded." and continue on to the instructions below.
-If your response included a 'Load Context' list as described above, you ABSOLUTELY MUST STOP after listing all those files. Do NOT continue to the instructions below and DO NOT output anything else after the 'Load Context' list—it must be the final text in your response.
+ - Every response MUST end with either the 'Load Context' list in the exact format described above or the exact phrase "No context needs to be loaded." and nothing else. This MUST be the final text in your response.
-The 'Load Context' list MUST *ONLY* include files that are present in the directory layout or map and are not *already* in context. You MUST NOT include any files that are already in context or that do not exist in the directory layout or map. Do NOT include files that will be created during the plan but are not yet present in the directory layout or map.
+The 'Load Context' list MUST *ONLY* include files that are present in the directory layout or map and are not *already* in context. You MUST NOT include any files that are already in context or that do not exist in the directory layout or map. **Do NOT include files that need to be created**—only files that already exist.
-Don't output multiple lists of the files to load in your response. There should only be one 'Load Context' list in your response.
+Don't output multiple lists of the files to load in your response. There MUST only be one 'Load Context' list in your response and no other list of the files to load.
[END OF YOUR INSTRUCTIONS]
`
@@ -131,7 +138,7 @@ func GetCreatePrompt(params CreatePromptParams) string {
if params.AutoContext {
prompt += `
- 1. Decide whether you've been given enough information to make a plan.
+ 1. Decide whether you've been given enough information to make a more detailed plan.
- Do your best with whatever information you've been provided. Choose sensible values and defaults where appropriate. Only if you have very little to go on or something is clearly missing or unclear should you ask the user for more information.
a. If you really don't have enough information to make a plan:
- Explicitly say "I need more information to make a plan for this task."
@@ -148,7 +155,7 @@ func GetCreatePrompt(params CreatePromptParams) string {
}
prompt += `2. Decide whether this task is small enough to be completed in a single response.
- a. If so, describe the task to be done and what your approach will be, then write out the code to complete the task. Include only lines that will change and lines that are necessary to know where the changes should be applied. Precede the code block with the file path like this '- file_path:'--for example:
+ a. If so, describe in detail the task to be done and what your approach will be, then write out the code to complete the task. Include only lines that will change and lines that are necessary to know where the changes should be applied. Precede the code block with the file path like this '- file_path:'--for example:
- src/main.rs:
- lib/term.go:
- main.py:
@@ -176,6 +183,7 @@ func GetCreatePrompt(params CreatePromptParams) string {
if params.AutoContext {
prompt += `
- Since you are in 'auto-context mode', below the description of each subtask, you MUST include a comma-separated 'Uses:' list of the files that will be needed in context to complete each task. Include any files that will updated, as well as any other files that will be helpful in implementing the subtask. ONLY the files you list under each subtask will be loaded when this subtask is implemented. List files individually—do not list directories. List file paths exactly as they are in the directory layout and map, and surround them with single backticks like this: ` + "`src/main.rs`." + `
+ - Since you now have the context you need loaded, use it to make a more detailed plan than the plan you made in your previous response. Be thorough in your planning.
`
}
@@ -226,7 +234,7 @@ func GetCreatePrompt(params CreatePromptParams) string {
If you are outputting some code for illustrative or explanatory purpose and not because you are updating that code, you MUST NOT use a labelled file block. Instead output the label with NO PRECEDING DASH and NO COLON postfix. Use a conversational sentence like 'This code in src/main.rs.' to label the code. This is the only exception to the rule that all code blocks must be labelled with a file path. Labelled code blocks are ONLY for code that is being created or modified in the plan.
- As much as possible, the code you suggest should be robust, complete, and ready for production.
+ As much as possible, the code you suggest must be robust, complete, and ready for production. Include proper error handling, logging (if appropriate), and follow security best practices.
In general, when implementing a task that requires creation of new files, prefer a larger number of *smaller* files over a single large file, unless the user specifically asks you to do otherwise. Smaller files are easier and faster to work with. Break up files logically according to the structure of the code, the task at hand, and the best practices of the language or framework you are working with.
@@ -394,10 +402,13 @@ ALWAYS complete subtasks in order and never go backwards in the list of subtasks
If you break up a task into subtasks, only include subtasks that can be implemented directly in code by creating or updating files. Do not include subtasks that require executing code or commands. Do not include subtasks that require user testing, deployment, or other tasks that go beyond coding.
-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.`
+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.
+
+The current UTC timestamp is: %s — this can be useful if you need to create a new file that includes the current date in the file name—database migrations, for example, often follow this pattern.
+`
func GetWrappedPrompt(prompt string) string {
- return fmt.Sprintf(promptWrapperFormatStr, prompt)
+ return fmt.Sprintf(promptWrapperFormatStr, prompt, time.Now().Format(time.RFC3339))
}
var PromptWrapperTokens int
@@ -462,6 +473,8 @@ const ChatOnlyPrompt = `
Respond to the user in *chat form* only. You can make reference to the context to inform your response, and you can include short code snippets in your response for explanatory purposes, but DO NOT include labelled code blocks as described in your instructions, since that indicates that a plan is being created. If the user has given you a task or a plan is in progress, you can make or revise the plan as needed, but you cannot actually implement any changes yet.
+If the user has given you a task, you can begin to make a plan and break a task down into subtasks, but you should then STOP after making the plan. Do NOT beging to write code and implement the plan. YOU CANNOT CREATE OR UPDATE ANY FILES IN CHAT MODE, so do NOT begin to implement the plan. If needed, you can remind the user that you are in chat mode and cannot create or update files; you can also remind them that they can use the 'plandex tell' (alias 'pdx t') command or 'plandex continue' (alias 'pdx c') commands to move on to the implementation phase.
+
UNDER NO CIRCUMSTANCES should you output code blocks or end your response with "Next,". Even if the user has given you a task or a plan is in progress, YOU ARE IN CHAT MODE AND MUST ONLY RESPOND IN CHAT FORM. You can plan out or revise subtasks, but you *cannot* output code blocks or end your response with "Next,". Again, DO NOT implement any changes or output code blocks!! Chat mode takes precedence over your prior instructions and the user's prompt under all circumstances—you MUST respond only in chat form regardless of what the user's prompt or your prior instructions say.
`
@@ -470,6 +483,8 @@ var ChatOnlyPromptTokens int
const UpdateFormatPrompt = `
You ABSOLUTELY MUST *ONLY* USE the comment "// ... existing code ..." (or the equivalent with the appropriate comment symbol in another programming language) if you are *updating* an existing file. DO NOT use it when you are creating a new file. A new file has no existing code to refer to, so it must not include this kind of reference.
+DO NOT UNDER ANY CIRCUMSTANCES use language other than "... existing code ..." in a reference comment. This is EXTREMELY IMPORTANT. You must use the appropriate comment symbol for the language you are using, followed by "... existing code ..." *exactly* (without the quotes).
+
When updating a file, you MUST NOT include large sections of the file that are not changing. Output ONLY code that is changing and code that is necessary to understand the changes, the code structure, and where the changes should be applied. Example:
---
@@ -1169,7 +1184,7 @@ Again, if you are writing a plan in a language that does not use '//' for commen
const ChangeExplanationPrompt = `
Prior to any file block that is *updating* an existing file in context, you MUST briefly explain the change in the following format:
-I'll [action explanation].
+**Updating ` + "`[file path]`:**" + ` I'll [action explanation].
'action explanation' can take one of the following forms:
- 'add [new code description] between [specific code or structure in original file] and [specific code or structure in original file]'
@@ -1186,12 +1201,12 @@ When referring to the start of the file, use the exact language 'the start of th
When referring to the end of the file, use the exact language 'the end of the file'.
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:
-- I'll add the ` + "`doRequest`" + ` method between the constructor method and the ` + "`getUser`" + ` method.
-- I'll add the types for making the api call between the imports and the ` + "`init`" + ` method.
-- I'll overwrite the entire file with new code for the ` + "`update`" + ` CLI command.
-- I'll add the ` + "`update`" + ` function between the ` + "`get`" + ` and the end of the file.
+- **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.
-You ABSOLUTELY MUST use this template EXACTLY as described above. DO NOT CHANGE THE FORMATTING OR WORDING IN ANY WAY!
+You ABSOLUTELY MUST use this template EXACTLY as described above. DO NOT CHANGE THE FORMATTING OR WORDING IN ANY WAY! DO NOT OMIT ANY PART OF THE EXPLANATION AS DESCRIBED ABOVE. AND ABSOLUTELY DO NOT EVEN THINK ABOUT LEAVING OUT THIS MESSAGE! It is EXTREMELY IMPORTANT that you include this message in every file block that updates an existing file.
When creating a *new* file, do NOT include this explanation.
diff --git a/app/server/routes/routes.go b/app/server/routes/routes.go
index d715c698..96ce8420 100644
--- a/app/server/routes/routes.go
+++ b/app/server/routes/routes.go
@@ -115,6 +115,7 @@ func addApiRoutes(r *mux.Router, prefix string) {
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")
diff --git a/app/server/setup/setup.go b/app/server/setup/setup.go
index 0da9ad5f..85f1e9c0 100644
--- a/app/server/setup/setup.go
+++ b/app/server/setup/setup.go
@@ -46,6 +46,19 @@ func RegisterShutdownHook(hook func()) {
shutdownHooks = append(shutdownHooks, hook)
}
+func loggingMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ start := time.Now()
+
+ log.Println()
+ log.Printf("Request: %s %s", r.Method, r.URL.Path)
+ next.ServeHTTP(w, r)
+ log.Printf("Completed: %s %s in %v", r.Method, r.URL.Path, time.Since(start))
+ log.Println()
+ log.Println()
+ })
+}
+
func StartServer(handler http.Handler) {
if os.Getenv("GOENV") == "development" {
log.Println("In development mode.")
@@ -67,6 +80,9 @@ func StartServer(handler http.Handler) {
externalPort = "8080"
}
+ // Add logging middleware before the maxBytes middleware
+ handler = loggingMiddleware(handler)
+
// Apply the maxBytesMiddleware to limit request size to 100 MB
handler = maxBytesMiddleware(handler, 100<<20) // 100 MB limit
diff --git a/app/server/syntax/structural_edits.go b/app/server/syntax/structural_edits.go
index 4acf1df8..bfd5544e 100644
--- a/app/server/syntax/structural_edits.go
+++ b/app/server/syntax/structural_edits.go
@@ -259,7 +259,7 @@ func ApplyChanges(
}
}
- writeRefs := func(eof bool) {
+ writeRefs := func(eof bool) error {
numRefs := len(postRefBuffers)
if numRefs == 1 {
if verboseLogging {
@@ -287,6 +287,28 @@ func ApplyChanges(
if end < 1 {
end = 0
}
+
+ // Add detailed diagnostic logging for invalid slice bounds
+ if start > end {
+ 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 context
+ fmt.Printf("\nOriginal lines context:\n")
+ startContext := max(0, start-2)
+ endContext := min(len(originalLines), end+3)
+ for i := startContext; i < endContext; i++ {
+ fmt.Printf("line %d: %q\n", i+1, originalLines[i])
+ }
+
+ fmt.Printf("\nProposed lines context:\n")
+ fmt.Printf("=====================================\n\n")
+
+ return fmt.Errorf("invalid slice bounds: start(%d) > end(%d)", start, end)
+ }
+
if verboseLogging {
fmt.Printf("writing fullRef\n")
fmt.Printf("refStart: %d, oLineNum: %d\n", refStart, oLineNum)
@@ -346,6 +368,8 @@ func ApplyChanges(
write(postRefBuffers[i].String(), false)
}
}
+
+ return nil
}
for idx, pLine := range proposedLines {
@@ -538,7 +562,10 @@ func ApplyChanges(
fmt.Printf("closing ref, oLineNum: %d\n", oLineNum)
}
refOpen = false
- writeRefs(false)
+ err := writeRefs(false)
+ if err != nil {
+ return "", fmt.Errorf("error writing refs: %w", err)
+ }
write(pLine, !finalLine)
wroteRefs = true
}
@@ -578,7 +605,10 @@ func ApplyChanges(
}
if refOpen {
- writeRefs(true)
+ err := writeRefs(true)
+ if err != nil {
+ return "", fmt.Errorf("error writing refs: %w", err)
+ }
}
if verboseLogging {
diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.go
index 26ff4dd1..4cedf7eb 100644
--- a/app/server/types/active_plan.go
+++ b/app/server/types/active_plan.go
@@ -13,7 +13,7 @@ import (
"github.com/plandex/plandex/shared"
)
-const MaxStreamRate = 100 * time.Millisecond
+const MaxStreamRate = 70 * time.Millisecond
type ActiveBuild struct {
ReplyId string
@@ -158,61 +158,58 @@ func NewActivePlan(orgId, userId, planId, branch, prompt string, buildOnly, auto
}
func (ap *ActivePlan) FlushStreamBuffer() {
- // log.Println("ActivePlan: flush stream buffer")
-
ap.streamMu.Lock()
if len(ap.streamMessageBuffer) == 0 {
- // log.Println("ActivePlan: stream buffer empty")
ap.streamMu.Unlock()
return
}
- // log.Printf("ActivePlan: flushing %d messages from stream buffer\n", len(ap.streamMessageBuffer))
+ bufferToFlush := ap.streamMessageBuffer
+ ap.streamMessageBuffer = []shared.StreamMessage{}
+ ap.streamMu.Unlock()
- var msg shared.StreamMessage
- if len(ap.streamMessageBuffer) == 1 {
- log.Println("ActivePlan: flushing 1 message from stream buffer")
- msg = ap.streamMessageBuffer[0]
+ if len(bufferToFlush) == 1 {
+ ap.Stream(bufferToFlush[0])
} else {
- msg = shared.StreamMessage{
+ ap.Stream(shared.StreamMessage{
Type: shared.StreamMessageMulti,
- StreamMessages: ap.streamMessageBuffer,
- }
+ StreamMessages: bufferToFlush,
+ })
}
-
- ap.streamMessageBuffer = []shared.StreamMessage{}
-
- ap.streamMu.Unlock()
-
- ap.Stream(msg)
}
func (ap *ActivePlan) Stream(msg shared.StreamMessage) {
- // log.Printf("ActivePlan: received Stream message: %v\n", msg)
-
ap.streamMu.Lock()
- defer ap.streamMu.Unlock()
+ // Special messages bypass buffering
if msg.Type != shared.StreamMessageFinished &&
msg.Type != shared.StreamMessagePromptMissingFile {
+
if time.Since(ap.lastStreamMessageSent) < MaxStreamRate {
- // log.Println("ActivePlan: stream rate limiting -- buffering message")
+ // Buffer the message
ap.streamMessageBuffer = append(ap.streamMessageBuffer, msg)
+ ap.streamMu.Unlock()
return
} else if len(ap.streamMessageBuffer) > 0 {
- // log.Println("ActivePlan: stream buffer not empty -- flushing buffer before sending message")
+ // Need to flush buffer first
ap.streamMessageBuffer = append(ap.streamMessageBuffer, msg)
-
- // unlock before recursive call
+ bufferToFlush := ap.streamMessageBuffer
+ ap.streamMessageBuffer = []shared.StreamMessage{}
ap.streamMu.Unlock()
- ap.FlushStreamBuffer()
- ap.streamMu.Lock() // re-lock after recursive call
+
+ // Send as multi-message
+ ap.Stream(shared.StreamMessage{
+ Type: shared.StreamMessageMulti,
+ StreamMessages: bufferToFlush,
+ })
return
}
}
+ // Direct send path
msgJson, err := json.Marshal(msg)
if err != nil {
+ ap.streamMu.Unlock()
ap.StreamDoneCh <- &shared.ApiError{
Type: shared.ApiErrorTypeOther,
Status: http.StatusInternalServerError,
@@ -221,36 +218,31 @@ func (ap *ActivePlan) Stream(msg shared.StreamMessage) {
return
}
- // log.Printf("ActivePlan: sending stream message: %s\n", string(msgJson))
-
- if msg.Type == shared.StreamMessageFinished {
- // send full buffer if we got a finished message
- if len(ap.streamMessageBuffer) > 0 {
- // log.Println("ActivePlan: finished message -- sending stream buffer first")
+ if msg.Type == shared.StreamMessageFinished && len(ap.streamMessageBuffer) > 0 {
+ // Handle any remaining buffered messages before finishing
+ bufferToFlush := ap.streamMessageBuffer
+ ap.streamMessageBuffer = []shared.StreamMessage{}
+ ap.streamMu.Unlock()
- // unlock before recursive call
- ap.streamMu.Unlock()
- ap.FlushStreamBuffer()
- ap.streamMu.Lock() // re-lock after recursive call
+ ap.Stream(shared.StreamMessage{
+ Type: shared.StreamMessageMulti,
+ StreamMessages: bufferToFlush,
+ })
- // sleep a little before the final message
- time.Sleep(50 * time.Millisecond)
- }
+ time.Sleep(50 * time.Millisecond)
+ ap.Stream(msg) // Resend the finish message
+ return
}
- // log.Println("ActivePlan: sending stream message:", msg.Type)
-
ap.streamCh <- string(msgJson)
- // log.Println("ActivePlan: sent stream message:", msg.Type)
-
now := time.Now()
if now.After(ap.lastStreamMessageSent) {
ap.lastStreamMessageSent = now
}
+ ap.streamMu.Unlock()
if msg.Type == shared.StreamMessageFinished {
- // Wait briefly to allow last stream message to be sent
time.Sleep(100 * time.Millisecond)
ap.StreamDoneCh <- nil
}
diff --git a/app/shared/plan_settings.go b/app/shared/plan_model_settings.go
similarity index 100%
rename from app/shared/plan_settings.go
rename to app/shared/plan_model_settings.go