Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
merge: resolve conflict with main (runTask refactor + mergeUsage)
yushen2f1000d81568
diff --cc server/internal/daemon/daemon.goindex f5519a92d9,c01e707b23..5a8dcc1218--- a/server/internal/daemon/daemon.go+++ b/server/internal/daemon/daemon.go@@@ -996,7 -993,77 +1003,78 @@@ func (d *Daemon) runTask(ctx context.Coreturn TaskResult{}, err}- // Drain message channel — forward to server for live output + log locally.+ // Fallback: if session resume failed before establishing a session, retry+ // with a fresh session. We check SessionID == "" to distinguish a resume+ // failure (no session established) from a failure during actual execution.+ if result.Status == "failed" && task.PriorSessionID != "" && result.SessionID == "" {+ firstUsage := result.Usage+ taskLog.Warn("session resume failed, retrying with fresh session", "error", result.Error)+ execOpts.ResumeSessionID = ""+ retryResult, retryTools, retryErr := d.executeAndDrain(ctx, backend, prompt, execOpts, taskLog, task.ID)+ if retryErr != nil {+ taskLog.Error("fresh session also failed to start", "error", retryErr)+ } else {+ result = retryResult+ result.Usage = mergeUsage(firstUsage, result.Usage)+ tools = retryTools+ }+ }++ elapsed := time.Since(taskStart).Round(time.Second)+ taskLog.Info("agent finished",+ "status", result.Status,+ "duration", elapsed.String(),+ "tools", tools,+ )++ // Convert agent usage map to task usage entries.+ var usageEntries []TaskUsageEntry+ for model, u := range result.Usage {+ if u.InputTokens == 0 && u.OutputTokens == 0 && u.CacheReadTokens == 0 && u.CacheWriteTokens == 0 {+ continue+ }+ usageEntries = append(usageEntries, TaskUsageEntry{+ Provider: provider,+ Model: model,+ InputTokens: u.InputTokens,+ OutputTokens: u.OutputTokens,+ CacheReadTokens: u.CacheReadTokens,+ CacheWriteTokens: u.CacheWriteTokens,+ })+ }++ switch result.Status {+ case "completed":+ if result.Output == "" {+ return TaskResult{}, fmt.Errorf("%s returned empty output", provider)+ }+ return TaskResult{+ Status: "completed",+ Comment: result.Output,+ SessionID: result.SessionID,+ WorkDir: env.WorkDir,++ EnvRoot: env.RootDir,+ Usage: usageEntries,+ }, nil+ case "timeout":+ return TaskResult{}, fmt.Errorf("%s timed out after %s", provider, d.cfg.AgentTimeout)+ default:+ errMsg := result.Error+ if errMsg == "" {+ errMsg = fmt.Sprintf("%s execution %s", provider, result.Status)+ }- return TaskResult{Status: "blocked", Comment: errMsg, Usage: usageEntries}, nil++ return TaskResult{Status: "blocked", Comment: errMsg, EnvRoot: env.RootDir, Usage: usageEntries}, nil+ }+ }++ // executeAndDrain runs a backend, drains its message stream (forwarding to the+ // server), and waits for the final result.+ func (d *Daemon) executeAndDrain(ctx context.Context, backend agent.Backend, prompt string, opts agent.ExecOptions, taskLog *slog.Logger, taskID string) (agent.Result, int32, error) {+ session, err := backend.Execute(ctx, prompt, opts)+ if err != nil {+ return agent.Result{}, 0, err+ }+var toolCount atomic.Int32go func() {var seq atomic.Int32diff --cc server/internal/daemon/gc_test.goindex 5b9da75cf3,0000000000..dd9c8ea059mode 100644,000000..100644--- a/server/internal/daemon/gc_test.go+++ b/server/internal/daemon/gc_test.go@@@ -1,300 -1,0 +1,300 @@@+package daemon++import (+ "context"+ "encoding/json"+ "fmt"+ "log/slog"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "testing"+ "time"++ "github.com/multica-ai/multica/server/internal/daemon/execenv"+)+- // newTestDaemon creates a minimal Daemon for GC testing with a mock HTTP server.- func newTestDaemon(t *testing.T, handler http.Handler) *Daemon {++// newGCTestDaemon creates a minimal Daemon for GC testing with a mock HTTP server.++func newGCTestDaemon(t *testing.T, handler http.Handler) *Daemon {+ t.Helper()+ srv := httptest.NewServer(handler)+ t.Cleanup(srv.Close)++ root := t.TempDir()+ cfg := Config{+ WorkspacesRoot: root,+ GCEnabled: true,+ GCInterval: 1 * time.Hour,+ GCTTL: 5 * 24 * time.Hour,+ GCOrphanTTL: 30 * 24 * time.Hour,+ }+ d := New(cfg, slog.Default())+ d.client = NewClient(srv.URL)+ d.client.SetToken("test-token")+ return d+}++// createTaskDir creates a task directory with optional GC metadata.+func createTaskDir(t *testing.T, root, wsID, dirName string, meta *execenv.GCMeta) string {+ t.Helper()+ taskDir := filepath.Join(root, wsID, dirName)+ if err := os.MkdirAll(taskDir, 0o755); err != nil {+ t.Fatal(err)+ }+ if meta != nil {+ data, _ := json.Marshal(meta)+ if err := os.WriteFile(filepath.Join(taskDir, ".gc_meta.json"), data, 0o644); err != nil {+ t.Fatal(err)+ }+ }+ return taskDir+}++func TestShouldCleanTaskDir_DoneIssueOverTTL(t *testing.T) {+ t.Parallel()+ issueID := "11111111-1111-1111-1111-111111111111"++ mux := http.NewServeMux()+ mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ json.NewEncoder(w).Encode(map[string]any{+ "status": "done",+ "updated_at": time.Now().Add(-10 * 24 * time.Hour), // 10 days ago+ })+ })+- d := newTestDaemon(t, mux)++ d := newGCTestDaemon(t, mux)+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task1", &execenv.GCMeta{+ IssueID: issueID,+ WorkspaceID: "ws1",+ CompletedAt: time.Now().Add(-10 * 24 * time.Hour),+ })++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionClean {+ t.Fatalf("expected gcActionClean, got %d", action)+ }+}++func TestShouldCleanTaskDir_CanceledIssueOverTTL(t *testing.T) {+ t.Parallel()+ issueID := "22222222-2222-2222-2222-222222222222"++ mux := http.NewServeMux()+ mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ json.NewEncoder(w).Encode(map[string]any{+ "status": "canceled",+ "updated_at": time.Now().Add(-6 * 24 * time.Hour),+ })+ })+- d := newTestDaemon(t, mux)++ d := newGCTestDaemon(t, mux)+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task2", &execenv.GCMeta{+ IssueID: issueID,+ WorkspaceID: "ws1",+ CompletedAt: time.Now(),+ })++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionClean {+ t.Fatalf("expected gcActionClean, got %d", action)+ }+}++func TestShouldCleanTaskDir_OpenIssueSkipped(t *testing.T) {+ t.Parallel()+ issueID := "33333333-3333-3333-3333-333333333333"++ mux := http.NewServeMux()+ mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ json.NewEncoder(w).Encode(map[string]any{+ "status": "in_progress",+ "updated_at": time.Now().Add(-30 * 24 * time.Hour),+ })+ })+- d := newTestDaemon(t, mux)++ d := newGCTestDaemon(t, mux)+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task3", &execenv.GCMeta{+ IssueID: issueID,+ WorkspaceID: "ws1",+ CompletedAt: time.Now(),+ })++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionSkip {+ t.Fatalf("expected gcActionSkip for open issue, got %d", action)+ }+}++func TestShouldCleanTaskDir_DoneButRecentSkipped(t *testing.T) {+ t.Parallel()+ issueID := "44444444-4444-4444-4444-444444444444"++ mux := http.NewServeMux()+ mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ json.NewEncoder(w).Encode(map[string]any{+ "status": "done",+ "updated_at": time.Now().Add(-1 * 24 * time.Hour), // 1 day ago, within TTL+ })+ })+- d := newTestDaemon(t, mux)++ d := newGCTestDaemon(t, mux)+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task4", &execenv.GCMeta{+ IssueID: issueID,+ WorkspaceID: "ws1",+ CompletedAt: time.Now(),+ })++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionSkip {+ t.Fatalf("expected gcActionSkip for recently-done issue, got %d", action)+ }+}++func TestShouldCleanTaskDir_NoMetaRecentSkipped(t *testing.T) {+ t.Parallel()+- d := newTestDaemon(t, http.NewServeMux())++ d := newGCTestDaemon(t, http.NewServeMux())+ // No meta, fresh directory — should skip.+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task5", nil)++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionSkip {+ t.Fatalf("expected gcActionSkip for recent orphan, got %d", action)+ }+}++func TestShouldCleanTaskDir_NoMetaOldOrphan(t *testing.T) {+ t.Parallel()+- d := newTestDaemon(t, http.NewServeMux())++ d := newGCTestDaemon(t, http.NewServeMux())+ d.cfg.GCOrphanTTL = 0 // treat all orphans as expired+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task6", nil)++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionOrphan {+ t.Fatalf("expected gcActionOrphan, got %d", action)+ }+}++func TestShouldCleanTaskDir_APIErrorSkipped(t *testing.T) {+ t.Parallel()+ issueID := "55555555-5555-5555-5555-555555555555"++ mux := http.NewServeMux()+ mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusInternalServerError)+ })+- d := newTestDaemon(t, mux)++ d := newGCTestDaemon(t, mux)+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task7", &execenv.GCMeta{+ IssueID: issueID,+ WorkspaceID: "ws1",+ CompletedAt: time.Now(),+ })++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionSkip {+ t.Fatalf("expected gcActionSkip on API error, got %d", action)+ }+}++func TestShouldCleanTaskDir_Issue404OldOrphan(t *testing.T) {+ t.Parallel()+ issueID := "66666666-6666-6666-6666-666666666666"++ mux := http.NewServeMux()+ mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusNotFound)+ w.Write([]byte(`{"error":"issue not found"}`))+ })+- d := newTestDaemon(t, mux)++ d := newGCTestDaemon(t, mux)+ d.cfg.GCOrphanTTL = 0 // treat orphans as immediately eligible+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task8", &execenv.GCMeta{+ IssueID: issueID,+ WorkspaceID: "ws1",+ CompletedAt: time.Now(),+ })++ action := d.shouldCleanTaskDir(context.Background(), taskDir)+ if action != gcActionOrphan {+ t.Fatalf("expected gcActionOrphan for deleted issue, got %d", action)+ }+}++func TestCleanTaskDir_RemovesDirectory(t *testing.T) {+ t.Parallel()- d := newTestDaemon(t, http.NewServeMux())++ d := newGCTestDaemon(t, http.NewServeMux())+ taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "doomed", nil)++ if _, err := os.Stat(taskDir); err != nil {+ t.Fatal("task dir should exist before cleanup")+ }++ d.cleanTaskDir(taskDir)++ if _, err := os.Stat(taskDir); !os.IsNotExist(err) {+ t.Fatal("task dir should be removed after cleanup")+ }+}++func TestGcWorkspace_CleansEmptyWorkspaceDir(t *testing.T) {+ t.Parallel()+ issueID := "77777777-7777-7777-7777-777777777777"++ mux := http.NewServeMux()+ mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ json.NewEncoder(w).Encode(map[string]any{+ "status": "done",+ "updated_at": time.Now().Add(-10 * 24 * time.Hour),+ })+ })+- d := newTestDaemon(t, mux)++ d := newGCTestDaemon(t, mux)+ wsDir := filepath.Join(d.cfg.WorkspacesRoot, "ws-empty")+ createTaskDir(t, d.cfg.WorkspacesRoot, "ws-empty", "only-task", &execenv.GCMeta{+ IssueID: issueID,+ WorkspaceID: "ws-empty",+ CompletedAt: time.Now(),+ })++ d.gcWorkspace(context.Background(), wsDir)++ if _, err := os.Stat(wsDir); !os.IsNotExist(err) {+ t.Fatal("empty workspace dir should be removed after all tasks cleaned")+ }+}++func TestIsBareRepo(t *testing.T) {+ t.Parallel()++ t.Run("valid bare repo", func(t *testing.T) {+ dir := t.TempDir()+ os.WriteFile(filepath.Join(dir, "HEAD"), []byte("ref: refs/heads/main"), 0o644)+ os.MkdirAll(filepath.Join(dir, "objects"), 0o755)+ if !isBareRepo(dir) {+ t.Fatal("expected isBareRepo=true for dir with HEAD + objects/")+ }+ })++ t.Run("HEAD only", func(t *testing.T) {+ dir := t.TempDir()+ os.WriteFile(filepath.Join(dir, "HEAD"), []byte("ref: refs/heads/main"), 0o644)+ if isBareRepo(dir) {+ t.Fatal("expected isBareRepo=false for dir with only HEAD")+ }+ })++ t.Run("empty dir", func(t *testing.T) {+ dir := t.TempDir()+ if isBareRepo(dir) {+ t.Fatal("expected isBareRepo=false for empty dir")+ }+ })+}