Skip to content

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

heartbeats for locks and model streams

Dane Schneiderd8fc659647ce
diff --git a/app/cli/cmd/checkout.go b/app/cli/cmd/checkout.go
index 3481b0ce..0d34e1fc 100644
--- a/app/cli/cmd/checkout.go
+++ b/app/cli/cmd/checkout.go
@@ -9,6 +9,7 @@ import (
"plandex/term"
"strings"
+ "github.com/fatih/color"
"github.com/plandex/plandex/shared"
"github.com/spf13/cobra"
)
@@ -119,7 +120,7 @@ func checkout(cmd *cobra.Command, args []string) {
}
term.StopSpinner()
- fmt.Printf("✅ Created branch '%s'\n", branchName)
+ fmt.Printf("✅ Created branch %s\n", color.New(color.Bold, color.FgHiGreen).Sprint(branchName))
}
err := lib.WriteCurrentBranch(branchName)
@@ -129,7 +130,7 @@ func checkout(cmd *cobra.Command, args []string) {
return
}
- fmt.Printf("✅ Checked out branch '%s'\n", branchName)
+ fmt.Printf("✅ Checked out branch %s\n", color.New(color.Bold, color.FgHiGreen).Sprint(branchName))
fmt.Println()
term.PrintCmds("", "load", "tell", "branches", "delete-branch")
diff --git a/app/cli/cmd/convo.go b/app/cli/cmd/convo.go
index 24deb138..3a1afe98 100644
--- a/app/cli/cmd/convo.go
+++ b/app/cli/cmd/convo.go
@@ -25,6 +25,8 @@ func init() {
RootCmd.AddCommand(convoCmd)
}
+const stoppedEarlyMsg = "You stopped the reply early"
+
func convo(cmd *cobra.Command, args []string) {
auth.MustResolveAuthWithOrg()
lib.MustResolveProject()
@@ -46,9 +48,6 @@ func convo(cmd *cobra.Command, args []string) {
var author string
if msg.Role == "assistant" {
author = "🤖 Plandex"
- if msg.Stopped {
- author += " | 🛑 stopped early"
- }
} else if msg.Role == "user" {
author = "💬 You"
} else {
@@ -71,6 +70,11 @@ func convo(cmd *cobra.Command, args []string) {
header := fmt.Sprintf("#### %d | %s | %s | %d 🪙 ", i+1,
author, formattedTs, msg.Tokens)
convMarkdown = append(convMarkdown, header, msg.Message, "")
+
+ if msg.Stopped {
+ convMarkdown = append(convMarkdown, fmt.Sprintf("🛑 **%s**", stoppedEarlyMsg), "")
+ }
+
totalTokens += msg.Tokens
}
@@ -80,6 +84,8 @@ func convo(cmd *cobra.Command, args []string) {
return
}
+ markdownString = strings.ReplaceAll(markdownString, stoppedEarlyMsg, color.New(color.FgHiRed).Sprint(stoppedEarlyMsg))
+
output :=
fmt.Sprintf("\n%s", markdownString) +
term.GetDivisionLine() +
diff --git a/app/cli/stream_tui/update.go b/app/cli/stream_tui/update.go
index f44389cc..45d157a1 100644
--- a/app/cli/stream_tui/update.go
+++ b/app/cli/stream_tui/update.go
@@ -52,6 +52,15 @@ func (m streamUIModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {
case bubbleKey.Matches(msg, m.keymap.quit):
return m, tea.Quit
+
+ case bubbleKey.Matches(msg, m.keymap.stop):
+ apiErr := api.Client.StopPlan(lib.CurrentPlanId, lib.CurrentBranch)
+ if apiErr != nil {
+ log.Println("stop plan api error:", apiErr)
+ m.apiErr = apiErr
+ return m, nil
+ }
+
case bubbleKey.Matches(msg, m.keymap.scrollDown) && !m.promptingMissingFile:
m.scrollDown()
case bubbleKey.Matches(msg, m.keymap.scrollUp) && !m.promptingMissingFile:
diff --git a/app/server/db/db.go b/app/server/db/db.go
index 870a6bb6..d2f585b8 100644
--- a/app/server/db/db.go
+++ b/app/server/db/db.go
@@ -92,7 +92,7 @@ func MigrationsUp() error {
// log.Println("ran down migrations - database was reset")
// }
- // go back one migration
+ // Uncomment below to go back ONE migration
// if os.Getenv("GOENV") == "development" {
// err = m.Steps(-1)
// if err != nil {
diff --git a/app/server/db/git.go b/app/server/db/git.go
index c616c09f..f9fe3685 100644
--- a/app/server/db/git.go
+++ b/app/server/db/git.go
@@ -10,7 +10,6 @@ import (
"time"
"github.com/fatih/color"
- "github.com/lib/pq"
)
func init() {
@@ -268,192 +267,3 @@ func gitCommit(repoDir, commitMsg string) error {
return nil
}
-
-// distributed locking to ensure only one user can write to a plan repo at a time
-// multiple readers are allowed, but read locks block writes
-// write lock is exclusive (blocks both reads and writes)
-const lockTimeout = 60 * time.Second
-
-func LockRepo(orgId, userId, planId, branch string, scope LockScope) (string, error) {
- return lockRepo(orgId, userId, planId, "", branch, scope, 0)
-}
-
-func LockRepoForBuild(orgId, userId, planId, planBuildId, branch string, scope LockScope) (string, error) {
- return lockRepo(orgId, userId, planId, planBuildId, branch, scope, 0)
-}
-
-func lockRepo(orgId, userId, planId, planBuildId, branch string, scope LockScope, numRetry int) (string, error) {
- tx, err := Conn.Begin()
- if err != nil {
- return "", fmt.Errorf("error starting transaction: %v", err)
- }
-
- // Ensure that rollback is attempted in case of failure
- defer func() {
- if err != nil {
- if rbErr := tx.Rollback(); rbErr != nil {
- log.Printf("transaction rollback error: %v\n", rbErr)
- } else {
- log.Println("transaction rolled back")
- }
- }
- }()
-
- query := "SELECT id, org_id, user_id, plan_id, plan_build_id, scope, branch, created_at FROM repo_locks WHERE plan_id = $1 FOR UPDATE"
- queryArgs := []interface{}{planId}
-
- var locks []*repoLock
-
- fn := func() error {
- rows, err := tx.Query(query, queryArgs...)
- if err != nil {
- return fmt.Errorf("error getting repo locks: %v", err)
- }
-
- defer rows.Close()
-
- var expiredLockIds []string
-
- now := time.Now()
- for rows.Next() {
- var lock repoLock
- if err := rows.Scan(&lock.Id, &lock.OrgId, &lock.UserId, &lock.PlanId, &lock.PlanBuildId, &lock.Scope, &lock.Branch, &lock.CreatedAt); err != nil {
- return fmt.Errorf("error scanning repo lock: %v", err)
- }
- if now.Sub(lock.CreatedAt) < lockTimeout {
- locks = append(locks, &lock)
- } else {
- expiredLockIds = append(expiredLockIds, lock.Id)
- }
- }
-
- if len(expiredLockIds) > 0 {
- query := "DELETE FROM repo_locks WHERE id = ANY($1)"
- _, err := tx.Exec(query, pq.Array(expiredLockIds))
- if err != nil {
- return fmt.Errorf("error removing expired locks: %v", err)
- }
- }
-
- return nil
- }
- if err := fn(); err != nil {
- return "", err
- }
-
- canAcquire := true
- canRetry := true
-
- // log.Println("locks:")
- // spew.Dump(locks)
-
- for _, lock := range locks {
- lockBranch := ""
- if lock.Branch != nil {
- lockBranch = *lock.Branch
- }
-
- if scope == LockScopeRead {
- canAcquireThisLock := lock.Scope == LockScopeRead && lockBranch == branch
- if !canAcquireThisLock {
- canAcquire = false
- }
- } else if scope == LockScopeWrite {
- canAcquire = false
-
- // if lock is for the same plan plan and branch, allow parallel writes
- if planId == lock.PlanId && branch == lockBranch {
- canAcquire = true
- }
-
- if lock.Scope == LockScopeWrite && lockBranch == branch {
- canRetry = false
- }
- } else {
- err = fmt.Errorf("invalid lock scope: %v", scope)
- return "", err
- }
- }
-
- if !canAcquire {
- if canRetry {
- // 10 second timeout
- if numRetry > 20 {
- err = fmt.Errorf("plan is currently being updated by another user")
- return "", err
- }
- time.Sleep(500 * time.Millisecond)
- return lockRepo(orgId, userId, planId, planBuildId, branch, scope, numRetry+1)
- }
- err = fmt.Errorf("plan is currently being updated by another user")
- return "", err
- }
-
- // Insert the new lock
- var lockPlanBuildId *string
- if planBuildId != "" {
- lockPlanBuildId = &planBuildId
- }
-
- var lockBranch *string
- if branch != "" {
- lockBranch = &branch
- }
-
- newLock := &repoLock{
- OrgId: orgId,
- UserId: userId,
- PlanId: planId,
- PlanBuildId: lockPlanBuildId,
- Scope: scope,
- Branch: lockBranch,
- }
- // log.Println("newLock:")
- // spew.Dump(newLock)
-
- insertQuery := "INSERT INTO repo_locks (org_id, user_id, plan_id, plan_build_id, scope, branch) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id"
- err = tx.QueryRow(
- insertQuery,
- newLock.OrgId,
- newLock.UserId,
- newLock.PlanId,
- newLock.PlanBuildId,
- newLock.Scope,
- newLock.Branch,
- ).Scan(&newLock.Id)
- if err != nil {
- return "", fmt.Errorf("error inserting new lock: %v", err)
- }
-
- branches, err := GitListBranches(orgId, planId)
- if err != nil {
- return "", fmt.Errorf("error getting branches: %v", err)
- }
-
- log.Println("branches:", branches)
-
- if branch != "" {
- // checkout the branch
- err = gitCheckoutBranch(getPlanDir(orgId, planId), branch)
- if err != nil {
- return "", fmt.Errorf("error checking out branch: %v", err)
- }
- }
-
- // Commit the transaction
- if err = tx.Commit(); err != nil {
- return "", fmt.Errorf("error committing transaction: %v", err)
- }
-
- return newLock.Id, nil
-}
-
-func UnlockRepo(id string) error {
- query := "DELETE FROM repo_locks WHERE id = $1"
- _, err := Conn.Exec(query, id)
- if err != nil {
- return fmt.Errorf("error removing lock: %v", err)
- }
-
- return nil
-}
diff --git a/app/server/db/locks.go b/app/server/db/locks.go
new file mode 100644
index 00000000..bff5fbc8
--- /dev/null
+++ b/app/server/db/locks.go
@@ -0,0 +1,252 @@
+package db
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "time"
+
+ "github.com/lib/pq"
+)
+
+const lockHeartbeatInterval = 700 * time.Millisecond
+const lockHeartbeatTimeout = 4 * time.Second
+
+// distributed locking to ensure only one user can write to a plan repo at a time
+// multiple readers are allowed, but read locks block writes
+// write lock is exclusive (blocks both reads and writes)
+
+type LockRepoParams struct {
+ OrgId string
+ UserId string
+ PlanId string
+ Branch string
+ Scope LockScope
+ PlanBuildId string
+ Ctx context.Context
+ CancelFn context.CancelFunc
+}
+
+func LockRepo(params LockRepoParams) (string, error) {
+ return lockRepo(params, 0)
+}
+
+func lockRepo(params LockRepoParams, numRetry int) (string, error) {
+ orgId := params.OrgId
+ userId := params.UserId
+ planId := params.PlanId
+ branch := params.Branch
+ scope := params.Scope
+ planBuildId := params.PlanBuildId
+ ctx := params.Ctx
+ cancelFn := params.CancelFn
+
+ tx, err := Conn.Begin()
+ if err != nil {
+ return "", fmt.Errorf("error starting transaction: %v", err)
+ }
+
+ // Ensure that rollback is attempted in case of failure
+ defer func() {
+ if err != nil {
+ if rbErr := tx.Rollback(); rbErr != nil {
+ log.Printf("transaction rollback error: %v\n", rbErr)
+ } else {
+ log.Println("transaction rolled back")
+ }
+ }
+ }()
+
+ query := "SELECT id, org_id, user_id, plan_id, plan_build_id, scope, branch, created_at FROM repo_locks WHERE plan_id = $1 FOR UPDATE"
+ queryArgs := []interface{}{planId}
+
+ var locks []*repoLock
+
+ fn := func() error {
+ rows, err := tx.Query(query, queryArgs...)
+ if err != nil {
+ return fmt.Errorf("error getting repo locks: %v", err)
+ }
+
+ defer rows.Close()
+
+ var expiredLockIds []string
+
+ now := time.Now()
+ for rows.Next() {
+ var lock repoLock
+ if err := rows.Scan(&lock.Id, &lock.OrgId, &lock.UserId, &lock.PlanId, &lock.PlanBuildId, &lock.Scope, &lock.Branch, &lock.CreatedAt); err != nil {
+ return fmt.Errorf("error scanning repo lock: %v", err)
+ }
+
+ // ensure heartbeat hasn't timed out
+ if now.Sub(lock.LastHeartbeatAt) < lockHeartbeatTimeout {
+ locks = append(locks, &lock)
+ } else {
+ expiredLockIds = append(expiredLockIds, lock.Id)
+ }
+ }
+
+ if len(expiredLockIds) > 0 {
+ query := "DELETE FROM repo_locks WHERE id = ANY($1)"
+ _, err := tx.Exec(query, pq.Array(expiredLockIds))
+ if err != nil {
+ return fmt.Errorf("error removing expired locks: %v", err)
+ }
+ }
+
+ return nil
+ }
+ if err := fn(); err != nil {
+ return "", err
+ }
+
+ canAcquire := true
+ canRetry := true
+
+ // log.Println("locks:")
+ // spew.Dump(locks)
+
+ for _, lock := range locks {
+ lockBranch := ""
+ if lock.Branch != nil {
+ lockBranch = *lock.Branch
+ }
+
+ if scope == LockScopeRead {
+ canAcquireThisLock := lock.Scope == LockScopeRead && lockBranch == branch
+ if !canAcquireThisLock {
+ canAcquire = false
+ }
+ } else if scope == LockScopeWrite {
+ canAcquire = false
+
+ // if lock is for the same plan plan and branch, allow parallel writes
+ if planId == lock.PlanId && branch == lockBranch {
+ canAcquire = true
+ }
+
+ if lock.Scope == LockScopeWrite && lockBranch == branch {
+ canRetry = false
+ }
+ } else {
+ err = fmt.Errorf("invalid lock scope: %v", scope)
+ return "", err
+ }
+ }
+
+ if !canAcquire {
+ if canRetry {
+ // 10 second timeout
+ if numRetry > 20 {
+ err = fmt.Errorf("plan is currently being updated by another user")
+ return "", err
+ }
+ time.Sleep(500 * time.Millisecond)
+ return lockRepo(params, numRetry+1)
+ }
+ err = fmt.Errorf("plan is currently being updated by another user")
+ return "", err
+ }
+
+ // Insert the new lock
+ var lockPlanBuildId *string
+ if planBuildId != "" {
+ lockPlanBuildId = &planBuildId
+ }
+
+ var lockBranch *string
+ if branch != "" {
+ lockBranch = &branch
+ }
+
+ newLock := &repoLock{
+ OrgId: orgId,
+ UserId: userId,
+ PlanId: planId,
+ PlanBuildId: lockPlanBuildId,
+ Scope: scope,
+ Branch: lockBranch,
+ }
+ // log.Println("newLock:")
+ // spew.Dump(newLock)
+
+ insertQuery := "INSERT INTO repo_locks (org_id, user_id, plan_id, plan_build_id, scope, branch) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id"
+ err = tx.QueryRow(
+ insertQuery,
+ newLock.OrgId,
+ newLock.UserId,
+ newLock.PlanId,
+ newLock.PlanBuildId,
+ newLock.Scope,
+ newLock.Branch,
+ ).Scan(&newLock.Id)
+ if err != nil {
+ return "", fmt.Errorf("error inserting new lock: %v", err)
+ }
+
+ branches, err := GitListBranches(orgId, planId)
+ if err != nil {
+ return "", fmt.Errorf("error getting branches: %v", err)
+ }
+
+ log.Println("branches:", branches)
+
+ if branch != "" {
+ // checkout the branch
+ err = gitCheckoutBranch(getPlanDir(orgId, planId), branch)
+ if err != nil {
+ return "", fmt.Errorf("error checking out branch: %v", err)
+ }
+ }
+
+ // Commit the transaction
+ if err = tx.Commit(); err != nil {
+ return "", fmt.Errorf("error committing transaction: %v", err)
+ }
+
+ // Start a goroutine to keep the lock alive
+ go func() {
+ numErrors := 0
+ for {
+ select {
+ case <-ctx.Done():
+ log.Printf("case <-stream.Ctx.Done(): %s\n", newLock.Id)
+ err := UnlockRepo(newLock.Id)
+ if err != nil {
+ log.Printf("Error unlocking repo: %v\n", err)
+ }
+ return
+
+ default:
+ _, err := Conn.Exec("UPDATE repo_locks SET last_heartbeat_at = NOW() WHERE id = $1", newLock.Id)
+
+ if err != nil {
+ log.Printf("Error updating repo lock last heartbeat: %v\n", err)
+ numErrors++
+
+ if numErrors > 5 {
+ log.Printf("Too many errors updating repo lock last heartbeat: %v\n", err)
+ cancelFn()
+ return
+ }
+ }
+
+ time.Sleep(lockHeartbeatInterval)
+ }
+
+ }
+ }()
+
+ return newLock.Id, nil
+}
+
+func UnlockRepo(id string) error {
+ query := "DELETE FROM repo_locks WHERE id = $1"
+ _, err := Conn.Exec(query, id)
+ if err != nil {
+ return fmt.Errorf("error removing lock: %v", err)
+ }
+
+ return nil
+}
diff --git a/app/server/db/models.go b/app/server/db/models.go
index 4ec520ee..eb4f4db8 100644
--- a/app/server/db/models.go
+++ b/app/server/db/models.go
@@ -251,13 +251,14 @@ func (role *OrgRole) ToApi() *shared.OrgRole {
}
type ModelStream struct {
- Id string `db:"id"`
- OrgId string `db:"org_id"`
- PlanId string `db:"plan_id"`
- InternalIp string `db:"internal_ip"`
- Branch string `db:"branch"`
- CreatedAt time.Time `db:"created_at"`
- FinishedAt *time.Time `db:"finished_at"`
+ Id string `db:"id"`
+ OrgId string `db:"org_id"`
+ PlanId string `db:"plan_id"`
+ InternalIp string `db:"internal_ip"`
+ Branch string `db:"branch"`
+ LastHeartbeatAt time.Time `db:"last_heartbeat_at"`
+ CreatedAt time.Time `db:"created_at"`
+ FinishedAt *time.Time `db:"finished_at"`
}
// type ModelStreamSubscription struct {
@@ -279,14 +280,15 @@ const (
)
type repoLock struct {
- Id string `db:"id"`
- OrgId string `db:"org_id"`
- UserId string `db:"user_id"`
- PlanId string `db:"plan_id"`
- Scope LockScope `db:"scope"`
- Branch *string `db:"branch"`
- PlanBuildId *string `db:"plan_build_id"`
- CreatedAt time.Time `db:"created_at"`
+ Id string `db:"id"`
+ OrgId string `db:"org_id"`
+ UserId string `db:"user_id"`
+ PlanId string `db:"plan_id"`
+ Scope LockScope `db:"scope"`
+ Branch *string `db:"branch"`
+ PlanBuildId *string `db:"plan_build_id"`
+ LastHeartbeatAt time.Time `db:"last_heartbeat_at"`
+ CreatedAt time.Time `db:"created_at"`
}
// Models below are stored in files, not in the database.
diff --git a/app/server/db/stream_helpers.go b/app/server/db/stream_helpers.go
index 0bb4d596..1fc07ac1 100644
--- a/app/server/db/stream_helpers.go
+++ b/app/server/db/stream_helpers.go
@@ -1,13 +1,17 @@
package db
import (
+ "context"
"database/sql"
"fmt"
"log"
"time"
)
-func StoreModelStream(stream *ModelStream) error {
+const modelStreamHeartbeatInterval = 1 * time.Second
+const modelStreamHeartbeatTimeout = 5 * time.Second
+
+func StoreModelStream(stream *ModelStream, ctx context.Context, cancelFn context.CancelFunc) error {
query := `INSERT INTO model_streams (org_id, plan_id, internal_ip, branch) VALUES (:org_id, :plan_id, :internal_ip, :branch) RETURNING id, created_at`
row, err := Conn.NamedQuery(query, stream)
@@ -29,6 +33,39 @@ func StoreModelStream(stream *ModelStream) error {
stream.CreatedAt = createdAt
}
+ // Start a goroutine to keep the lock alive
+ go func() {
+ numErrors := 0
+ for {
+ select {
+ case <-ctx.Done():
+ log.Printf("case <-stream.Ctx.Done(): %s\n", stream.Id)
+ err := SetModelStreamFinished(stream.Id)
+ if err != nil {
+ log.Printf("Error setting model stream %s finished: %v\n", stream.Id, err)
+ }
+ return
+
+ default:
+ _, err := Conn.Exec("UPDATE model_streams SET last_heartbeat_at = NOW() WHERE id = $1", stream.Id)
+
+ if err != nil {
+ log.Printf("Error updating model stream last heartbeat: %v\n", err)
+ numErrors++
+
+ if numErrors > 5 {
+ log.Printf("Too many errors updating model stream last heartbeat: %v\n", err)
+ cancelFn()
+ return
+ }
+ }
+
+ time.Sleep(modelStreamHeartbeatInterval)
+ }
+
+ }
+ }()
+
return nil
}
@@ -58,6 +95,18 @@ func GetActiveModelStream(planId, branch string) (*ModelStream, error) {
return nil, fmt.Errorf("error getting active model stream: %v", err)
}
+ if time.Now().Add(-modelStreamHeartbeatTimeout).After(stream.LastHeartbeatAt) {
+ log.Printf("Model stream %s has not sent a heartbeat in %s\n", stream.Id, modelStreamHeartbeatTimeout)
+
+ err := SetModelStreamFinished(stream.Id)
+
+ if err != nil {
+ return nil, fmt.Errorf("error setting model stream finished: %v", err)
+ }
+
+ return nil, nil
+ }
+
return &stream, nil
}
diff --git a/app/server/handlers/branches.go b/app/server/handlers/branches.go
index cd23ebc8..bbc900eb 100644
--- a/app/server/handlers/branches.go
+++ b/app/server/handlers/branches.go
@@ -157,7 +157,16 @@ func DeleteBranchHandler(w http.ResponseWriter, r *http.Request) {
return
}
- repoLockId, err := db.LockRepo(auth.OrgId, auth.User.Id, planId, "main", db.LockScopeRead)
+ repoLockId, err := db.LockRepo(
+ db.LockRepoParams{
+ OrgId: auth.OrgId,
+ UserId: auth.User.Id,
+ PlanId: planId,
+ Branch: "main",
+ Scope: db.LockScopeRead,
+ },
+ )
+
if err != nil {
log.Printf("Error locking repo: %v\n", err)
http.Error(w, "Error locking repo: "+err.Error(), http.StatusInternalServerError)
diff --git a/app/server/handlers/fs_helper.go b/app/server/handlers/fs_helper.go
index 61f21777..448ea65d 100644
--- a/app/server/handlers/fs_helper.go
+++ b/app/server/handlers/fs_helper.go
@@ -14,7 +14,16 @@ func lockRepo(w http.ResponseWriter, r *http.Request, auth *types.ServerAuth, sc
planId := vars["planId"]
branch := vars["branch"]
- repoLockId, err := db.LockRepo(auth.OrgId, auth.User.Id, planId, branch, db.LockScopeRead)
+ repoLockId, err := db.LockRepo(
+ db.LockRepoParams{
+ OrgId: auth.OrgId,
+ UserId: auth.User.Id,
+ PlanId: planId,
+ Branch: branch,
+ Scope: scope,
+ },
+ )
+
if err != nil {
log.Printf("Error locking repo: %v\n", err)
http.Error(w, "Error locking repo: "+err.Error(), http.StatusInternalServerError)
diff --git a/app/server/handlers/proxy_helper.go b/app/server/handlers/proxy_helper.go
index a867efa2..51b45c13 100644
--- a/app/server/handlers/proxy_helper.go
+++ b/app/server/handlers/proxy_helper.go
@@ -26,6 +26,7 @@ func proxyActivePlanMethod(w http.ResponseWriter, r *http.Request, planId, branc
}
if modelStream.InternalIp == host.Ip {
+ // No active plan for this plan or else we wouldn't be calling proxyActivePlanMethod -- set the model stream to finished because something went wrong
db.SetModelStreamFinished(modelStream.Id)
log.Printf("No active plan for plan %s\n", planId)
http.Error(w, "No active plan for plan", http.StatusNotFound)
diff --git a/app/server/migrations/2024020800_heartbeats.down.sql b/app/server/migrations/2024020800_heartbeats.down.sql
new file mode 100644
index 00000000..149cbb35
--- /dev/null
+++ b/app/server/migrations/2024020800_heartbeats.down.sql
@@ -0,0 +1,2 @@
+ALTER TABLE repo_locks DROP COLUMN last_heartbeat_at;
+ALTER TABLE model_streams DROP COLUMN last_heartbeat_at;
\ No newline at end of file
diff --git a/app/server/migrations/2024020800_heartbeats.up.sql b/app/server/migrations/2024020800_heartbeats.up.sql
new file mode 100644
index 00000000..82ad644f
--- /dev/null
+++ b/app/server/migrations/2024020800_heartbeats.up.sql
@@ -0,0 +1,2 @@
+ALTER TABLE repo_locks ADD COLUMN last_heartbeat_at TIMESTAMP NOT NULL DEFAULT NOW();
+ALTER TABLE model_streams ADD COLUMN last_heartbeat_at TIMESTAMP NOT NULL DEFAULT NOW();
\ No newline at end of file
diff --git a/app/server/model/plan/build.go b/app/server/model/plan/build.go
index c57925d1..b5d3b71a 100644
--- a/app/server/model/plan/build.go
+++ b/app/server/model/plan/build.go
@@ -142,13 +142,15 @@ func execPlanBuild(client *openai.Client, currentOrgId, currentUserId, branch st
finished := false
log.Println("onFinishBuildFile: " + filePath)
- repoLockId, err := db.LockRepoForBuild(
- currentOrgId,
- currentUserId,
- planId,
- planRes.PlanBuildId,
- branch,
- db.LockScopeWrite,
+ repoLockId, err := db.LockRepo(
+ db.LockRepoParams{
+ OrgId: currentOrgId,
+ UserId: currentUserId,
+ PlanId: planId,
+ Branch: branch,
+ PlanBuildId: build.Id,
+ Scope: db.LockScopeWrite,
+ },
)
if err != nil {
log.Printf("Error locking repo for build file: %v\n", err)
diff --git a/app/server/model/plan/state.go b/app/server/model/plan/state.go
index de1c4e99..2edc07f5 100644
--- a/app/server/model/plan/state.go
+++ b/app/server/model/plan/state.go
@@ -29,12 +29,7 @@ func CreateActivePlan(planId, branch, prompt string) *types.ActivePlan {
case <-activePlan.Ctx.Done():
log.Printf("case <-activePlan.Ctx.Done(): %s\n", planId)
- err := db.SetModelStreamFinished(activePlan.ModelStreamId)
- if err != nil {
- log.Printf("Error setting model stream %s finished: %v\n", activePlan.ModelStreamId, err)
- }
-
- err = db.SetPlanStatus(planId, branch, shared.PlanStatusStopped, "")
+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusStopped, "")
if err != nil {
log.Printf("Error setting plan %s status to stopped: %v\n", planId, err)
}
@@ -63,11 +58,6 @@ func CreateActivePlan(planId, branch, prompt string) *types.ActivePlan {
}
}
- err := db.SetModelStreamFinished(activePlan.ModelStreamId)
- if err != nil {
- log.Printf("Error setting model stream %s finished: %v\n", activePlan.ModelStreamId, err)
- }
-
activePlan.CancelFn()
DeleteActivePlan(planId, branch)
return
diff --git a/app/server/model/plan/stop.go b/app/server/model/plan/stop.go
index 14a5dd72..165e7c93 100644
--- a/app/server/model/plan/stop.go
+++ b/app/server/model/plan/stop.go
@@ -14,8 +14,6 @@ func Stop(planId, branch, currentUserId, currentOrgId string) error {
return fmt.Errorf("no active plan with id %s", planId)
}
- content := active.CurrentReplyContent
-
active.CancelFn()
userMsg := db.ConvoMessage{
@@ -26,7 +24,7 @@ func Stop(planId, branch, currentUserId, currentOrgId string) error {
Tokens: active.NumTokens,
Num: active.PromptMessageNum + 1,
Stopped: true,
- Message: content,
+ Message: active.CurrentReplyContent,
}
_, err := db.StoreConvoMessage(&userMsg, currentUserId, branch, true)
diff --git a/app/server/model/plan/tell.go b/app/server/model/plan/tell.go
index 42de5a68..9fc07fef 100644
--- a/app/server/model/plan/tell.go
+++ b/app/server/model/plan/tell.go
@@ -58,7 +58,7 @@ func Tell(client *openai.Client, plan *db.Plan, branch string, auth *types.Serve
InternalIp: host.Ip,
Branch: branch,
}
- err = db.StoreModelStream(modelStream)
+ err = db.StoreModelStream(modelStream, active.ModelStreamCtx, active.CancelModelStreamFn)
if err != nil {
log.Printf("Tell: Error storing model stream for plan ID %s on branch %s: %v\n", plan.Id, branch, err) // Log error storing model stream
log.Printf("Error storing model stream: %v\n", err)
@@ -119,7 +119,15 @@ func execTellPlan(client *openai.Client, plan *db.Plan, branch string, auth *typ
if iteration > 0 || missingFileResponse != "" {
lockScope = db.LockScopeRead
}
- repoLockId, err := db.LockRepo(auth.OrgId, auth.User.Id, planId, branch, lockScope)
+ repoLockId, err := db.LockRepo(
+ db.LockRepoParams{
+ OrgId: auth.OrgId,
+ UserId: auth.User.Id,
+ PlanId: planId,
+ Branch: branch,
+ Scope: lockScope,
+ },
+ )
if err != nil {
log.Printf("execTellPlan: Error locking repo for plan ID %s on branch %s: %v\n", plan.Id, branch, err) // Log error locking repo
@@ -728,6 +736,11 @@ func execTellPlan(client *openai.Client, plan *db.Plan, branch string, auth *typ
}
if err != nil {
+ if err.Error() == "context canceled" {
+ log.Println("Tell: stream context canceled")
+ return
+ }
+
onError(fmt.Errorf("stream error: %v", err), true, "", "")
return
}
@@ -756,7 +769,6 @@ func execTellPlan(client *openai.Client, plan *db.Plan, branch string, auth *typ
onError(fmt.Errorf("failed to set plan status to describing: %v", err), true, "", "")
return
}
-
// log.Println("summarize convo:", spew.Sdump(convo))
if len(convo) > 0 {
@@ -771,7 +783,27 @@ func execTellPlan(client *openai.Client, plan *db.Plan, branch string, auth *typ
})
}
- repoLockId, err := db.LockRepo(auth.OrgId, auth.User.Id, planId, branch, db.LockScopeWrite)
+ repoLockId, err := db.LockRepo(
+ db.LockRepoParams{
+ OrgId: currentOrgId,
+ UserId: currentUserId,
+ PlanId: planId,
+ Branch: branch,
+ Scope: db.LockScopeWrite,
+ Ctx: active.Ctx,
+ CancelFn: active.CancelFn,
+ },
+ )
+
+ if err != nil {
+ log.Printf("Error locking repo: %v\n", err)
+ active.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: "Error locking repo",
+ }
+ return
+ }
var shouldContinue bool
err = func() error {
@@ -833,7 +865,6 @@ func execTellPlan(client *openai.Client, plan *db.Plan, branch string, auth *typ
}()
go func() {
-
shouldContinue, err = ExecStatusShouldContinue(client, assistantMsg.Message, active.Ctx)
if err != nil {
onError(fmt.Errorf("failed to get exec status: %v", err), false, assistantMsg.Id, convoCommitMsg)