Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-8419 fix(cli): warn when repo checkout falls back to a stale cache (#9738)
Yeldos Adetbekov727acf20082f
diff --git a/server/cmd/multica/cmd_repo.go b/server/cmd/multica/cmd_repo.goindex 14e3b746dd..8736436846 100644--- a/server/cmd/multica/cmd_repo.go+++ b/server/cmd/multica/cmd_repo.go@@ -436,19 +436,25 @@ func runRepoCheckout(cmd *cobra.Command, args []string) error {}// repoCheckoutResult is the daemon's /repo/checkout response. Daemons older-// than MUL-7284 never keep an existing checkout and omit Kept and the counts.+// than MUL-7284 never keep an existing checkout and omit Kept and the counts;+// older daemons also omit FetchError and only log a failed fetch.type repoCheckoutResult struct {Path string `json:"path"`BranchName string `json:"branch_name"`Kept string `json:"kept"`UncommittedFiles int `json:"uncommitted_files"`UnpushedCommits int `json:"unpushed_commits"`+ FetchError string `json:"fetch_error"`}// repoCheckoutSummary says what the checkout did. A kept checkout has to read// differently from a new branch off the default branch, or the agent works on// as if the checkout were fresh and loses track of what it holds.func repoCheckoutSummary(repoURL string, result repoCheckoutResult) string {+ return repoCheckoutOutcome(repoURL, result) + repoCheckoutFetchWarning(result)+}++func repoCheckoutOutcome(repoURL string, result repoCheckoutResult) string {if result.Kept == "" {return fmt.Sprintf("Checked out %s → %s (branch: %s)", repoURL, result.Path, result.BranchName)}@@ -459,13 +465,30 @@ func repoCheckoutSummary(repoURL string, result repoCheckoutResult) string {if result.Kept == "task_branch" {branch += ", this task's branch"}- return fmt.Sprintf("Kept the existing checkout of %s at %s (branch: %s; %d uncommitted file%s, %d unpushed commit%s): "+- "nothing was reset, cleaned, or switched; only remote refs were fetched.\n"++ untouched := "nothing was reset, cleaned, or switched; only remote refs were fetched."+ if result.FetchError != "" {+ // The fetch warning that follows says the remote refs were not updated.+ untouched = "nothing was reset, cleaned, or switched."+ }+ return fmt.Sprintf("Kept the existing checkout of %s at %s (branch: %s; %d uncommitted file%s, %d unpushed commit%s): %s\n"+"To discard its uncommitted changes and untracked files and start over on a new branch from the latest default branch (or --ref), "+"re-run with --fresh; commits stay on the old branch, but push any you still need first.",repoURL, result.Path, branch,result.UncommittedFiles, pluralS(result.UncommittedFiles),- result.UnpushedCommits, pluralS(result.UnpushedCommits))+ result.UnpushedCommits, pluralS(result.UnpushedCommits),+ untouched)+}++// repoCheckoutFetchWarning flags a checkout made from the daemon's cached copy+// because fetching the remote failed. Without it the checkout reads as current+// while its branches may be behind the remote.+func repoCheckoutFetchWarning(result repoCheckoutResult) string {+ if result.FetchError == "" {+ return ""+ }+ return "\nWarning: fetching the remote failed, so this checkout comes from the cached copy and may be behind the remote. " ++ "Compare its commit with the remote before relying on it.\n" ++ "Fetch error: " + result.FetchError}func repoCheckoutRetryDelay(value string, now time.Time) time.Duration {diff --git a/server/cmd/multica/cmd_repo_test.go b/server/cmd/multica/cmd_repo_test.goindex d1e7614b83..2942743006 100644--- a/server/cmd/multica/cmd_repo_test.go+++ b/server/cmd/multica/cmd_repo_test.go@@ -242,9 +242,10 @@ func TestRepoCheckoutSummary(t *testing.T) {t.Parallel()const repoURL = "https://github.com/org/repo.git"for _, tc := range []struct {- name string- result repoCheckoutResult- want []string+ name string+ result repoCheckoutResult+ want []string+ notWant []string}{{name: "new branch",@@ -256,7 +257,7 @@ func TestRepoCheckoutSummary(t *testing.T) {result: repoCheckoutResult{Path: "/work/repo", BranchName: "agent/test/old", Kept: "local_work", UncommittedFiles: 2, UnpushedCommits: 1},want: []string{"Kept the existing checkout of " + repoURL + " at /work/repo (branch: agent/test/old; 2 uncommitted files, 1 unpushed commit)",- "nothing was reset, cleaned, or switched",+ "nothing was reset, cleaned, or switched; only remote refs were fetched.","re-run with --fresh",},},@@ -270,6 +271,21 @@ func TestRepoCheckoutSummary(t *testing.T) {result: repoCheckoutResult{Path: "/work/repo", Kept: "local_work", UnpushedCommits: 3},want: []string{"(branch: detached HEAD; 0 uncommitted files, 3 unpushed commits)"},},+ {+ name: "fetch failed",+ result: repoCheckoutResult{Path: "/work/repo", BranchName: "agent/test/task", FetchError: "git fetch: fatal: unable to access remote"},+ want: []string{+ "Checked out " + repoURL + " → /work/repo (branch: agent/test/task)",+ "\nWarning: fetching the remote failed, so this checkout comes from the cached copy and may be behind the remote.",+ "Fetch error: git fetch: fatal: unable to access remote",+ },+ },+ {+ name: "kept after a failed fetch",+ result: repoCheckoutResult{Path: "/work/repo", BranchName: "agent/test/task", Kept: "task_branch", FetchError: "git fetch: boom"},+ want: []string{"nothing was reset, cleaned, or switched.\n", "Warning: fetching the remote failed", "Fetch error: git fetch: boom"},+ notWant: []string{"only remote refs were fetched"},+ },} {t.Run(tc.name, func(t *testing.T) {got := repoCheckoutSummary(repoURL, tc.result)@@ -278,9 +294,17 @@ func TestRepoCheckoutSummary(t *testing.T) {t.Fatalf("summary missing %q:\n%s", want, got)}}+ for _, notWant := range tc.notWant {+ if strings.Contains(got, notWant) {+ t.Fatalf("summary contains %q:\n%s", notWant, got)+ }+ }if kept := strings.HasPrefix(got, "Kept "); kept != (tc.result.Kept != "") {t.Fatalf("summary reads kept=%v for Kept=%q:\n%s", kept, tc.result.Kept, got)}+ if warned := strings.Contains(got, "Warning:"); warned != (tc.result.FetchError != "") {+ t.Fatalf("summary warns=%v for FetchError=%q:\n%s", warned, tc.result.FetchError, got)+ }})}}diff --git a/server/internal/daemon/repocache/cache.go b/server/internal/daemon/repocache/cache.goindex 8edc9de466..4981604ad4 100644--- a/server/internal/daemon/repocache/cache.go+++ b/server/internal/daemon/repocache/cache.go@@ -19,6 +19,7 @@ import ("time""github.com/multica-ai/multica/server/internal/daemon/processtree"+ "github.com/multica-ai/multica/server/pkg/redact")// gitEnv returns an environment for git subprocesses that contact remotes.@@ -776,6 +777,10 @@ type WorktreeResult struct {// that no remote-tracking ref reaches.UncommittedFiles int `json:"uncommitted_files,omitempty"`UnpushedCommits int `json:"unpushed_commits,omitempty"`+ // FetchError is set when refreshing the cache from the remote failed and+ // the checkout was based on what the cache already held, so it may be+ // behind the remote. It carries the redacted git error for the caller.+ FetchError string `json:"fetch_error,omitempty"`}// Reasons CreateWorktree keeps an existing checkout, reported in@@ -840,13 +845,15 @@ func (c *Cache) CreateWorktreeContext(ctx context.Context, params WorktreeParams// to the modern remote-tracking layout on first run, so subsequent fetches// never collide with the refs/heads/agent/* branches that worktree creation// locks in this same bare repo.+ var fetchErr stringif err := gitFetchContext(ctx, barePath); err != nil {if ctx.Err() != nil {return nil, context.Cause(ctx)}- // Non-fatal: preserve cached state and continue, but make the warning- // loud enough that it's findable in the daemon log. The agent will- // receive an older snapshot than the remote head.+ // Non-fatal: preserve cached state and continue. The agent will+ // receive an older snapshot than the remote head, so the result+ // carries the error back to the CLI as well as the daemon log.+ fetchErr = redact.Text(err.Error())c.logger.Warn("repo checkout: fetch failed, agent will see possibly stale code","url", params.RepoURL,"error", err,@@ -917,6 +924,7 @@ func (c *Cache) CreateWorktreeContext(ctx context.Context, params WorktreeParamsreturn nil, context.Cause(ctx)}+ result.FetchError = fetchErrc.logCheckoutReady("repo checkout: isolated checkout ready", params.RepoURL, baseRef, result)return result, nil}@@ -947,6 +955,7 @@ func (c *Cache) CreateWorktreeContext(ctx context.Context, params WorktreeParamsreturn nil, context.Cause(ctx)}+ result.FetchError = fetchErrc.logCheckoutReady("repo checkout: existing worktree updated", params.RepoURL, baseRef, result)return result, nil}@@ -985,6 +994,7 @@ func (c *Cache) CreateWorktreeContext(ctx context.Context, params WorktreeParamsreturn &WorktreeResult{Path: worktreePath,BranchName: actualBranch,+ FetchError: fetchErr,}, nil}diff --git a/server/internal/daemon/repocache/cache_test.go b/server/internal/daemon/repocache/cache_test.goindex f89b98f25d..254709097f 100644--- a/server/internal/daemon/repocache/cache_test.go+++ b/server/internal/daemon/repocache/cache_test.go@@ -775,6 +775,72 @@ func TestCreateWorktree(t *testing.T) {}}+func TestCreateWorktreeReportsFailedFetch(t *testing.T) {+ t.Parallel()+ for _, tc := range []struct {+ name string+ isolated bool+ }{+ {name: "shared worktree"},+ {name: "isolated checkout", isolated: true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()+ sourceRepo := createTestRepo(t)+ cache := New(t.TempDir(), testLogger())+ if err := cache.Sync("ws-1", []RepoInfo{{URL: sourceRepo}}); err != nil {+ t.Fatalf("sync failed: %v", err)+ }+ params := WorktreeParams{+ WorkspaceID: "ws-1",+ RepoURL: sourceRepo,+ WorkDir: t.TempDir(),+ AgentName: "QA",+ TaskID: "b2c3d4e5-f6a7-8901-bcde-f12345678901",+ IsolatedGitMetadata: tc.isolated,+ }++ result, err := cache.CreateWorktree(params)+ if err != nil {+ t.Fatalf("CreateWorktree failed: %v", err)+ }+ if result.FetchError != "" {+ t.Fatalf("FetchError = %q after a successful fetch, want empty", result.FetchError)+ }+ if got := isIsolatedCheckout(result.Path); got != tc.isolated {+ t.Fatalf("isolated checkout = %v, want %v", got, tc.isolated)+ }++ if err := os.RemoveAll(sourceRepo); err != nil {+ t.Fatalf("remove origin: %v", err)+ }++ // The reused checkout and a new one for another task both come from the+ // cached copy and must say so instead of reading as current.+ result, err = cache.CreateWorktree(params)+ if err != nil {+ t.Fatalf("CreateWorktree on the existing checkout failed: %v", err)+ }+ if !strings.Contains(result.FetchError, "git fetch") {+ t.Fatalf("existing checkout FetchError = %q, want the git fetch error", result.FetchError)+ }++ params.WorkDir = t.TempDir()+ params.TaskID = "c3d4e5f6-a7b8-9012-cdef-123456789012"+ result, err = cache.CreateWorktree(params)+ if err != nil {+ t.Fatalf("CreateWorktree with an unreachable origin failed: %v", err)+ }+ if !strings.Contains(result.FetchError, "git fetch") {+ t.Fatalf("new checkout FetchError = %q, want the git fetch error", result.FetchError)+ }+ if _, err := os.Stat(result.Path); err != nil {+ t.Fatalf("checkout from the cached copy is missing: %v", err)+ }+ })+ }+}+func TestCreateWorktreeWithIsolatedGitMetadata(t *testing.T) {t.Parallel()sourceRepo := createTestRepo(t)