Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix: keep daemon health responsive during repo lookup (#2211)
Qiang Zhangf1dc3dc986eb
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.goindex ee23e99fef..bee242f9eb 100644--- a/server/internal/daemon/daemon.go+++ b/server/internal/daemon/daemon.go@@ -50,11 +50,17 @@ type workspaceState struct {repoRefreshMu sync.Mutex}+type repoCache interface {+ Lookup(workspaceID, url string) string+ Sync(workspaceID string, repos []repocache.RepoInfo) error+ CreateWorktree(params repocache.WorktreeParams) (*repocache.WorktreeResult, error)+}+// Daemon is the local agent runtime that polls for and executes tasks.type Daemon struct {cfg Configclient *Client- repoCache *repocache.Cache+ repoCache repoCachelogger *slog.Loggermu sync.Mutex@@ -464,6 +470,11 @@ func (d *Daemon) registerTaskRepos(workspaceID string, repos []RepoData) {return}+ type repoCandidate struct {+ url string+ tracked bool+ }+d.mu.Lock()ws, ok := d.workspaces[workspaceID]if !ok {@@ -473,7 +484,7 @@ func (d *Daemon) registerTaskRepos(workspaceID string, repos []RepoData) {if ws.taskRepoURLs == nil {ws.taskRepoURLs = make(map[string]struct{}, len(repos))}- toSync := make([]RepoData, 0, len(repos))+ candidates := make([]repoCandidate, 0, len(repos))for _, repo := range repos {url := strings.TrimSpace(repo.URL)if url == "" {@@ -483,15 +494,22 @@ func (d *Daemon) registerTaskRepos(workspaceID string, repos []RepoData) {// AND the cache already has it._, inWorkspace := ws.allowedRepoURLs[url]_, inTask := ws.taskRepoURLs[url]- if (inWorkspace || inTask) && d.repoCache != nil && d.repoCache.Lookup(workspaceID, url) != "" {- ws.taskRepoURLs[url] = struct{}{}- continue- }ws.taskRepoURLs[url] = struct{}{}- toSync = append(toSync, RepoData{URL: url})+ candidates = append(candidates, repoCandidate{+ url: url,+ tracked: inWorkspace || inTask,+ })}d.mu.Unlock()+ toSync := make([]RepoData, 0, len(candidates))+ for _, candidate := range candidates {+ if candidate.tracked && d.repoCache != nil && d.repoCache.Lookup(workspaceID, candidate.url) != "" {+ continue+ }+ toSync = append(toSync, RepoData{URL: candidate.url})+ }+if d.repoCache != nil && len(toSync) > 0 {// Sync in the background — same shape used at workspace registration.// `ensureRepoReady` reports a meaningful error if the cache isn't readydiff --git a/server/internal/daemon/health_test.go b/server/internal/daemon/health_test.goindex 9ef8ba623f..06c491bbf9 100644--- a/server/internal/daemon/health_test.go+++ b/server/internal/daemon/health_test.go@@ -6,8 +6,11 @@ import ("log/slog""net/http""net/http/httptest"+ "sync""testing""time"++ "github.com/multica-ai/multica/server/internal/daemon/repocache")func TestHealthHandlerReportsCLIVersionAndActiveTaskCount(t *testing.T) {@@ -130,6 +133,104 @@ func TestShutdownHandlerRejectsNonPost(t *testing.T) {}}+func TestHealthHandlerRespondsWhileTaskRepoLookupWaits(t *testing.T) {+ const workspaceID = "ws-health"+ const repoURL = "https://github.com/org/repo.git"+ cache := newBlockingLookupRepoCache("/cache/org/repo.git")+ d := &Daemon{+ cfg: Config{CLIVersion: "v1.0.0"},+ workspaces: map[string]*workspaceState{+ workspaceID: {+ workspaceID: workspaceID,+ runtimeIDs: []string{"rt-1"},+ allowedRepoURLs: map[string]struct{}{repoURL: {}},+ taskRepoURLs: map[string]struct{}{},+ },+ },+ repoCache: cache,+ logger: slog.Default(),+ }+ defer cache.release()++ registerDone := make(chan struct{})+ go func() {+ d.registerTaskRepos(workspaceID, []RepoData{{URL: repoURL}})+ close(registerDone)+ }()+ cache.waitForLookup(t)++ rec := httptest.NewRecorder()+ healthDone := make(chan struct{})+ go func() {+ d.healthHandler(time.Now()).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/health", nil))+ close(healthDone)+ }()++ select {+ case <-healthDone:+ if rec.Code != http.StatusOK {+ t.Fatalf("expected 200, got %d", rec.Code)+ }+ case <-time.After(200 * time.Millisecond):+ t.Fatal("/health blocked behind task repo cache lookup")+ }++ cache.release()+ select {+ case <-registerDone:+ case <-time.After(time.Second):+ t.Fatal("registerTaskRepos did not unblock after repo lookup finished")+ }+}++type blockingLookupRepoCache struct {+ path string+ lookupSeen chan struct{}+ releaseLookup chan struct{}+ releaseOnce sync.Once+}++func newBlockingLookupRepoCache(path string) *blockingLookupRepoCache {+ return &blockingLookupRepoCache{+ path: path,+ lookupSeen: make(chan struct{}),+ releaseLookup: make(chan struct{}),+ }+}++func (c *blockingLookupRepoCache) Lookup(_, _ string) string {+ select {+ case <-c.lookupSeen:+ default:+ close(c.lookupSeen)+ }+ <-c.releaseLookup+ return c.path+}++func (c *blockingLookupRepoCache) Sync(string, []repocache.RepoInfo) error {+ return nil+}++func (c *blockingLookupRepoCache) CreateWorktree(repocache.WorktreeParams) (*repocache.WorktreeResult, error) {+ return nil, nil+}++func (c *blockingLookupRepoCache) waitForLookup(t *testing.T) {+ t.Helper()+ select {+ case <-c.lookupSeen:+ case <-time.After(time.Second):+ t.Fatal("registerTaskRepos did not call repo lookup")+ }+}++func (c *blockingLookupRepoCache) release() {+ c.releaseOnce.Do(func() {+ close(c.releaseLookup)+ })+}+func assertActiveTaskCount(t *testing.T, h http.HandlerFunc, want int64) {t.Helper()rec := httptest.NewRecorder()