summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/api/server.go1
-rw-r--r--internal/api/stories.go65
-rw-r--r--internal/api/stories_test.go45
-rw-r--r--internal/cli/serve.go3
-rw-r--r--internal/executor/container.go43
-rw-r--r--internal/executor/executor.go29
-rw-r--r--internal/executor/executor_test.go95
7 files changed, 269 insertions, 12 deletions
diff --git a/internal/api/server.go b/internal/api/server.go
index bb23f46..fc9bd63 100644
--- a/internal/api/server.go
+++ b/internal/api/server.go
@@ -147,6 +147,7 @@ func (s *Server) routes() {
s.mux.HandleFunc("GET /api/stories/{id}/tasks", s.handleListStoryTasks)
s.mux.HandleFunc("POST /api/stories/{id}/tasks", s.handleAddTaskToStory)
s.mux.HandleFunc("PUT /api/stories/{id}/status", s.handleUpdateStoryStatus)
+ s.mux.HandleFunc("GET /api/stories/{id}/deployment-status", s.handleStoryDeploymentStatus)
s.mux.HandleFunc("GET /api/health", s.handleHealth)
s.mux.HandleFunc("GET /api/version", s.handleVersion)
s.mux.HandleFunc("POST /api/webhooks/github", s.handleGitHubWebhook)
diff --git a/internal/api/stories.go b/internal/api/stories.go
index 459d0db..640bb0e 100644
--- a/internal/api/stories.go
+++ b/internal/api/stories.go
@@ -1,6 +1,7 @@
package api
import (
+ "database/sql"
"encoding/json"
"fmt"
"net/http"
@@ -9,22 +10,35 @@ import (
"time"
"github.com/google/uuid"
+ "github.com/thepeterstone/claudomator/internal/deployment"
"github.com/thepeterstone/claudomator/internal/task"
)
-// createStoryBranch creates a new git branch in localPath and pushes it to origin.
+// createStoryBranch creates a new git branch in localPath from origin/master (or main)
+// and pushes it to origin. Idempotent: treats "already exists" as success.
func createStoryBranch(localPath, branchName string) error {
- out, err := exec.Command("git", "-C", localPath, "checkout", "-b", branchName).CombinedOutput()
+ // Fetch latest from origin so origin/master is up to date.
+ if out, err := exec.Command("git", "-C", localPath, "fetch", "origin").CombinedOutput(); err != nil {
+ return fmt.Errorf("git fetch: %w (output: %s)", err, string(out))
+ }
+ // Try to create branch from origin/master; fall back to origin/main.
+ base := "origin/master"
+ if out, err := exec.Command("git", "-C", localPath, "rev-parse", "--verify", "origin/master").CombinedOutput(); err != nil {
+ if strings.Contains(string(out), "fatal") || err != nil {
+ base = "origin/main"
+ }
+ }
+ out, err := exec.Command("git", "-C", localPath, "checkout", "-b", branchName, base).CombinedOutput()
if err != nil {
if !strings.Contains(string(out), "already exists") {
return fmt.Errorf("git checkout -b: %w (output: %s)", err, string(out))
}
- // Branch exists; switch to it.
+ // Branch exists; switch to it — idempotent.
if out2, err2 := exec.Command("git", "-C", localPath, "checkout", branchName).CombinedOutput(); err2 != nil {
return fmt.Errorf("git checkout: %w (output: %s)", err2, string(out2))
}
}
- if out, err := exec.Command("git", "-C", localPath, "push", "-u", "origin", branchName).CombinedOutput(); err != nil {
+ if out, err := exec.Command("git", "-C", localPath, "push", "origin", branchName).CombinedOutput(); err != nil {
return fmt.Errorf("git push: %w (output: %s)", err, string(out))
}
return nil
@@ -306,3 +320,46 @@ func (s *Server) handleApproveStory(w http.ResponseWriter, r *http.Request) {
"task_ids": taskIDs,
})
}
+
+// handleStoryDeploymentStatus aggregates the deployment status across all tasks in a story.
+// GET /api/stories/{id}/deployment-status
+func (s *Server) handleStoryDeploymentStatus(w http.ResponseWriter, r *http.Request) {
+ id := r.PathValue("id")
+
+ story, err := s.store.GetStory(id)
+ if err != nil {
+ writeJSON(w, http.StatusNotFound, map[string]string{"error": "story not found"})
+ return
+ }
+
+ tasks, err := s.store.ListTasksByStory(id)
+ if err != nil {
+ writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
+ return
+ }
+
+ // Collect all commits from the latest execution of each task.
+ var allCommits []task.GitCommit
+ for _, t := range tasks {
+ exec, err := s.store.GetLatestExecution(t.ID)
+ if err != nil {
+ if err == sql.ErrNoRows {
+ continue
+ }
+ writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
+ return
+ }
+ allCommits = append(allCommits, exec.Commits...)
+ }
+
+ // Determine project remote URL for the deployment check.
+ projectRemoteURL := ""
+ if story.ProjectID != "" {
+ if proj, err := s.store.GetProject(story.ProjectID); err == nil {
+ projectRemoteURL = proj.RemoteURL
+ }
+ }
+
+ status := deployment.Check(allCommits, projectRemoteURL)
+ writeJSON(w, http.StatusOK, status)
+}
diff --git a/internal/api/stories_test.go b/internal/api/stories_test.go
index cf522e1..17bea07 100644
--- a/internal/api/stories_test.go
+++ b/internal/api/stories_test.go
@@ -7,7 +7,9 @@ import (
"net/http/httptest"
"strings"
"testing"
+ "time"
+ "github.com/thepeterstone/claudomator/internal/deployment"
"github.com/thepeterstone/claudomator/internal/task"
)
@@ -202,3 +204,46 @@ func TestHandleStoryApprove_WiresDepends(t *testing.T) {
t.Errorf("task3.DependsOn: want [%s], got %v", task2.ID, task3.DependsOn)
}
}
+
+func TestHandleStoryDeploymentStatus(t *testing.T) {
+ srv, store := testServer(t)
+
+ // Create a story.
+ now := time.Now().UTC()
+ story := &task.Story{
+ ID: "deploy-story-1",
+ Name: "Deploy Status Story",
+ Status: task.StoryInProgress,
+ CreatedAt: now,
+ UpdatedAt: now,
+ }
+ if err := store.CreateStory(story); err != nil {
+ t.Fatalf("CreateStory: %v", err)
+ }
+
+ // Request deployment status — no tasks yet.
+ req := httptest.NewRequest("GET", "/api/stories/deploy-story-1/deployment-status", nil)
+ w := httptest.NewRecorder()
+ srv.mux.ServeHTTP(w, req)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+
+ var status deployment.Status
+ if err := json.NewDecoder(w.Body).Decode(&status); err != nil {
+ t.Fatalf("decode: %v", err)
+ }
+ // No tasks → no commits → IncludesFix = false (nothing to check).
+ if status.IncludesFix {
+ t.Error("expected IncludesFix=false when no commits")
+ }
+
+ // 404 for unknown story.
+ req2 := httptest.NewRequest("GET", "/api/stories/nonexistent/deployment-status", nil)
+ w2 := httptest.NewRecorder()
+ srv.mux.ServeHTTP(w2, req2)
+ if w2.Code != http.StatusNotFound {
+ t.Errorf("expected 404 for unknown story, got %d", w2.Code)
+ }
+}
diff --git a/internal/cli/serve.go b/internal/cli/serve.go
index 3850ca9..644392e 100644
--- a/internal/cli/serve.go
+++ b/internal/cli/serve.go
@@ -91,6 +91,7 @@ func serve(addr string) error {
SSHAuthSock: cfg.SSHAuthSock,
ClaudeConfigDir: claudeConfigDir,
CredentialSyncCmd: filepath.Join(repoDir, "scripts", "sync-credentials"),
+ Store: store,
},
"gemini": &executor.ContainerRunner{
Image: cfg.GeminiImage,
@@ -101,6 +102,7 @@ func serve(addr string) error {
SSHAuthSock: cfg.SSHAuthSock,
ClaudeConfigDir: claudeConfigDir,
CredentialSyncCmd: filepath.Join(repoDir, "scripts", "sync-credentials"),
+ Store: store,
},
"container": &executor.ContainerRunner{
Image: "claudomator-agent:latest",
@@ -111,6 +113,7 @@ func serve(addr string) error {
SSHAuthSock: cfg.SSHAuthSock,
ClaudeConfigDir: claudeConfigDir,
CredentialSyncCmd: filepath.Join(repoDir, "scripts", "sync-credentials"),
+ Store: store,
},
}
diff --git a/internal/executor/container.go b/internal/executor/container.go
index d9ed8ef..5e1a026 100644
--- a/internal/executor/container.go
+++ b/internal/executor/container.go
@@ -28,6 +28,7 @@ type ContainerRunner struct {
GeminiBinary string // optional path to gemini binary in container
ClaudeConfigDir string // host path to ~/.claude; mounted into container for auth credentials
CredentialSyncCmd string // optional path to sync-credentials script for auth-error auto-recovery
+ Store Store // optional; used to look up stories and projects for story-aware cloning
// Command allows mocking exec.CommandContext for tests.
Command func(ctx context.Context, name string, arg ...string) *exec.Cmd
}
@@ -95,6 +96,20 @@ func (r *ContainerRunner) Run(ctx context.Context, t *task.Task, e *storage.Exec
}
}()
+ // Resolve story branch and project local path if this is a story task.
+ var storyBranch string
+ var storyLocalPath string
+ if t.StoryID != "" && r.Store != nil {
+ if story, err := r.Store.GetStory(t.StoryID); err == nil && story != nil {
+ storyBranch = story.BranchName
+ if story.ProjectID != "" {
+ if proj, err := r.Store.GetProject(story.ProjectID); err == nil && proj != nil {
+ storyLocalPath = proj.LocalPath
+ }
+ }
+ }
+ }
+
// 2. Clone repo into workspace if not resuming.
// git clone requires the target directory to not exist; remove the MkdirTemp-created dir first.
if !isResume {
@@ -102,9 +117,21 @@ func (r *ContainerRunner) Run(ctx context.Context, t *task.Task, e *storage.Exec
return fmt.Errorf("removing workspace before clone: %w", err)
}
r.Logger.Info("cloning repository", "url", repoURL, "workspace", workspace)
- if out, err := r.command(ctx, "git", "clone", repoURL, workspace).CombinedOutput(); err != nil {
+ var cloneArgs []string
+ if storyLocalPath != "" {
+ cloneArgs = []string{"clone", "--reference", storyLocalPath, repoURL, workspace}
+ } else {
+ cloneArgs = []string{"clone", repoURL, workspace}
+ }
+ if out, err := r.command(ctx, "git", cloneArgs...).CombinedOutput(); err != nil {
return fmt.Errorf("git clone failed: %w\n%s", err, string(out))
}
+ if storyBranch != "" {
+ r.Logger.Info("checking out story branch", "branch", storyBranch)
+ if out, err := r.command(ctx, "git", "-C", workspace, "checkout", storyBranch).CombinedOutput(); err != nil {
+ return fmt.Errorf("git checkout story branch %q failed: %w\n%s", storyBranch, err, string(out))
+ }
+ }
if err = os.Chmod(workspace, 0755); err != nil {
return fmt.Errorf("chmod cloned workspace: %w", err)
}
@@ -145,7 +172,7 @@ func (r *ContainerRunner) Run(ctx context.Context, t *task.Task, e *storage.Exec
}
// Run container (with auth retry on failure).
- runErr := r.runContainer(ctx, t, e, workspace, agentHome, isResume)
+ runErr := r.runContainer(ctx, t, e, workspace, agentHome, isResume, storyBranch)
if runErr != nil && isAuthError(runErr) && r.CredentialSyncCmd != "" {
r.Logger.Warn("auth failure detected, syncing credentials and retrying once", "taskID", t.ID)
syncOut, syncErr := r.command(ctx, r.CredentialSyncCmd).CombinedOutput()
@@ -159,7 +186,7 @@ func (r *ContainerRunner) Run(ctx context.Context, t *task.Task, e *storage.Exec
if srcData, readErr := os.ReadFile(filepath.Join(r.ClaudeConfigDir, ".claude.json")); readErr == nil {
_ = os.WriteFile(filepath.Join(agentHome, ".claude.json"), srcData, 0644)
}
- runErr = r.runContainer(ctx, t, e, workspace, agentHome, isResume)
+ runErr = r.runContainer(ctx, t, e, workspace, agentHome, isResume, storyBranch)
}
if runErr == nil {
@@ -175,7 +202,7 @@ func (r *ContainerRunner) Run(ctx context.Context, t *task.Task, e *storage.Exec
// runContainer runs the docker container for the given task and handles log setup,
// environment files, instructions, and post-execution git operations.
-func (r *ContainerRunner) runContainer(ctx context.Context, t *task.Task, e *storage.Execution, workspace, agentHome string, isResume bool) error {
+func (r *ContainerRunner) runContainer(ctx context.Context, t *task.Task, e *storage.Execution, workspace, agentHome string, isResume bool, storyBranch string) error {
repoURL := t.RepositoryURL
image := t.Agent.ContainerImage
@@ -322,8 +349,12 @@ func (r *ContainerRunner) runContainer(ctx context.Context, t *task.Task, e *sto
}
if hasCommits {
- r.Logger.Info("pushing changes back to remote", "url", repoURL)
- if out, err := r.command(ctx, "git", "-C", workspace, "push", "origin", "HEAD").CombinedOutput(); err != nil {
+ pushRef := "HEAD"
+ if storyBranch != "" {
+ pushRef = storyBranch
+ }
+ r.Logger.Info("pushing changes back to remote", "url", repoURL, "ref", pushRef)
+ if out, err := r.command(ctx, "git", "-C", workspace, "push", "origin", pushRef).CombinedOutput(); err != nil {
r.Logger.Warn("git push failed", "error", err, "output", string(out))
return fmt.Errorf("git push failed: %w\n%s", err, string(out))
}
diff --git a/internal/executor/executor.go b/internal/executor/executor.go
index 440294c..22273d9 100644
--- a/internal/executor/executor.go
+++ b/internal/executor/executor.go
@@ -33,6 +33,9 @@ type Store interface {
UpdateExecutionChangestats(execID string, stats *task.Changestats) error
RecordAgentEvent(e storage.AgentEvent) error
GetProject(id string) (*task.Project, error)
+ GetStory(id string) (*task.Story, error)
+ ListTasksByStory(storyID string) ([]*task.Task, error)
+ UpdateStoryStatus(id string, status task.StoryState) error
}
// LogPather is an optional interface runners can implement to provide the log
@@ -399,6 +402,9 @@ func (p *Pool) handleRunResult(ctx context.Context, t *task.Task, exec *storage.
}
p.maybeUnblockParent(t.ParentTaskID)
}
+ if t.StoryID != "" {
+ go p.checkStoryCompletion(ctx, t.StoryID)
+ }
}
summary := exec.Summary
@@ -430,6 +436,29 @@ func (p *Pool) handleRunResult(ctx context.Context, t *task.Task, exec *storage.
p.resultCh <- &Result{TaskID: t.ID, Execution: exec, Err: err}
}
+// checkStoryCompletion checks whether all tasks in a story have reached a terminal
+// success state and transitions the story to SHIPPABLE if so.
+func (p *Pool) checkStoryCompletion(ctx context.Context, storyID string) {
+ tasks, err := p.store.ListTasksByStory(storyID)
+ if err != nil {
+ p.logger.Error("checkStoryCompletion: failed to list tasks", "storyID", storyID, "error", err)
+ return
+ }
+ if len(tasks) == 0 {
+ return
+ }
+ for _, t := range tasks {
+ if t.State != task.StateCompleted && t.State != task.StateReady {
+ return // not all tasks done
+ }
+ }
+ if err := p.store.UpdateStoryStatus(storyID, task.StoryShippable); err != nil {
+ p.logger.Error("checkStoryCompletion: failed to update story status", "storyID", storyID, "error", err)
+ return
+ }
+ p.logger.Info("story transitioned to SHIPPABLE", "storyID", storyID)
+}
+
// UndrainingAgent resets the drain state and failure counter for the given agent type.
func (p *Pool) UndrainingAgent(agentType string) {
p.mu.Lock()
diff --git a/internal/executor/executor_test.go b/internal/executor/executor_test.go
index 1f4e92f..1e92093 100644
--- a/internal/executor/executor_test.go
+++ b/internal/executor/executor_test.go
@@ -1056,8 +1056,11 @@ func (m *minimalMockStore) UpdateExecutionChangestats(execID string, stats *task
m.mu.Unlock()
return nil
}
-func (m *minimalMockStore) RecordAgentEvent(_ storage.AgentEvent) error { return nil }
-func (m *minimalMockStore) GetProject(_ string) (*task.Project, error) { return nil, nil }
+func (m *minimalMockStore) RecordAgentEvent(_ storage.AgentEvent) error { return nil }
+func (m *minimalMockStore) GetProject(_ string) (*task.Project, error) { return nil, nil }
+func (m *minimalMockStore) GetStory(_ string) (*task.Story, error) { return nil, nil }
+func (m *minimalMockStore) ListTasksByStory(_ string) ([]*task.Task, error) { return nil, nil }
+func (m *minimalMockStore) UpdateStoryStatus(_ string, _ task.StoryState) error { return nil }
func (m *minimalMockStore) lastStateUpdate() (string, task.State, bool) {
m.mu.Lock()
@@ -1624,6 +1627,94 @@ func TestPool_ConsecutiveFailures_ResetOnSuccess(t *testing.T) {
}
}
+func TestPool_CheckStoryCompletion_AllComplete(t *testing.T) {
+ store := testStore(t)
+ logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
+ pool := NewPool(2, map[string]Runner{"claude": &mockRunner{}}, store, logger)
+
+ // Create a story in IN_PROGRESS state.
+ now := time.Now().UTC()
+ story := &task.Story{
+ ID: "story-comp-1",
+ Name: "Completion Test",
+ Status: task.StoryInProgress,
+ CreatedAt: now,
+ UpdatedAt: now,
+ }
+ if err := store.CreateStory(story); err != nil {
+ t.Fatalf("CreateStory: %v", err)
+ }
+
+ // Create two story tasks and drive them through valid transitions to COMPLETED.
+ for i, id := range []string{"sctask-1", "sctask-2"} {
+ tk := makeTask(id)
+ tk.StoryID = "story-comp-1"
+ tk.ParentTaskID = "fake-parent" // so it goes to COMPLETED
+ tk.State = task.StatePending
+ if err := store.CreateTask(tk); err != nil {
+ t.Fatalf("CreateTask %d: %v", i, err)
+ }
+ for _, s := range []task.State{task.StateQueued, task.StateRunning, task.StateCompleted} {
+ if err := store.UpdateTaskState(id, s); err != nil {
+ t.Fatalf("UpdateTaskState %s → %s: %v", id, s, err)
+ }
+ }
+ }
+
+ pool.checkStoryCompletion(context.Background(), "story-comp-1")
+
+ got, err := store.GetStory("story-comp-1")
+ if err != nil {
+ t.Fatalf("GetStory: %v", err)
+ }
+ if got.Status != task.StoryShippable {
+ t.Errorf("story status: want SHIPPABLE, got %v", got.Status)
+ }
+}
+
+func TestPool_CheckStoryCompletion_PartialComplete(t *testing.T) {
+ store := testStore(t)
+ logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
+ pool := NewPool(2, map[string]Runner{"claude": &mockRunner{}}, store, logger)
+
+ now := time.Now().UTC()
+ story := &task.Story{
+ ID: "story-partial-1",
+ Name: "Partial Test",
+ Status: task.StoryInProgress,
+ CreatedAt: now,
+ UpdatedAt: now,
+ }
+ if err := store.CreateStory(story); err != nil {
+ t.Fatalf("CreateStory: %v", err)
+ }
+
+ // First task driven to COMPLETED.
+ tk1 := makeTask("sptask-1")
+ tk1.StoryID = "story-partial-1"
+ tk1.ParentTaskID = "fake-parent"
+ store.CreateTask(tk1)
+ for _, s := range []task.State{task.StateQueued, task.StateRunning, task.StateCompleted} {
+ store.UpdateTaskState("sptask-1", s)
+ }
+
+ // Second task still in PENDING (not done).
+ tk2 := makeTask("sptask-2")
+ tk2.StoryID = "story-partial-1"
+ tk2.ParentTaskID = "fake-parent"
+ store.CreateTask(tk2)
+
+ pool.checkStoryCompletion(context.Background(), "story-partial-1")
+
+ got, err := store.GetStory("story-partial-1")
+ if err != nil {
+ t.Fatalf("GetStory: %v", err)
+ }
+ if got.Status != task.StoryInProgress {
+ t.Errorf("story status: want IN_PROGRESS (no transition), got %v", got.Status)
+ }
+}
+
func TestPool_Undrain_ResumesExecution(t *testing.T) {
store := testStore(t)