From 45fc498212e4665fa8faf2df6dca355d849a52d3 Mon Sep 17 00:00:00 2001 From: Philip Langer Date: Thu, 12 Nov 2026 15:53:32 +0100 Subject: [PATCH 01/13] feat(daemon): suggest untracked worktrees, and retire worktrees gone The core provider surveys the linked worktrees of a project's repository. Those no workstream works in are carried on the project as untrackedWorktrees, with the number of sessions running there, instead of having their sessions adopted into a workstream without a branch. POST /worktree-workstreams adds one as a worktree workstream, which then behaves like one HomeShell created. HomeShell never adds one on its own. A worktree workstream whose worktree was removed outside HomeShell is retired like a Remove. Projections and removals in flight do not bring back or drop a workstream changed meanwhile. --- CONTEXT.md | 7 +- daemon/internal/api/api.go | 4 +- daemon/internal/api/workstreams.go | 32 + daemon/internal/api/workstreams_test.go | 74 ++ daemon/internal/domain/domain.go | 32 + daemon/internal/domain/domain_test.go | 52 ++ daemon/internal/git/worktree.go | 57 +- daemon/internal/git/worktree_test.go | 116 +++ daemon/internal/persist/state_test.go | 2 +- daemon/internal/provider/core.go | 40 +- daemon/internal/provider/core_survey.go | 101 +++ daemon/internal/provider/core_survey_test.go | 310 ++++++++ daemon/internal/provider/core_test.go | 33 + .../provider/default_workstream_test.go | 2 +- daemon/internal/provider/provider.go | 10 + daemon/internal/provider/reconcile.go | 373 +++++---- daemon/internal/provider/reconcile_survey.go | 327 ++++++++ .../provider/reconcile_survey_test.go | 745 ++++++++++++++++++ daemon/internal/provider/reconcile_test.go | 2 +- daemon/internal/provider/restart.go | 6 +- daemon/internal/provider/service.go | 240 +++++- .../internal/provider/service_remove_test.go | 97 +++ .../internal/provider/service_restart_test.go | 78 ++ .../provider/service_untracked_test.go | 229 ++++++ daemon/internal/provider/stub_test.go | 6 +- daemon/internal/provider/survey.go | 77 ++ .../internal/store/ephemeral_session_test.go | 98 +-- daemon/internal/store/launchconfig_test.go | 32 +- .../store/projection_persistence_test.go | 10 +- daemon/internal/store/session_title_test.go | 85 +- daemon/internal/store/store.go | 343 +++++--- daemon/internal/store/store_test.go | 36 +- .../internal/store/untracked_worktree_test.go | 256 ++++++ daemon/internal/store/workstream_name_test.go | 8 +- docs/API.md | 13 +- docs/ARCHITECTURE.md | 41 +- docs/CONFIGURATION.md | 2 +- docs/PROVIDERS.md | 12 + 38 files changed, 3582 insertions(+), 406 deletions(-) create mode 100644 daemon/internal/provider/core_survey.go create mode 100644 daemon/internal/provider/core_survey_test.go create mode 100644 daemon/internal/provider/reconcile_survey.go create mode 100644 daemon/internal/provider/reconcile_survey_test.go create mode 100644 daemon/internal/provider/service_untracked_test.go create mode 100644 daemon/internal/provider/survey.go create mode 100644 daemon/internal/store/untracked_worktree_test.go diff --git a/CONTEXT.md b/CONTEXT.md index f76f630..500d7ca 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -72,7 +72,7 @@ _Avoid_: feature flag **Adopted workstream**: A workstream HomeShell did not create, made up on the fly for sessions a provider reports in a -directory that no workstream claims. +directory that no workstream claims. A folder that is an untracked worktree is never adopted. ### Working directory @@ -90,6 +90,11 @@ _Avoid_: isolation mode A workstream's working directory when it is a **worktree** or a **folder** (copy), not the project directory itself. +**Untracked worktree**: +A linked git worktree of a project's repository that no workstream works in. HomeShell suggests +adding it; it never adds one on its own. +_Avoid_: orphan worktree, discovered worktree + **Note**: A Markdown file a workstream keeps in its working directory, readable by the user and by the agents working there. diff --git a/daemon/internal/api/api.go b/daemon/internal/api/api.go index 9d02f07..2dce92c 100644 --- a/daemon/internal/api/api.go +++ b/daemon/internal/api/api.go @@ -219,7 +219,9 @@ func writeProviderError(w http.ResponseWriter, err error) { errors.Is(err, provider.ErrSessionNeverStarted), errors.Is(err, provider.ErrSessionNotRestartable), errors.Is(err, provider.ErrCannotContinue), - errors.Is(err, provider.ErrOptionNotOffered): + errors.Is(err, provider.ErrOptionNotOffered), + // The folder is no suggestion of the project now: added, removed, or never one. + errors.Is(err, provider.ErrNotUntrackedWorktree): jsonError(w, err.Error(), http.StatusConflict) case errors.Is(err, provider.ErrConcurrencyLimit), errors.Is(err, provider.ErrSessionStopped), errors.Is(err, provider.ErrSessionGone), diff --git a/daemon/internal/api/workstreams.go b/daemon/internal/api/workstreams.go index 7a4abf0..81ccc55 100644 --- a/daemon/internal/api/workstreams.go +++ b/daemon/internal/api/workstreams.go @@ -15,6 +15,9 @@ func init() { register("POST /api/projects/{projectId}/default-workstream", func(deps Deps) http.HandlerFunc { return addDefaultWorkstreamHandler(deps) }) + register("POST /api/projects/{projectId}/worktree-workstreams", func(deps Deps) http.HandlerFunc { + return addUntrackedWorktreeHandler(deps) + }) register("POST /api/projects/{projectId}/workstream-draft", func(deps Deps) http.HandlerFunc { return workstreamDraftHandler(deps) }) @@ -76,6 +79,35 @@ func addDefaultWorkstreamHandler(deps Deps) http.HandlerFunc { } } +// addUntrackedWorktreeHandler makes a workstream of one of a project's +// untracked worktrees, the one at the body's path. +func addUntrackedWorktreeHandler(deps Deps) http.HandlerFunc { + return func(writer http.ResponseWriter, request *http.Request) { + if deps.Providers == nil { + jsonError(writer, "no providers are available", http.StatusServiceUnavailable) + return + } + var body struct { + Path string `json:"path"` + } + if err := json.NewDecoder(request.Body).Decode(&body); err != nil { + jsonError(writer, "invalid JSON", http.StatusBadRequest) + return + } + path := strings.TrimSpace(body.Path) + if path == "" { + jsonError(writer, "path must not be empty", http.StatusBadRequest) + return + } + added, err := deps.Providers.AddUntrackedWorktree(request.Context(), request.PathValue("projectId"), path) + if err != nil { + writeProviderError(writer, err) + return + } + writeJSON(writer, http.StatusCreated, added) + } +} + // workstreamDraftHandler answers a create's body with what the create would // derive for the values it leaves blank (name, branch, path), creating nothing. func workstreamDraftHandler(deps Deps) http.HandlerFunc { diff --git a/daemon/internal/api/workstreams_test.go b/daemon/internal/api/workstreams_test.go index 9ea05db..93e7d99 100644 --- a/daemon/internal/api/workstreams_test.go +++ b/daemon/internal/api/workstreams_test.go @@ -6,6 +6,9 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "os" + "os/exec" + "path/filepath" "strings" "testing" "time" @@ -381,3 +384,74 @@ func TestCreatedWorkstreamAppearsOnEventStream(t *testing.T) { t.Errorf("pushed state lacks the new workstream: %s", update.Data) } } + +// worktreeHandler builds a handler over a git project proj-git with one linked +// worktree on branch wt-a, and returns the worktree's path. +func worktreeHandler(t *testing.T) (http.Handler, string) { + t.Helper() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + repo, dir := t.TempDir(), filepath.Join(t.TempDir(), "wt") + for _, args := range [][]string{ + {"init", "-q", "-b", "main"}, + {"commit", "-q", "--allow-empty", "-m", "initial"}, + {"worktree", "add", "-q", "-b", "wt-a", dir}, + } { + cmd := exec.Command("git", append([]string{"-C", repo}, args...)...) + cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null", + "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com", + "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + } + projectStore := store.NewTestStore(t, []domain.Project{{ + ID: "proj-git", Name: "git", Path: repo, GitBacked: true, Workstreams: []domain.Workstream{}, + }}) + service := provider.NewService(projectStore, provider.NewRegistry(), 0) + service.Reconcile(context.Background()) + return handlerForTest(Deps{Version: "test", Store: projectStore, Heartbeat: time.Minute, Providers: service, + Notifications: notify.NewHub()}), dir +} + +func TestAddUntrackedWorktree(t *testing.T) { + handler, dir := worktreeHandler(t) + body, _ := json.Marshal(map[string]string{"path": dir}) + + recorder := do(t, handler, http.MethodPost, "/api/projects/proj-git/worktree-workstreams", string(body)) + if recorder.Code != http.StatusCreated { + t.Fatalf("status = %d (%s), want 201", recorder.Code, recorder.Body.String()) + } + var added domain.Workstream + if err := json.Unmarshal(recorder.Body.Bytes(), &added); err != nil { + t.Fatal(err) + } + if added.ID == "" || added.Name != "wt-a" || added.WorkingDir != dir { + t.Errorf("workstream = %+v, want wt-a in %s", added, dir) + } +} + +func TestAddUntrackedWorktreeRefusals(t *testing.T) { + handler, _ := worktreeHandler(t) + tests := []struct { + name string + body string + want int + }{ + {name: "no path", body: `{}`, want: http.StatusBadRequest}, + {name: "a blank path", body: `{"path":" "}`, want: http.StatusBadRequest}, + {name: "not an untracked worktree", body: `{"path":"/nowhere"}`, want: http.StatusConflict}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + recorder := do(t, handler, http.MethodPost, "/api/projects/proj-git/worktree-workstreams", tt.body) + var body struct { + Error string `json:"error"` + } + if recorder.Code != tt.want || json.Unmarshal(recorder.Body.Bytes(), &body) != nil || body.Error == "" { + t.Errorf("status = %d (%s), want %d with an error", recorder.Code, recorder.Body.String(), tt.want) + } + }) + } +} diff --git a/daemon/internal/domain/domain.go b/daemon/internal/domain/domain.go index 549d4c5..f3b17a8 100644 --- a/daemon/internal/domain/domain.go +++ b/daemon/internal/domain/domain.go @@ -96,6 +96,17 @@ func (s SessionState) Ended() bool { return s == SessionDone || s == SessionStopped } +// LiveCount is the number of sessions that have not ended (Terminal). +func LiveCount(sessions []Session) int { + live := 0 + for _, session := range sessions { + if !session.State.Terminal() { + live++ + } + } + return live +} + // statePrecedence is attention-first: error > waiting > unknown > running > // starting > idle > stopped. "Done" only surfaces once every child is terminal. // Starting ranks below running because a workstream that already has a live agent @@ -422,6 +433,21 @@ type Project struct { RuntimeState RuntimeState `json:"runtimeState"` Effective string `json:"effectiveState"` Workstreams []Workstream `json:"workstreams"` + // UntrackedWorktrees are the project's linked git worktrees no workstream + // works in, suggested for adding. Derived by the reconciler; not persisted. + UntrackedWorktrees []UntrackedWorktree `json:"untrackedWorktrees"` +} + +// UntrackedWorktree is a linked worktree of a project's repository that no +// workstream works in: a folder HomeShell suggests adding as a workstream. +// Derived by the reconciler; not persisted. +type UntrackedWorktree struct { + Name string `json:"name"` + Branch string `json:"branch,omitempty"` + Path string `json:"path"` + SessionCount int `json:"sessionCount"` // Sessions not ended; derived by Recompute + // Sessions are the ones reported in the folder, hidden until it is added. + Sessions []Session `json:"-"` } // StrategyLabel returns the human-readable display label for a raw strategy ID. @@ -480,6 +506,12 @@ func (p *Project) Recompute() { workstreamStates[i] = w.State workstreamRuntimeStates[i] = w.RuntimeState } + if p.UntrackedWorktrees == nil { + p.UntrackedWorktrees = []UntrackedWorktree{} + } + for i := range p.UntrackedWorktrees { + p.UntrackedWorktrees[i].SessionCount = LiveCount(p.UntrackedWorktrees[i].Sessions) + } p.State = Aggregate(workstreamStates) p.RuntimeState = AggregateRuntime(workstreamRuntimeStates) p.Effective = EffectiveState(p.RuntimeState, p.State) diff --git a/daemon/internal/domain/domain_test.go b/daemon/internal/domain/domain_test.go index eb00359..3658ce1 100644 --- a/daemon/internal/domain/domain_test.go +++ b/daemon/internal/domain/domain_test.go @@ -344,3 +344,55 @@ func TestRuntimeStateValid(t *testing.T) { } } } + +func TestRecomputeCountsLiveSessionsOnUntrackedWorktrees(t *testing.T) { + tests := []struct { + name string + states []SessionState + want int + }{ + {"none", nil, 0}, + {"only ended", []SessionState{SessionDone, SessionStopped, SessionError}, 0}, + {"live and ended", []SessionState{ + SessionRunning, SessionWaiting, SessionDone, SessionStopped, SessionError, + }, 2}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + var sessions []Session + for _, state := range test.states { + sessions = append(sessions, Session{ID: "s", State: state}) + } + worktree := UntrackedWorktree{Name: "wt", Path: "/wt", Sessions: sessions} + project := Project{UntrackedWorktrees: []UntrackedWorktree{worktree}} + project.Recompute() + if got := project.UntrackedWorktrees[0].SessionCount; got != test.want { + t.Errorf("SessionCount = %d, want %d", got, test.want) + } + }) + } +} + +// A suggestion's sessions stay hidden: clients see only how many are live. +func TestUntrackedWorktreeJSONLeavesOutItsSessions(t *testing.T) { + worktree := UntrackedWorktree{Name: "wt", Path: "/wt", Sessions: []Session{{ID: "s", State: SessionRunning}}} + data, err := json.Marshal(worktree) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(data), `"sessions"`) { + t.Errorf("suggestion JSON lists its sessions: %s", data) + } +} + +func TestRecomputeMakesNoUntrackedWorktreesAnEmptyList(t *testing.T) { + project := Project{} + project.Recompute() + data, err := json.Marshal(project) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(data), `"untrackedWorktrees":[]`) { + t.Errorf("want an empty array, got %s", data) + } +} diff --git a/daemon/internal/git/worktree.go b/daemon/internal/git/worktree.go index f7ad4c2..22b72af 100644 --- a/daemon/internal/git/worktree.go +++ b/daemon/internal/git/worktree.go @@ -227,6 +227,8 @@ type worktree struct { branch string prunable bool locked bool + // missing says the folder is not there: deleted, or out of reach. + missing bool } // deleted reports whether the worktree's folder was deleted: git marks it @@ -276,23 +278,66 @@ func worktrees(ctx context.Context, dir string) ([]worktree, error) { } } for i := range entries { - if entry := &entries[i]; !entry.prunable && !entry.locked { - _, err := os.Lstat(entry.path) - entry.prunable = errors.Is(err, fs.ErrNotExist) + entry := &entries[i] + if entry.prunable { + entry.missing = true + continue } + _, err := os.Lstat(entry.path) + entry.missing = errors.Is(err, fs.ErrNotExist) + entry.prunable = entry.missing && !entry.locked } return entries, nil } +// LinkedWorktree is a linked worktree of a repository. +type LinkedWorktree struct { + Path string + // Branch is the branch checked out there; "" on a detached HEAD. + Branch string + // Missing is set when the worktree's folder is not there: deleted, or out + // of reach (see worktree.deleted). + Missing bool + // Deleted is set when the worktree's folder was deleted (see worktree.deleted). + Deleted bool +} + +// LinkedWorktrees lists the linked worktrees of the repository at dir: every +// worktree but the first git lists, the main one (or the bare repository). +func LinkedWorktrees(ctx context.Context, dir string) ([]LinkedWorktree, error) { + entries, err := worktrees(ctx, dir) + if err != nil { + return nil, err + } + return linkedWorktrees(entries) +} + +// linkedWorktrees are the linked worktrees among a repository's entries, all +// but the first. No entry at all is an error. Why: git always lists the main +// worktree, so an empty listing was misread, and taken as is it would say every +// linked worktree is gone. +func linkedWorktrees(entries []worktree) ([]LinkedWorktree, error) { + if len(entries) == 0 { + return nil, errors.New("git worktree list: no worktree listed, not even the main one") + } + var linked []LinkedWorktree + for _, entry := range entries[1:] { + linked = append(linked, LinkedWorktree{ + Path: entry.path, Branch: entry.branch, Missing: entry.missing, Deleted: entry.deleted(), + }) + } + return linked, nil +} + // SamePath reports whether two paths name the same file, symlinks resolved: // git reports a worktree by its real path. func SamePath(first, second string) bool { - return realPath(first) == realPath(second) + return RealPath(first) == RealPath(second) } -// realPath is path absolute with the symlinks of its nearest existing ancestor +// RealPath is path absolute with the symlinks of its nearest existing ancestor // resolved. -func realPath(path string) string { +func RealPath(path string) string { absolute, err := filepath.Abs(path) if err != nil { return filepath.Clean(path) diff --git a/daemon/internal/git/worktree_test.go b/daemon/internal/git/worktree_test.go index d222b53..c88bea0 100644 --- a/daemon/internal/git/worktree_test.go +++ b/daemon/internal/git/worktree_test.go @@ -1195,3 +1195,119 @@ func TestEnclosingHeadBranch(t *testing.T) { }) } } + +func TestLinkedWorktreesSkipsTheMainWorktree(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + dir := t.TempDir() + initRepo(t, dir) + parent := t.TempDir() + withBranch, detached := filepath.Join(parent, "a"), filepath.Join(parent, "b") + runGit(t, dir, "worktree", "add", "-b", "wt-a", withBranch) + runGit(t, dir, "worktree", "add", "--detach", detached) + + got, err := LinkedWorktrees(context.Background(), dir) + if err != nil { + t.Fatal(err) + } + want := []LinkedWorktree{{Path: RealPath(withBranch), Branch: "wt-a"}, {Path: RealPath(detached)}} + slices.SortFunc(got, func(a, b LinkedWorktree) int { return strings.Compare(a.Path, b.Path) }) + if !slices.Equal(got, want) { + t.Errorf("LinkedWorktrees = %+v; want %+v", got, want) + } +} + +func TestLinkedWorktreesFromALinkedWorktree(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + dir := t.TempDir() + initRepo(t, dir) + linked := filepath.Join(t.TempDir(), "wt") + runGit(t, dir, "worktree", "add", "-b", "wt-a", linked) + + got, err := LinkedWorktrees(context.Background(), linked) + if err != nil { + t.Fatal(err) + } + if len(got) != 1 || !SamePath(got[0].Path, linked) { + t.Errorf("LinkedWorktrees from a linked worktree = %+v; want only %s", got, linked) + } +} + +func TestLinkedWorktreesMissingAndDeletedFlags(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + keep := func(_, _ string) string { return "" } + tests := []struct { + name string + lock bool + remove func(parent, worktree string) string + wantMissing bool + wantDeleted bool + }{ + {"folder there", false, keep, false, false}, + {"folder deleted", false, func(_, worktree string) string { return worktree }, true, true}, + {"folder out of reach", false, func(parent, _ string) string { return parent }, true, false}, + {"locked, folder out of reach", true, func(parent, _ string) string { return parent }, true, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + initRepo(t, dir) + parent := filepath.Join(t.TempDir(), "drive") + linked := filepath.Join(parent, "wt") + runGit(t, dir, "worktree", "add", "-b", "wt-a", linked) + if tt.lock { + runGit(t, dir, "worktree", "lock", linked) + } + if removed := tt.remove(parent, linked); removed != "" { + if err := os.RemoveAll(removed); err != nil { + t.Fatal(err) + } + } + + got, err := LinkedWorktrees(context.Background(), dir) + if err != nil { + t.Fatal(err) + } + if len(got) != 1 || got[0].Missing != tt.wantMissing || got[0].Deleted != tt.wantDeleted { + t.Errorf("LinkedWorktrees = %+v; want one with Missing=%v, Deleted=%v", + got, tt.wantMissing, tt.wantDeleted) + } + }) + } +} + +func TestLinkedWorktreesOutsideARepository(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + if _, err := LinkedWorktrees(context.Background(), t.TempDir()); err == nil { + t.Error("LinkedWorktrees outside a repository succeeded") + } +} + +func TestLinkedWorktreesOfEntries(t *testing.T) { + tests := []struct { + name string + entries []worktree + want []LinkedWorktree + wantErr bool + }{ + {name: "no entry, not even the main worktree", wantErr: true}, + {name: "the main worktree only", entries: []worktree{{path: "/repo"}}}, + {name: "a linked worktree", entries: []worktree{{path: "/repo"}, {path: "/wt", branch: "wt-a"}}, + want: []LinkedWorktree{{Path: "/wt", Branch: "wt-a"}}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := linkedWorktrees(tt.entries) + if (err != nil) != tt.wantErr || !slices.Equal(got, tt.want) { + t.Errorf("linkedWorktrees = %+v, %v; want %+v, error %v", got, err, tt.want, tt.wantErr) + } + }) + } +} diff --git a/daemon/internal/persist/state_test.go b/daemon/internal/persist/state_test.go index b994fa5..df8c07f 100644 --- a/daemon/internal/persist/state_test.go +++ b/daemon/internal/persist/state_test.go @@ -180,7 +180,7 @@ func TestMemorySaverRecordsAndFails(t *testing.T) { // so adding one to the domain is a conscious choice at this table. func TestSnapshotDropsOnlyLiveState(t *testing.T) { dropped := map[string][]string{ - "Project": {"State", "RuntimeState", "Effective"}, + "Project": {"State", "RuntimeState", "Effective", "UntrackedWorktrees"}, "Workstream": {"ProviderLabel", "Strategy", "InProjectFolder", "State", "RuntimeState", "Effective", "StatusMessage", "OpenModes", "Actions", "PlaceholderID", "CreateFailed"}, "Session": {"State", "Ended", "Preview", "Error", "PlaceholderID", "Restartable", "OutdatedSettings", "ObservationData"}, } diff --git a/daemon/internal/provider/core.go b/daemon/internal/provider/core.go index c92384d..8438688 100644 --- a/daemon/internal/provider/core.go +++ b/daemon/internal/provider/core.go @@ -41,8 +41,8 @@ const coreProviderLabel = "Default" // location, when set (via SetSettings), is read live to let the configured // workstreams.location override root; an empty configured value keeps root. // -// claimed holds the managed directories of creates in flight, which may not -// exist on disk yet. +// claimed holds the destinations of creates in flight, which may not exist on +// disk yet. type core struct { // SettingsHolder supplies the refresh interval the reconciler follows // in-place workstreams' branches at (Refresher). @@ -62,6 +62,7 @@ var ( _ StrategyTeller = (*core)(nil) _ ValidationSchemer = (*core)(nil) _ Refresher = (*core)(nil) + _ FolderSurveyor = (*core)(nil) ) // suggestTimeout bounds all of a form's git queries together. @@ -365,6 +366,15 @@ func worktreeRefusals(ctx context.Context, dir, branch, baseRef string, fromName func (c *core) Create(ctx context.Context, req CreateRequest) (domain.Workstream, error) { strategy, branch, baseRef, destDir := parseCoreConfig(req.Config, req.Project.GitBacked) name, branch := coreNameAndBranch(ctx, req, strategy, branch) + // The destination is claimed while the create runs, or for longer (CreateRequest.HoldClaim). + releaseClaim := func() {} + defer func() { + if req.HoldClaim != nil { + req.HoldClaim(releaseClaim) + } else { + releaseClaim() + } + }() // Resolve a user-supplied destination path (tilde expansion, reject relative). if destDir != "" { resolved, err := pathutil.Resolve(destDir) @@ -372,6 +382,9 @@ func (c *core) Create(ctx context.Context, req CreateRequest) (domain.Workstream return domain.Workstream{}, fmt.Errorf("invalid path: %w", err) } destDir = resolved + if wsfs.Strategy(strategy) != wsfs.InPlace { + releaseClaim = c.claimDir(destDir) + } } // Worktree/copy need a destination that does not yet exist. When the user // supplies none, materialize a managed directory under root, so these @@ -384,9 +397,7 @@ func (c *core) Create(ctx context.Context, req CreateRequest) (domain.Workstream if err := os.MkdirAll(root, 0o755); err != nil { return domain.Workstream{}, fmt.Errorf("creating workstream root %q: %w", root, err) } - managed, release := c.claimManagedDir(root, req.Project.Path, name) - defer release() - destDir = managed + destDir, releaseClaim = c.claimManagedDir(root, req.Project.Path, name) } dir, err := wsfs.Prepare(ctx, wsfs.Strategy(strategy), req.Project.Path, branch, baseRef, destDir, req.Project.GitBacked) if err != nil { @@ -470,11 +481,28 @@ func (c *core) claimManagedDir(root, projectDir, name string) (string, func()) { c.claimMu.Lock() defer c.claimMu.Unlock() dir := managedDir(root, projectDir, name, c.claimed) + return dir, c.claimLocked(dir) +} + +// claimDir holds dir, a Create's destination, until release, so a survey does +// not suggest the worktree being made there. +func (c *core) claimDir(dir string) func() { + c.claimMu.Lock() + defer c.claimMu.Unlock() + return c.claimLocked(dir) +} + +// claimLocked claims dir with claimMu held. A dir already claimed stays its +// first claimer's, and the release returned is a no-op. +func (c *core) claimLocked(dir string) func() { + if c.claimed[dir] { + return func() {} + } if c.claimed == nil { c.claimed = map[string]bool{} } c.claimed[dir] = true - return dir, func() { + return func() { c.claimMu.Lock() defer c.claimMu.Unlock() delete(c.claimed, dir) diff --git a/daemon/internal/provider/core_survey.go b/daemon/internal/provider/core_survey.go new file mode 100644 index 0000000..454538f --- /dev/null +++ b/daemon/internal/provider/core_survey.go @@ -0,0 +1,101 @@ +package provider + +import ( + "context" + "fmt" + + "github.com/eclipsesource/homeshell/daemon/internal/domain" + "github.com/eclipsesource/homeshell/daemon/internal/git" + wsfs "github.com/eclipsesource/homeshell/daemon/internal/provider/workstream" +) + +// ListFolders lists the linked worktrees of the project's repository: every +// one git lists but its first, the main worktree (see listingContext for its +// bound). +func (c *core) ListFolders(ctx context.Context, project ProjectRef) ([]Folder, error) { + if !project.GitBacked { + return nil, fmt.Errorf("project %q is not a git repository", project.Path) + } + ctx, cancel := c.listingContext(ctx) + defer cancel() + worktrees, err := git.LinkedWorktrees(ctx, project.Path) + if err != nil { + return nil, err + } + folders := make([]Folder, 0, len(worktrees)) + for _, worktree := range worktrees { + folders = append(folders, Folder{ + Path: worktree.Path, Branch: worktree.Branch, Missing: worktree.Missing, Gone: worktree.Deleted, + }) + } + return folders, nil +} + +// listingContext bounds a listing by the caller's deadline where it set one, as +// a user's Add does. Without one it is a refresh's, bounded like a terminal +// read. Why: a refresh lists every git project each tick, and a slow one must +// not hold up the others; a user waits on an Add, which may take longer. +func (c *core) listingContext(ctx context.Context) (context.Context, context.CancelFunc) { + if _, bounded := ctx.Deadline(); bounded { + return context.WithCancel(ctx) + } + return c.CaptureContext(ctx) +} + +// Survey suggests each listed worktree whose folder is there, not claimed and +// not being created, named after its branch or, detached, its folder. A +// worktree workstream of the core's vanishes once its folder is unlisted or +// gone; one only out of reach is still listed, and stays. +func (c *core) Survey(_ ProjectRef, folders []Folder, workstreams []domain.Workstream, claimed []string) Survey { + c.claimMu.Lock() + inFlight := make([]string, 0, len(c.claimed)) + for dir := range c.claimed { + inFlight = append(inFlight, dir) + } + c.claimMu.Unlock() + + taken := newFolderSet(claimed, inFlight) + var survey Survey + present := map[string]bool{} + for _, folder := range folders { + if folder.Gone { + continue + } + present[taken.realPath(folder.Path)] = true + if folder.Missing || taken.has(folder.Path) { + continue + } + survey.Untracked = append(survey.Untracked, domain.UntrackedWorktree{ + Name: coreWorkstreamName(folder.Path, folder.Branch), Branch: folder.Branch, Path: folder.Path, + }) + } + for _, workstream := range workstreams { + if workstream.Provider != CoreProviderID || isPlaceholder(workstream) || + wsfs.Strategy(parseCreatorData(workstream).Strategy) != wsfs.Worktree { + continue + } + if !present[taken.realPath(workstream.WorkingDir)] { + survey.Vanished = append(survey.Vanished, workstream.ID) + } + } + return survey +} + +// TrackFolder makes a worktree workstream of an untracked worktree, named after +// it and numbered past the names nameTaken reports taken (nil: none), a +// numbered name capped like a managed directory's. Remove then treats it like +// one the core created. +func (c *core) TrackFolder(folder domain.UntrackedWorktree, nameTaken func(name string) bool) domain.Workstream { + name := folder.Name + for number := 2; nameTaken != nil && nameTaken(name); number++ { + name = capWithSuffix(folder.Name, fmt.Sprintf("-%d", number)) + } + return domain.Workstream{ + Name: name, + StrategyID: string(wsfs.Worktree), + Branch: folder.Branch, + WorkingDir: folder.Path, + CreatorData: marshalCreatorData(coreCreatorData{Strategy: string(wsfs.Worktree)}), + Sessions: []domain.Session{}, + } +} diff --git a/daemon/internal/provider/core_survey_test.go b/daemon/internal/provider/core_survey_test.go new file mode 100644 index 0000000..f14060f --- /dev/null +++ b/daemon/internal/provider/core_survey_test.go @@ -0,0 +1,310 @@ +package provider + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "slices" + "strings" + "testing" + "time" + + "github.com/eclipsesource/homeshell/daemon/internal/domain" + "github.com/eclipsesource/homeshell/daemon/internal/git" +) + +// addWorktree adds a linked worktree of the repository at repo in a fresh +// folder, on a new branch or, given none, detached, and returns its path. +func addWorktree(t *testing.T, repo, branch string) string { + t.Helper() + dir := filepath.Join(t.TempDir(), "wt") + args := []string{"-C", repo, "worktree", "add", "-q", "--detach", dir} + if branch != "" { + args = []string{"-C", repo, "worktree", "add", "-q", "-b", branch, dir} + } + if out, err := exec.Command("git", args...).CombinedOutput(); err != nil { + t.Fatalf("git worktree add: %v\n%s", err, out) + } + return dir +} + +// surveyRepo surveys the repository at repo as the core sees it now. +func surveyRepo(t *testing.T, provider *core, repo string, workstreams []domain.Workstream, claimed []string) Survey { + t.Helper() + project := ProjectRef{Path: repo, GitBacked: true} + folders, err := provider.ListFolders(context.Background(), project) + if err != nil { + t.Fatalf("ListFolders: %v", err) + } + return provider.Survey(project, folders, workstreams, claimed) +} + +// worktreeWorkstream is a core worktree workstream working in dir. +func worktreeWorkstream(id, dir string) domain.Workstream { + return domain.Workstream{ID: id, Provider: CoreProviderID, WorkingDir: dir, + CreatorData: marshalCreatorData(coreCreatorData{Strategy: "worktree"})} +} + +// outOfReach puts the linked worktree at dir out of reach, as an unmounted +// drive does: its parent folder goes, and git keeps it registered. +func outOfReach(t *testing.T, dir string) { + t.Helper() + if err := os.RemoveAll(filepath.Dir(dir)); err != nil { + t.Fatal(err) + } +} + +// surveySetup prepares the repository at repo and returns the folder looked +// for, the claimed folders, and the suggestion wanted for it (nil: none). +type surveySetup func(t *testing.T, repo string) (string, []string, *domain.UntrackedWorktree) + +func TestCoreSurveySuggestsLinkedWorktrees(t *testing.T) { + tests := []struct { + name string + setup surveySetup + }{ + {"branch", func(t *testing.T, repo string) (string, []string, *domain.UntrackedWorktree) { + dir := addWorktree(t, repo, "wt-a") + return dir, nil, &domain.UntrackedWorktree{Name: "wt-a", Branch: "wt-a"} + }}, + {"detached", func(t *testing.T, repo string) (string, []string, *domain.UntrackedWorktree) { + dir := addWorktree(t, repo, "") + return dir, nil, &domain.UntrackedWorktree{Name: filepath.Base(dir)} + }}, + {"main checkout", func(t *testing.T, repo string) (string, []string, *domain.UntrackedWorktree) { + return repo, nil, nil + }}, + {"deleted folder", func(t *testing.T, repo string) (string, []string, *domain.UntrackedWorktree) { + dir := addWorktree(t, repo, "wt-a") + if err := os.RemoveAll(dir); err != nil { + t.Fatal(err) + } + return dir, nil, nil + }}, + {"folder out of reach", func(t *testing.T, repo string) (string, []string, *domain.UntrackedWorktree) { + dir := addWorktree(t, repo, "wt-a") + outOfReach(t, dir) + return dir, nil, nil + }}, + {"path in claimed", func(t *testing.T, repo string) (string, []string, *domain.UntrackedWorktree) { + dir := addWorktree(t, repo, "wt-a") + return dir, []string{dir}, nil + }}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + repo := gitProject(t) + dir, claimed, want := tt.setup(t, repo) + survey := surveyRepo(t, &core{}, repo, nil, claimed) + var got *domain.UntrackedWorktree + for i, untracked := range survey.Untracked { + if git.SamePath(untracked.Path, dir) { + got = &survey.Untracked[i] + } + } + if want == nil { + if got != nil { + t.Errorf("Survey suggests %+v; want no suggestion", *got) + } + return + } + if got == nil || got.Name != want.Name || got.Branch != want.Branch { + t.Errorf("Survey.Untracked = %+v; want %+v in %s", survey.Untracked, *want, dir) + } + }) + } +} + +// A project registered at a linked worktree lists the repository's other +// worktrees, its own folder among them: the folder it sits in is held, and the +// main checkout is git's first entry. +func TestCoreSurveyOfAProjectAtALinkedWorktree(t *testing.T) { + repo := gitProject(t) + projectDir, other := addWorktree(t, repo, "wt-a"), addWorktree(t, repo, "wt-b") + survey := surveyRepo(t, &core{}, projectDir, nil, []string{projectDir}) + if len(survey.Untracked) != 1 || !git.SamePath(survey.Untracked[0].Path, other) { + t.Errorf("Survey.Untracked = %+v; want only %s", survey.Untracked, other) + } +} + +// A worktree out of reach cannot be added, so it is not suggested, and may come +// back, so its workstream stays. +func TestCoreSurveyKeepsAWorktreeOutOfReach(t *testing.T) { + repo := gitProject(t) + tracked, untracked := addWorktree(t, repo, "wt-a"), addWorktree(t, repo, "wt-b") + outOfReach(t, tracked) + outOfReach(t, untracked) + survey := surveyRepo(t, &core{}, repo, []domain.Workstream{worktreeWorkstream("ws", tracked)}, nil) + if len(survey.Untracked) != 0 || len(survey.Vanished) != 0 { + t.Errorf("Survey = %+v; want nothing untracked and nothing vanished", survey) + } +} + +func TestCoreSurveyVanishesWorktreeWorkstreams(t *testing.T) { + listed, gone, unlisted := t.TempDir(), t.TempDir(), t.TempDir() + folders := []Folder{ + {Path: listed, Branch: "a", Missing: true}, + {Path: gone, Branch: "b", Missing: true, Gone: true}, + } + withStrategy := func(strategy string) domain.Workstream { + workstream := worktreeWorkstream("ws", unlisted) + workstream.CreatorData = marshalCreatorData(coreCreatorData{Strategy: strategy}) + return workstream + } + otherProvider := worktreeWorkstream("ws", unlisted) + otherProvider.Provider = "fake" + tests := []struct { + name string + workstream domain.Workstream + wantVanished bool + }{ + {"worktree whose folder is unlisted", worktreeWorkstream("ws", unlisted), true}, + {"worktree whose folder is listed gone", worktreeWorkstream("ws", gone), true}, + {"worktree whose folder is out of reach", worktreeWorkstream("ws", listed), false}, + {"in place", withStrategy("in-place"), false}, + {"copy", withStrategy("copy"), false}, + {"another provider's", otherProvider, false}, + {"placeholder", worktreeWorkstream(PlaceholderIDPrefix+"1", unlisted), false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + survey := (&core{}).Survey(ProjectRef{}, folders, []domain.Workstream{tt.workstream}, nil) + if got := slices.Contains(survey.Vanished, tt.workstream.ID); got != tt.wantVanished { + t.Errorf("vanished = %v (%v); want %v", got, survey.Vanished, tt.wantVanished) + } + }) + } +} + +func TestCoreSurveyResolvesSymlinks(t *testing.T) { + repo := gitProject(t) + tracked, claimedDir := addWorktree(t, repo, "wt-a"), addWorktree(t, repo, "wt-b") + links := t.TempDir() + linkTo := func(target, name string) string { + link := filepath.Join(links, name) + if err := os.Symlink(target, link); err != nil { + t.Fatal(err) + } + return link + } + workstream := worktreeWorkstream("ws", linkTo(tracked, "tracked")) + survey := surveyRepo(t, &core{}, repo, []domain.Workstream{workstream}, + []string{linkTo(tracked, "tracked-claim"), linkTo(claimedDir, "claimed")}) + if len(survey.Untracked) != 0 || len(survey.Vanished) != 0 { + t.Errorf("Survey = %+v; want nothing untracked and nothing vanished", survey) + } +} + +func TestCoreSurveySkipsAFolderBeingCreated(t *testing.T) { + repo := gitProject(t) + dir := addWorktree(t, repo, "wt-a") + provider := &core{} + release := provider.claimDir(dir) + defer release() + if survey := surveyRepo(t, provider, repo, nil, nil); len(survey.Untracked) != 0 { + t.Errorf("Survey.Untracked = %+v; want none while a create claims the folder", survey.Untracked) + } +} + +func TestCoreListFoldersOutsideGit(t *testing.T) { + if _, err := (&core{}).ListFolders(context.Background(), ProjectRef{Path: t.TempDir()}); err == nil { + t.Error("ListFolders on a project outside git succeeded") + } +} + +func TestCoreTrackFolderNumbersATakenName(t *testing.T) { + tests := []struct { + name string + taken []string + wantName string + }{ + {"free", nil, "wt-a"}, + {"taken", []string{"wt-a"}, "wt-a-2"}, + {"taken twice", []string{"wt-a", "wt-a-2"}, "wt-a-3"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + folder := domain.UntrackedWorktree{Name: "wt-a", Path: "/wt"} + got := (&core{}).TrackFolder(folder, func(name string) bool { return slices.Contains(tt.taken, name) }) + if got.Name != tt.wantName { + t.Errorf("TrackFolder name = %q; want %q", got.Name, tt.wantName) + } + }) + } +} + +func TestCoreTrackFolderCapsALongNumberedName(t *testing.T) { + long := strings.Repeat("long-branch-name-", 5) + "end" + folder := domain.UntrackedWorktree{Name: long, Path: "/wt"} + got := (&core{}).TrackFolder(folder, func(name string) bool { return name == long }).Name + if len(got) > maxNameBytes || !strings.HasSuffix(got, "-2") || + !strings.HasPrefix(long, strings.TrimSuffix(got, "-2")) { + t.Errorf("TrackFolder name = %q; want a prefix of the name numbered -2, within %d bytes", got, maxNameBytes) + } +} + +// A worktree added from a suggestion is one like the core creates: it follows +// its branch, and Remove deletes the worktree. +func TestCoreTrackFolderMakesAWorktreeWorkstream(t *testing.T) { + repo := gitProject(t) + project := ProjectRef{Path: repo, GitBacked: true} + provider := &core{} + branched := addWorktree(t, repo, "wt-a") + survey := surveyRepo(t, provider, repo, nil, nil) + if len(survey.Untracked) != 1 { + t.Fatalf("Survey.Untracked = %+v; want %s", survey.Untracked, branched) + } + + workstream := provider.TrackFolder(survey.Untracked[0], nil) + if workstream.Name != "wt-a" || workstream.Branch != "wt-a" || !git.SamePath(workstream.WorkingDir, branched) || + workstream.StrategyID != "worktree" { + t.Errorf("TrackFolder = %q on %q in %q (%s); want wt-a on wt-a in %s (worktree)", + workstream.Name, workstream.Branch, workstream.WorkingDir, workstream.StrategyID, branched) + } + if refreshed := provider.Refresh(context.Background(), project, workstream); refreshed.Branch != "wt-a" { + t.Errorf("Refresh = branch %q; want wt-a", refreshed.Branch) + } + if err := provider.Remove(context.Background(), project, workstream, RemoveOptions{}); err != nil { + t.Fatalf("Remove: %v", err) + } + if pathExists(branched) { + t.Errorf("%s still exists after Remove", branched) + } + out, err := exec.Command("git", "-C", repo, "worktree", "list").CombinedOutput() + if err != nil { + t.Fatalf("git worktree list: %v\n%s", err, out) + } + if strings.Contains(string(out), git.RealPath(branched)) { + t.Errorf("git worktree list still lists %s:\n%s", branched, out) + } +} + +func TestCoreListingContext(t *testing.T) { + tests := []struct { + name string + callerTime time.Duration // the caller's deadline; 0: none + wantAtMost time.Duration + wantOver time.Duration + }{ + {name: "a refresh, bounded like a terminal read", wantAtMost: time.Second}, + {name: "an Add, bounded by its caller", callerTime: 5 * time.Second, wantAtMost: 5 * time.Second, + wantOver: 4 * time.Second}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + if tt.callerTime > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, tt.callerTime) + defer cancel() + } + listingCtx, cancel := (&core{}).listingContext(ctx) + defer cancel() + deadline, ok := listingCtx.Deadline() + if left := time.Until(deadline); !ok || left > tt.wantAtMost || left <= tt.wantOver { + t.Errorf("listing deadline in %v (set %v); want within (%v, %v]", left, ok, tt.wantOver, tt.wantAtMost) + } + }) + } +} diff --git a/daemon/internal/provider/core_test.go b/daemon/internal/provider/core_test.go index a706536..a158271 100644 --- a/daemon/internal/provider/core_test.go +++ b/daemon/internal/provider/core_test.go @@ -740,6 +740,39 @@ func TestCoreCreatesClaimDistinctManagedDirs(t *testing.T) { } } +// A create given a path holds it while it runs, so a survey does not suggest +// the worktree being made there, and lets go of it however it ends. +func TestCoreCreateReleasesTheClaimOnAPath(t *testing.T) { + tests := []struct { + name string + gitBacked bool + wantErr bool + }{ + {"created", true, false}, + {"failed", false, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + provider := &core{root: t.TempDir()} + dest := filepath.Join(t.TempDir(), "wt") + config, err := json.Marshal(map[string]string{"strategy": "worktree", "branch": "wt-a", "path": dest}) + if err != nil { + t.Fatal(err) + } + _, err = provider.Create(context.Background(), CreateRequest{ + Project: ProjectRef{Path: gitProject(t), GitBacked: tt.gitBacked}, + Config: config, + }) + if (err != nil) != tt.wantErr { + t.Fatalf("Create: err = %v; want error %v", err, tt.wantErr) + } + if len(provider.claimed) != 0 { + t.Errorf("claims left after the create: %v", provider.claimed) + } + }) + } +} + // A branch derived from a long name is capped, at a separator where one is near // the end, and keeps the number a name ends in; one typed is kept as it is. func TestCoreNameAndBranchCapsADerivedBranch(t *testing.T) { diff --git a/daemon/internal/provider/default_workstream_test.go b/daemon/internal/provider/default_workstream_test.go index 01a46a1..1c54206 100644 --- a/daemon/internal/provider/default_workstream_test.go +++ b/daemon/internal/provider/default_workstream_test.go @@ -550,7 +550,7 @@ func (p *pausingRefresher) Refresh(_ context.Context, _ ProjectRef, ws domain.Wo // A rename made while a projection pass is in flight outlives the pass, and the // next pass moves the branch under it. The store's rules for what a pass keeps -// are tested with store.ReplaceWorkstreams; this is the reconciler handing over +// are tested with store.ReplaceProjection; this is the reconciler handing over // its read. func TestReconcileKeepsARenameMadeDuringThePass(t *testing.T) { st := store.NewTestStore(t, []domain.Project{{ID: "p1", Path: "/p1", Workstreams: []domain.Workstream{ diff --git a/daemon/internal/provider/provider.go b/daemon/internal/provider/provider.go index c62a9b6..3c34e0e 100644 --- a/daemon/internal/provider/provider.go +++ b/daemon/internal/provider/provider.go @@ -360,6 +360,11 @@ type ProjectRef struct { FolderTaken bool } +// projectRefOf is the reference to project a provider is handed. +func projectRefOf(project domain.Project) ProjectRef { + return ProjectRef{ID: project.ID, Path: project.Path, GitBacked: project.GitBacked} +} + // WorkstreamRef identifies a workstream within a project. type WorkstreamRef struct { ProjectID string @@ -398,6 +403,11 @@ type CreateRequest struct { WorkingDirUser func(dir string) string Config json.RawMessage // user-supplied values, valid against the provider's schema OnProgress func(string) // optional callback for intermediate status updates + // HoldClaim, when set, is handed the release of the folder the provider holds while it creates + // there, and the caller releases it once the workstream is stored or the create failed. Unset, the + // provider releases it as Create returns. Why: until stored, no workstream claims the folder, and a + // survey in between would suggest it. + HoldClaim func(release func()) } // Capabilities is the data-only capability declaration. Behavioural flags are diff --git a/daemon/internal/provider/reconcile.go b/daemon/internal/provider/reconcile.go index 7ae134b..436becc 100644 --- a/daemon/internal/provider/reconcile.go +++ b/daemon/internal/provider/reconcile.go @@ -5,7 +5,9 @@ import ( "crypto/sha1" "fmt" "log/slog" + "maps" "path/filepath" + "slices" "strings" "sync" "time" @@ -20,6 +22,17 @@ func isPlaceholder(ws domain.Workstream) bool { return strings.HasPrefix(ws.ID, PlaceholderIDPrefix) } +// adoptedIDPrefix marks a workstream the reconciler adopted for sessions found +// in a folder no workstream claimed (adoptedWorkstreamID). +const adoptedIDPrefix = "disc-" + +// isAdopted reports whether workstream is a workstream the reconciler adopted. It does +// not claim its folder: a folder that turns out to be an untracked worktree is +// suggested instead, and the adopted workstream dropped. +func isAdopted(workstream domain.Workstream) bool { + return strings.HasPrefix(workstream.ID, adoptedIDPrefix) +} + // Reconciler keeps the store's workstreams in sync with session-provider state: // it attaches each discovered session to the workstream with a matching // WorkingDir, adopting a workstream when none matches. It implements Sink so @@ -38,39 +51,68 @@ type Reconciler struct { // and sessions (open modes, materialization, restartability). The owning // Service supplies it; nil leaves them empty. annotate func(projectID string, workstreams []domain.Workstream) - // projectLocks serializes projection passes per project: each ends in - // ReplaceWorkstreams, which writes a whole list derived from its own read, so - // two interleaved passes over one project would have the later revert the - // earlier. Per project rather than global, so a reconcile triggered by an API - // call does not queue behind an unrelated project's provider calls. - locksMu sync.Mutex - projectLocks map[string]*sync.Mutex + // projects holds what passes keep of each project (projectState). + projectsMu sync.Mutex + projects map[string]*projectState + // cachedListings caches each FolderSurveyor's folder listing of each project. + cachedListings *listingCache + // retire starts removing a workstream whose folder vanished, unless a + // removal of it is under way. Passes keep it listed until the removal drops + // its record. The owning Service supplies it; nil leaves it listed. + retire func(ctx context.Context, ref WorkstreamRef) } -// projectLock returns the mutex guarding projection passes for a project. -func (r *Reconciler) projectLock(projectID string) *sync.Mutex { - r.locksMu.Lock() - defer r.locksMu.Unlock() - if r.projectLocks == nil { - r.projectLocks = map[string]*sync.Mutex{} +// projectState is what the reconciler keeps of a project between passes. +type projectState struct { + // passLock serializes projection passes over the project: each ends in + // ReplaceProjection, which writes a whole list derived from its own read, so + // two interleaved passes would have the later revert the earlier. Per project + // rather than global, so a reconcile triggered by an API call does not queue + // behind an unrelated project's provider calls. + passLock sync.Mutex + // firstSeen is when a pass first saw each of the project's workstreams, by + // ID; held under passLock. + firstSeen map[string]time.Time +} + +// sight stamps the workstreams a pass sees for the first time with now, drops +// those gone, and returns when each was first seen. Called with passLock held. +func (ps *projectState) sight(workstreams []domain.Workstream) map[string]time.Time { + now := time.Now() + present := map[string]bool{} + for _, workstream := range workstreams { + present[workstream.ID] = true + if _, seen := ps.firstSeen[workstream.ID]; !seen { + ps.firstSeen[workstream.ID] = now + } } - lock, ok := r.projectLocks[projectID] + maps.DeleteFunc(ps.firstSeen, func(workstreamID string, _ time.Time) bool { return !present[workstreamID] }) + return ps.firstSeen +} + +// projectState returns what passes keep of a project. +func (r *Reconciler) projectState(projectID string) *projectState { + r.projectsMu.Lock() + defer r.projectsMu.Unlock() + state, ok := r.projects[projectID] if !ok { - lock = &sync.Mutex{} - r.projectLocks[projectID] = lock + state = &projectState{firstSeen: map[string]time.Time{}} + r.projects[projectID] = state } - return lock + return state } // NewReconciler builds a reconciler. A poll of 0 disables periodic polling // (the reconciler then refreshes only on explicit calls and Sink.Changed). func NewReconciler(st *store.Store, reg *Registry, poll time.Duration) *Reconciler { return &Reconciler{ - store: st, - registry: reg, - poll: poll, - refreshEvery: 2 * time.Second, - trigger: make(chan struct{}, 1), + store: st, + registry: reg, + poll: poll, + refreshEvery: 2 * time.Second, + trigger: make(chan struct{}, 1), + projects: map[string]*projectState{}, + cachedListings: newListingCache(), } } @@ -130,169 +172,224 @@ func (r *Reconciler) reconcileOnly(ctx context.Context, only dirtySet) { return } for _, project := range r.store.Projects() { - r.reconcileProjectOnly(ctx, project, only) + r.reconcileProjectOnly(ctx, projectRefOf(project), only) } r.store.RetryUnsavedWrite() } // ReconcileProject reconciles a single project by ID. func (r *Reconciler) ReconcileProject(ctx context.Context, projectID string) error { - for _, project := range r.store.Projects() { - if project.ID == projectID { - r.reconcileProject(ctx, project) - return nil - } + project, err := r.store.Project(projectID) + if err != nil { + return err } - return fmt.Errorf("project %q not found", projectID) + r.reconcileProjectOnly(ctx, projectRefOf(project), dirtySet{all: true}) + return nil } -func (r *Reconciler) reconcileProject(ctx context.Context, project domain.Project) { - r.reconcileProjectOnly(ctx, project, dirtySet{all: true}) +// reconcileProjectOnly lists the project's folders, projects it, then has the +// workstreams whose folder vanished retired. Why around the pass: listing runs +// git and retiring is service work, and neither must hold up the pass lock. +// Listing first also means a workstream the pass sees for the first time is +// stamped after the listing began, so that listing cannot vanish it. +func (r *Reconciler) reconcileProjectOnly(ctx context.Context, project ProjectRef, only dirtySet) { + listings, listErr := r.listings(ctx, project, r.listing) + vanished := r.projectPass(ctx, project, only, listings, listErr) + if r.retire == nil { + return + } + for _, workstreamID := range vanished { + r.retire(ctx, WorkstreamRef{ProjectID: project.ID, WorkstreamID: workstreamID}) + } } -func (r *Reconciler) reconcileProjectOnly(ctx context.Context, project domain.Project, only dirtySet) { - lock := r.projectLock(project.ID) - lock.Lock() - defer lock.Unlock() - ref := ProjectRef{ID: project.ID, Path: project.Path, GitBacked: project.GitBacked} - - // Gather the sessions every available session provider observes, keyed by - // the cleaned working directory they run in. refreshed records which - // providers reported this cycle, so a provider that hit a transient error - // does not flush its sessions from the store. - refreshed := map[string]bool{} - discovered := map[string][]domain.Session{} - for _, p := range r.registry.SessionProviders() { - if !only.has(p.ID()) { +// discovery is what the session providers reported in one pass. +type discovery struct { + // sessions are the sessions reported, by the cleaned folder they run in. + sessions map[string][]domain.Session + // refreshed are the providers that reported, so one that hit a transient + // error does not flush its sessions from the store. + refreshed map[string]bool + // superseded are the IDs of sessions reported from a folder: a fresh report + // supersedes any retained session with the same ID, wherever it sits, so an + // ID cannot land in the result set twice. Empty-dir reports are excluded: + // neither pass attaches them anywhere, so letting them supersede would drop + // the stored session outright. + superseded map[string]bool +} + +// discover gathers the sessions every available session provider in only +// observes in the project. +func (r *Reconciler) discover(ctx context.Context, project ProjectRef, only dirtySet) discovery { + found := discovery{ + sessions: map[string][]domain.Session{}, + refreshed: map[string]bool{}, + superseded: map[string]bool{}, + } + for _, sessionProvider := range r.registry.SessionProviders() { + if !only.has(sessionProvider.ID()) { continue } - available, err := p.Available(ctx) + available, err := sessionProvider.Available(ctx) if err != nil { - slog.Warn("provider availability check failed", "provider", p.ID(), "error", err) + slog.Warn("provider availability check failed", "provider", sessionProvider.ID(), "error", err) continue } if !available { // Its sessions are gone, so count it as refreshed with none. - refreshed[p.ID()] = true + found.refreshed[sessionProvider.ID()] = true continue } var sessions []DiscoveredSession - if watched, ok := p.(WatchedDiscoverer); !only.all && ok { - sessions, err = watched.DiscoverWatchedSessions(ctx, ref) + if watched, ok := sessionProvider.(WatchedDiscoverer); !only.all && ok { + sessions, err = watched.DiscoverWatchedSessions(ctx, project) } else { - sessions, err = p.DiscoverSessions(ctx, ref) + sessions, err = sessionProvider.DiscoverSessions(ctx, project) } if err != nil { - slog.Warn("provider failed to discover sessions", "provider", p.ID(), "project", project.ID, "error", err) + slog.Warn("provider failed to discover sessions", + "provider", sessionProvider.ID(), "project", project.ID, "error", err) continue } - refreshed[p.ID()] = true - for _, ds := range sessions { - sess := ds.Session - sess.Provider = p.ID() - dir := cleanDir(ds.WorkingDir) - discovered[dir] = append(discovered[dir], sess) + found.refreshed[sessionProvider.ID()] = true + for _, discovered := range sessions { + session := discovered.Session + session.Provider = sessionProvider.ID() + dir := cleanDir(discovered.WorkingDir) + found.sessions[dir] = append(found.sessions[dir], session) + if dir != "" { + found.superseded[session.ID] = true + } } } + return found +} - // A fresh discovery supersedes any retained session with the same ID, - // wherever it sits, so an ID cannot land in the result set twice. - // Empty-dir discoveries are excluded: neither pass attaches them anywhere, - // so letting them supersede would drop the stored session outright. - superseded := map[string]bool{} - for dir, sessions := range discovered { - if dir == "" { - continue - } - for _, sess := range sessions { - superseded[sess.ID] = true - } - } +// projectPass is one projection pass over a project, against its listings +// (listErr: one failed). It returns the IDs of the workstreams whose folder +// vanished; they stay listed, with the sessions there, until their removal +// drops them. +func (r *Reconciler) projectPass(ctx context.Context, project ProjectRef, only dirtySet, + listings surveyorListings, listErr error) []string { + state := r.projectState(project.ID) + state.passLock.Lock() + defer state.passLock.Unlock() + + found := r.discover(ctx, project, only) // Discovery can be slow and workstreams land concurrently, so re-read the - // store: ReplaceWorkstreams below writes the whole list, and deriving it - // from the pre-discovery snapshot would erase anything added in between. - // ReplaceWorkstreams is handed this read, so it keeps what changes after it + // store: ReplaceProjection below writes the whole list, and deriving it + // from a pre-discovery snapshot would erase anything added in between. + // ReplaceProjection is handed this read, so it keeps what changes after it // while the pass refreshes and annotates (store.keepChangesSinceRead). - workstreams := project.Workstreams - for _, current := range r.store.Projects() { - if current.ID == project.ID { - workstreams = current.Workstreams - break - } + projects := r.store.Projects() + index := slices.IndexFunc(projects, func(candidate domain.Project) bool { return candidate.ID == project.ID }) + if index < 0 { + r.forgetProject(project.ID) // removed meanwhile + return nil } + stored := projects[index] + firstSeen := state.sight(stored.Workstreams) + + // A failed listing is no empty listing: nothing vanishes, and the stored + // suggestions stay, unless claimed since. + claimed := claimedFolders(projects) + var survey folderSurvey + if listErr != nil { + survey.untracked = unclaimedSuggestions(stored.UntrackedWorktrees, claimed) + } else { + survey = listings.survey(project, stored.Workstreams, claimed, firstSeen) + } + if survey.stale { + r.cachedListings.forget(project.ID) + } + suggested, unhidden := r.hideSessions(survey.untracked, stored, found) result := []domain.Workstream{} - matchedDirs := map[string]bool{} + var vanished []string + matchedDirs := maps.Clone(suggested) // Pass 1: existing workstreams keep their identity; re-attach sessions by - // matching working directory. - for _, ws := range workstreams { - if isPlaceholder(ws) { - result = append(result, ws) + // matching working directory. One working in a folder suggested until now + // takes the sessions hidden there; one adopted in a folder suggested now + // gave its sessions to the suggestion. One whose folder vanished stays, + // and holds its folder's sessions, until its removal is done. + for _, workstream := range stored.Workstreams { + if isPlaceholder(workstream) { + result = append(result, workstream) continue } - dir := cleanDir(ws.WorkingDir) - discoveredHere := discovered[dir] - - sessions := []domain.Session{} - // Retain a session only while a registered provider could still confirm it - // but did not report this cycle, and no fresh discovery supersedes it. A - // session whose provider is not in the registry is dropped: nothing can - // ever confirm it or route to it. - for _, sess := range ws.Sessions { - // A starting placeholder is retained unconditionally: no provider reports it - // (it stands for a session that does not exist yet), so the rule above would - // drop it on the first cycle after a create — mid-build, which is exactly - // when it is the only thing the user has to look at. - // Service.finishCreateSession is what ends its life — except when the create - // fails, where the same row stays on as the `error` record of the attempt and - // only a removal by the user clears it. Because it is never persisted a daemon - // restart cannot leave one behind either way. - // - // This covers the placeholders that existed when the snapshot was taken. One - // that appears *while* the providers are being asked is invisible here, and is - // carried over by the store instead (store.retainInFlightLocked) — the only - // place that can see it, since it is the only one holding the live state. - if IsStartingPlaceholder(sess) { - sessions = append(sessions, sess) - continue - } - if _, known := r.registry.SessionProvider(sess.Provider); !known { - continue - } - if refreshed[sess.Provider] || superseded[sess.ID] { - continue - } - sessions = append(sessions, sess) + dir := cleanDir(workstream.WorkingDir) + if isAdopted(workstream) && suggested[dir] { + continue } + sessions := r.retainedSessions(slices.Concat(workstream.Sessions, unhidden[dir]), found) if dir != "" { - sessions = append(sessions, discoveredHere...) + sessions = append(sessions, found.sessions[dir]...) matchedDirs[dir] = true } - ws.Sessions = sessions - result = append(result, ws) + workstream.Sessions = sessions + result = append(result, workstream) + if survey.vanished[workstream.ID] { + vanished = append(vanished, workstream.ID) + } } // Pass 2: discovered sessions whose directory matches no known workstream - // get a workstream adopted for them. - for dir, sessions := range discovered { + // or suggestion get a workstream adopted for them. + for dir, sessions := range found.sessions { if dir == "" || matchedDirs[dir] { continue } result = append(result, adoptedWorkstream(dir, sessions)) } - r.refresh(ctx, ref, result) + r.refresh(ctx, project, result) // Derived last, so it sees each workstream's final session set. if r.annotate != nil { r.annotate(project.ID, result) } - if err := r.store.ReplaceWorkstreams(project.ID, workstreams, result); err != nil { + if err := r.store.ReplaceProjection(project.ID, stored.Workstreams, result, survey.untracked); err != nil { slog.Warn("projecting workstreams failed", "project", project.ID, "error", err) } + return vanished +} + +// retainedSessions are the stored sessions a pass keeps: only while a +// registered provider could still confirm one but did not report in found, +// and no fresh discovery supersedes it. A session whose provider is not in the +// registry is dropped: nothing can ever confirm it or route to it. +func (r *Reconciler) retainedSessions(stored []domain.Session, found discovery) []domain.Session { + sessions := []domain.Session{} + for _, session := range stored { + // A starting placeholder is retained unconditionally: no provider reports it + // (it stands for a session that does not exist yet), so the retention rule + // would drop it on the first cycle after a create — mid-build, which is + // exactly when it is the only thing the user has to look at. + // Service.finishCreateSession is what ends its life — except when the create + // fails, where the same row stays on as the `error` record of the attempt and + // only a removal by the user clears it. Because it is never persisted a daemon + // restart cannot leave one behind either way. + // + // This covers the placeholders that existed when the snapshot was taken. One + // that appears *while* the providers are being asked is invisible here, and is + // carried over by the store instead (store.retainInFlightLocked) — the only + // place that can see it, since it is the only one holding the live state. + if IsStartingPlaceholder(session) { + sessions = append(sessions, session) + continue + } + if _, known := r.registry.SessionProvider(session.Provider); !known { + continue + } + if found.refreshed[session.Provider] || found.superseded[session.ID] { + continue + } + sessions = append(sessions, session) + } + return sessions } // refresh has each workstream's provider bring it up to date (Refresher). A @@ -325,9 +422,10 @@ func (r *Reconciler) refresh(ctx context.Context, project ProjectRef, workstream // branchMoved reports whether a refresh would move any of providerID's // workstreams to another branch. The branch is what moves a name too, so it is // all that is compared. -func (r *Reconciler) branchMoved(ctx context.Context, providerID string, refresher Refresher) bool { - for _, project := range r.store.Projects() { - ref := ProjectRef{ID: project.ID, Path: project.Path, GitBacked: project.GitBacked} +func (r *Reconciler) branchMoved(ctx context.Context, projects []domain.Project, providerID string, + refresher Refresher) bool { + for _, project := range projects { + ref := projectRefOf(project) for _, workstream := range project.Workstreams { if workstream.Provider != providerID || isPlaceholder(workstream) { continue @@ -361,7 +459,7 @@ func adoptedWorkstream(dir string, sessions []domain.Session) domain.Workstream // adopted workstream keeps the same identity across reconcile cycles. func adoptedWorkstreamID(dir string) string { sum := sha1.Sum([]byte(dir)) - return fmt.Sprintf("disc-%x", sum[:8]) + return fmt.Sprintf("%s%x", adoptedIDPrefix, sum[:8]) } func cleanDir(dir string) string { @@ -381,12 +479,19 @@ func (r *Reconciler) Run(ctx context.Context) { } } for _, workstreamProvider := range r.registry.WorkstreamProviders() { - if refresher, ok := workstreamProvider.(Refresher); ok { - sink := watcherSink{reconciler: r, providerID: workstreamProvider.ID()} - go WatchLoop(ctx, sink, refresher.RefreshInterval, r.refreshEvery, func(ctx context.Context) bool { - return r.branchMoved(ctx, workstreamProvider.ID(), refresher) - }) + refresher, ok := workstreamProvider.(Refresher) + if !ok { + continue } + providerID := workstreamProvider.ID() + surveyor, surveys := workstreamProvider.(FolderSurveyor) + sink := watcherSink{reconciler: r, providerID: providerID} + go WatchLoop(ctx, sink, refresher.RefreshInterval, r.refreshEvery, func(ctx context.Context) bool { + projects := r.store.Projects() + // Both asked every tick: the folder listings are refreshed on each. + changed := surveys && r.foldersChanged(ctx, projects, surveyor) + return r.branchMoved(ctx, projects, providerID, refresher) || changed + }) } r.Reconcile(ctx) diff --git a/daemon/internal/provider/reconcile_survey.go b/daemon/internal/provider/reconcile_survey.go new file mode 100644 index 0000000..322e64f --- /dev/null +++ b/daemon/internal/provider/reconcile_survey.go @@ -0,0 +1,327 @@ +package provider + +import ( + "context" + "log/slog" + "maps" + "slices" + "sync" + "time" + + "github.com/eclipsesource/homeshell/daemon/internal/domain" +) + +// listingKey names one surveyor's listing of one project. +type listingKey struct { + providerID string + projectID string +} + +// folderListing is a surveyor's listing of a project's folders. +type folderListing struct { + folders []Folder + // takenAt is when the listing began. + takenAt time.Time +} + +// listingCache holds each surveyor's latest listing of each project, so a +// pass lists only when none is held, and which listings' latest attempt +// failed, so a project out of reach warns once per failure streak. +type listingCache struct { + mu sync.Mutex + entries map[listingKey]listingEntry +} + +// listingEntry is what the cache holds of one listing. +type listingEntry struct { + // listing is the latest that succeeded; a zero takenAt while none did. + listing folderListing + failing bool +} + +func newListingCache() *listingCache { + return &listingCache{entries: map[listingKey]listingEntry{}} +} + +// get returns the listing held for key. +func (c *listingCache) get(key listingKey) (folderListing, bool) { + c.mu.Lock() + defer c.mu.Unlock() + listing := c.entries[key].listing + return listing, !listing.takenAt.IsZero() +} + +// put holds listing for key, unless the one held began later. +func (c *listingCache) put(key listingKey, listing folderListing) { + c.mu.Lock() + defer c.mu.Unlock() + entry := c.entries[key] + if listing.takenAt.After(entry.listing.takenAt) { + entry.listing = listing + } + entry.failing = false + c.entries[key] = entry +} + +// fail records that the latest attempt at key's listing failed, and reports +// whether that starts a failure streak. +func (c *listingCache) fail(key listingKey) (streakStarts bool) { + c.mu.Lock() + defer c.mu.Unlock() + entry := c.entries[key] + streakStarts = !entry.failing + entry.failing = true + c.entries[key] = entry + return streakStarts +} + +// forget drops what is held of a project's listings: its next pass lists anew. +func (c *listingCache) forget(projectID string) { + c.mu.Lock() + defer c.mu.Unlock() + maps.DeleteFunc(c.entries, func(key listingKey, _ listingEntry) bool { return key.projectID == projectID }) +} + +// retain drops the listings of every project but projects. +func (c *listingCache) retain(projects []domain.Project) { + listed := map[string]bool{} + for _, project := range projects { + listed[project.ID] = true + } + c.mu.Lock() + defer c.mu.Unlock() + maps.DeleteFunc(c.entries, func(key listingKey, _ listingEntry) bool { return !listed[key.projectID] }) +} + +// forgetFolders drops what is cached of a project's listings, so a worktree +// deleted is not suggested from a listing taken before, and one kept is +// suggested on the next refresh. +func (r *Reconciler) forgetFolders(projectID string) { + r.cachedListings.forget(projectID) +} + +// forgetProject drops all the reconciler keeps of a removed project. +func (r *Reconciler) forgetProject(projectID string) { + r.cachedListings.forget(projectID) + r.projectsMu.Lock() + defer r.projectsMu.Unlock() + delete(r.projects, projectID) +} + +// resurvey asks for a pass of every project that surveys its folders again, +// as a refresh tick that saw a listing change does; the session providers are +// not asked anew. For changes that claim or free folders without changing a +// listing: a removal that kept the files, a project added or removed. +func (r *Reconciler) resurvey() { + for _, workstreamProvider := range r.registry.WorkstreamProviders() { + if _, ok := workstreamProvider.(FolderSurveyor); ok { + r.markDirty(workstreamProvider.ID()) + } + } +} + +// list has surveyor list the project's folders, and holds the listing. A +// failure is logged as a warning only when it starts a failure streak. +func (r *Reconciler) list(ctx context.Context, project ProjectRef, surveyor FolderSurveyor) (folderListing, error) { + key := listingKey{providerID: surveyor.ID(), projectID: project.ID} + takenAt := time.Now() + folders, err := surveyor.ListFolders(ctx, project) + if err != nil { + log := slog.Debug + if r.cachedListings.fail(key) { + log = slog.Warn + } + log("listing folders failed", "provider", key.providerID, "project", project.ID, "error", err) + return folderListing{}, err + } + listing := folderListing{folders: folders, takenAt: takenAt} + r.cachedListings.put(key, listing) + return listing, nil +} + +// listing returns the listing of the project by surveyor held, or lists it +// when none is. +func (r *Reconciler) listing(ctx context.Context, project ProjectRef, surveyor FolderSurveyor) (folderListing, error) { + if listing, held := r.cachedListings.get(listingKey{providerID: surveyor.ID(), projectID: project.ID}); held { + return listing, nil + } + return r.list(ctx, project, surveyor) +} + +// foldersChanged has surveyor list each git project's folders anew, holding +// the listings, and reports whether one differs from the listing held before, +// or none was held. Workstreams added or removed project themselves; a listing +// that fails changes nothing, as the pass keeps using the one held. +func (r *Reconciler) foldersChanged(ctx context.Context, projects []domain.Project, surveyor FolderSurveyor) bool { + // A listing a pass put for a project removed meanwhile is dropped here. + r.cachedListings.retain(projects) + changed := false + for _, project := range projects { + if !project.GitBacked { + continue + } + previous, held := r.cachedListings.get(listingKey{providerID: surveyor.ID(), projectID: project.ID}) + listing, err := r.list(ctx, projectRefOf(project), surveyor) + if err != nil { + continue + } + changed = changed || !held || !slices.Equal(previous.folders, listing.folders) + } + return changed +} + +// surveyorListing is one FolderSurveyor's listing of a project. +type surveyorListing struct { + surveyor FolderSurveyor + folderListing +} + +// surveyorListings are each FolderSurveyor's listing of one project. +type surveyorListings []surveyorListing + +// lister gets a surveyor's listing of a project: Reconciler.listing the one +// held, Reconciler.list a fresh one. +type lister func(ctx context.Context, project ProjectRef, surveyor FolderSurveyor) (folderListing, error) + +// listings are each FolderSurveyor's listing of a git project, got by list, +// none outside git, or the error of the first that failed. +func (r *Reconciler) listings(ctx context.Context, project ProjectRef, list lister) (surveyorListings, error) { + if !project.GitBacked { + return nil, nil + } + var listings surveyorListings + for _, workstreamProvider := range r.registry.WorkstreamProviders() { + surveyor, ok := workstreamProvider.(FolderSurveyor) + if !ok { + continue + } + listing, err := list(ctx, project, surveyor) + if err != nil { + return nil, err + } + listings = append(listings, surveyorListing{surveyor: surveyor, folderListing: listing}) + } + return listings, nil +} + +// untrackedSuggestion is a folder a surveyor suggests, and that surveyor. +type untrackedSuggestion struct { + surveyor FolderSurveyor + folder domain.UntrackedWorktree +} + +// untracked are the folders the project's surveyors suggest now, none outside +// git. Listed anew, not from the cache: a worktree removed since the last +// refresh is suggested no more. A user waits on it, so the listing gets a +// create's checks' bound rather than a refresh's. +func (r *Reconciler) untracked(ctx context.Context, project domain.Project) ([]untrackedSuggestion, error) { + ref := projectRefOf(project) + ctx, cancel := context.WithTimeout(ctx, createCheckTimeout) + defer cancel() + listings, err := r.listings(ctx, ref, r.list) + if err != nil { + return nil, err + } + claimed := claimedFolders(r.store.Projects()) + var suggestions []untrackedSuggestion + for _, listing := range listings { + for _, folder := range listing.surveyor.Survey(ref, listing.folders, project.Workstreams, claimed).Untracked { + suggestions = append(suggestions, untrackedSuggestion{surveyor: listing.surveyor, folder: folder}) + } + } + return suggestions, nil +} + +// folderSurvey is what a pass makes of a project's listings. +type folderSurvey struct { + // untracked are the folders to suggest, no sessions attached yet. + untracked []domain.UntrackedWorktree + // vanished are the IDs of the workstreams whose folder is gone. + vanished map[string]bool + // stale says a listing missed a workstream first seen after it began. It + // may predate that workstream's worktree, so it vanishes nothing, and is + // to be listed anew. + stale bool +} + +// survey has each surveyor survey its listing against the project's +// workstreams and the folders claimed. A workstream vanishes only from a +// listing begun after firstSeen has it. Why: a listing older than a worktree +// workstream does not name its worktree. +func (listings surveyorListings) survey(project ProjectRef, workstreams []domain.Workstream, claimed []string, + firstSeen map[string]time.Time) folderSurvey { + result := folderSurvey{vanished: map[string]bool{}} + for _, listing := range listings { + survey := listing.surveyor.Survey(project, listing.folders, workstreams, claimed) + result.untracked = append(result.untracked, survey.Untracked...) + for _, workstreamID := range survey.Vanished { + if listing.takenAt.After(firstSeen[workstreamID]) { + result.vanished[workstreamID] = true + } else { + result.stale = true + } + } + } + return result +} + +// unclaimedSuggestions are the stored suggestions, sessions not attached yet, +// but those whose folder something claims since, as a workstream just added +// there does: a session there must not be listed by both. Folders compare as +// the surveyors compare them, symlinks resolved. +func unclaimedSuggestions(stored []domain.UntrackedWorktree, claimed []string) []domain.UntrackedWorktree { + claimedFolders := newFolderSet(claimed) + kept := []domain.UntrackedWorktree{} + for _, folder := range stored { + if !claimedFolders.has(folder.Path) { + kept = append(kept, domain.UntrackedWorktree{Name: folder.Name, Branch: folder.Branch, Path: folder.Path}) + } + } + return kept +} + +// claimedFolders are the folders already held: every project's, and every +// workstream's. Adopted and placeholder workstreams hold none. +func claimedFolders(projects []domain.Project) []string { + var claimed []string + for _, project := range projects { + claimed = append(claimed, project.Path) + for _, workstream := range project.Workstreams { + if workstream.WorkingDir != "" && !isPlaceholder(workstream) && !isAdopted(workstream) { + claimed = append(claimed, workstream.WorkingDir) + } + } + } + return claimed +} + +// hideSessions gives each suggestion in untracked the sessions hidden in its +// folder: those stored hidden there, those of a workstream adopted there +// before it was listed, and those discovered there now. Matched by cleaned +// folder, as workstreams are, so a session counted there is one that attaches +// once the folder is added. It returns the suggested folders, and the +// sessions stored hidden on folders suggested no more, for a workstream +// working there to take. +func (r *Reconciler) hideSessions(untracked []domain.UntrackedWorktree, stored domain.Project, + found discovery) (suggested map[string]bool, unhidden map[string][]domain.Session) { + suggested = map[string]bool{} + for _, folder := range untracked { + suggested[cleanDir(folder.Path)] = true + } + unhidden = map[string][]domain.Session{} + for _, folder := range stored.UntrackedWorktrees { + dir := cleanDir(folder.Path) + unhidden[dir] = append(unhidden[dir], folder.Sessions...) + } + for _, workstream := range stored.Workstreams { + if dir := cleanDir(workstream.WorkingDir); isAdopted(workstream) && suggested[dir] { + unhidden[dir] = append(unhidden[dir], workstream.Sessions...) + } + } + for i := range untracked { + dir := cleanDir(untracked[i].Path) + untracked[i].Sessions = append(r.retainedSessions(unhidden[dir], found), found.sessions[dir]...) + delete(unhidden, dir) + } + return suggested, unhidden +} diff --git a/daemon/internal/provider/reconcile_survey_test.go b/daemon/internal/provider/reconcile_survey_test.go new file mode 100644 index 0000000..759bfd4 --- /dev/null +++ b/daemon/internal/provider/reconcile_survey_test.go @@ -0,0 +1,745 @@ +package provider + +import ( + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "slices" + "testing" + "time" + + "github.com/eclipsesource/homeshell/daemon/internal/domain" + "github.com/eclipsesource/homeshell/daemon/internal/store" +) + +// stubSurveyor is a workstream provider whose listing is set per project. It +// suggests every listed folder not claimed, named after its branch, vanishes +// the workstreams in vanished, and records what it was asked. +type stubSurveyor struct { + stubWorkstreamProvider + folders map[string][]Folder + vanished []string + listErr error + // onList runs inside each ListFolders, as something landing while git lists. + onList func() + lists int + projectList map[string]int + claimed []string + surveyed []domain.Workstream + // listDeadline is the deadline the latest ListFolders was given; zero for none. + listDeadline time.Time +} + +func (s *stubSurveyor) ListFolders(ctx context.Context, project ProjectRef) ([]Folder, error) { + s.lists++ + s.listDeadline, _ = ctx.Deadline() + if s.projectList == nil { + s.projectList = map[string]int{} + } + s.projectList[project.ID]++ + if s.onList != nil { + s.onList() + } + if s.listErr != nil { + return nil, s.listErr + } + return s.folders[project.ID], nil +} + +func (s *stubSurveyor) Survey(_ ProjectRef, folders []Folder, workstreams []domain.Workstream, claimed []string) Survey { + s.claimed, s.surveyed = claimed, workstreams + survey := Survey{Vanished: s.vanished} + for _, folder := range folders { + if !slices.Contains(claimed, folder.Path) { + survey.Untracked = append(survey.Untracked, + domain.UntrackedWorktree{Name: folder.Branch, Branch: folder.Branch, Path: folder.Path}) + } + } + return survey +} + +func (s *stubSurveyor) TrackFolder(domain.UntrackedWorktree, func(string) bool) domain.Workstream { + return domain.Workstream{} +} + +// surveyedReconciler reconciles projects with a surveyor listing /wt/a for p1, +// and a session provider "prov" reporting sessions. +func surveyedReconciler(t *testing.T, projects []domain.Project, + sessions map[string][]DiscoveredSession) (*Reconciler, *store.Store, *stubSurveyor, *stubSessionProvider) { + t.Helper() + st := store.NewTestStore(t, projects) + surveyor := &stubSurveyor{ + stubWorkstreamProvider: stubWorkstreamProvider{id: "core", available: true}, + folders: map[string][]Folder{"p1": {{Path: "/wt/a", Branch: "a"}}}, + } + sessionProvider := &stubSessionProvider{id: "prov", available: true, discovered: sessions} + reg := NewRegistry() + if err := reg.RegisterWorkstreamProvider(surveyor); err != nil { + t.Fatal(err) + } + if err := reg.RegisterSessionProvider(sessionProvider); err != nil { + t.Fatal(err) + } + return NewReconciler(st, reg, 0), st, surveyor, sessionProvider +} + +func surveyedProject(workstreams ...domain.Workstream) domain.Project { + return domain.Project{ID: "p1", Name: "p1", Path: "/p1", GitBacked: true, Workstreams: workstreams} +} + +func suggestionAt(t *testing.T, st *store.Store, path string) domain.UntrackedWorktree { + t.Helper() + for _, suggestion := range st.Projects()[0].UntrackedWorktrees { + if suggestion.Path == path { + return suggestion + } + } + t.Fatalf("no suggestion at %s: %+v", path, st.Projects()[0].UntrackedWorktrees) + return domain.UntrackedWorktree{} +} + +func sessionIDs(sessions []domain.Session) []string { + ids := []string{} + for _, session := range sessions { + ids = append(ids, session.ID) + } + return ids +} + +func TestReconcileSuggestsUntrackedWorktrees(t *testing.T) { + reconciler, st, _, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, nil) + reconciler.Reconcile(context.Background()) + + got := st.Projects()[0].UntrackedWorktrees + if len(got) != 1 || got[0].Name != "a" || got[0].Branch != "a" || got[0].Path != "/wt/a" || + len(got[0].Sessions) != 0 || got[0].SessionCount != 0 { + t.Errorf("suggestions = %+v, want one named a, on branch a, at /wt/a, no sessions", got) + } +} + +func TestReconcileHidesSessionsInAnUntrackedWorktree(t *testing.T) { + reconciler, st, _, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, map[string][]DiscoveredSession{ + "p1": {ds("/wt/a", "s1", domain.SessionRunning)}, + }) + reconciler.Reconcile(context.Background()) + + if workstreams := st.Projects()[0].Workstreams; len(workstreams) != 0 { + t.Errorf("workstreams = %+v, want none adopted", workstreams) + } + suggestion := suggestionAt(t, st, "/wt/a") + if ids := sessionIDs(suggestion.Sessions); !slices.Equal(ids, []string{"s1"}) || suggestion.SessionCount != 1 { + t.Errorf("suggestion sessions = %q, count %d; want [s1], 1", ids, suggestion.SessionCount) + } +} + +func TestReconcileStillAdoptsOtherFolders(t *testing.T) { + reconciler, st, _, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, map[string][]DiscoveredSession{ + "p1": {ds("/elsewhere", "s1", domain.SessionRunning)}, + }) + reconciler.Reconcile(context.Background()) + + workstreams := st.Projects()[0].Workstreams + if len(workstreams) != 1 || workstreams[0].ID != adoptedWorkstreamID("/elsewhere") { + t.Fatalf("workstreams = %+v, want the one adopted for /elsewhere", workstreams) + } + if suggestion := suggestionAt(t, st, "/wt/a"); len(suggestion.Sessions) != 0 { + t.Errorf("suggestion sessions = %+v, want none", suggestion.Sessions) + } +} + +func TestReconcileDropsAnAdoptedWorkstreamInAnUntrackedWorktree(t *testing.T) { + adopted := domain.Workstream{ + ID: adoptedWorkstreamID("/wt/a"), Name: "a", Provider: "prov", WorkingDir: "/wt/a", + Sessions: []domain.Session{{ID: "s1", Provider: "prov", State: domain.SessionRunning}}, + } + reconciler, st, _, sessionProvider := surveyedReconciler(t, []domain.Project{surveyedProject(adopted)}, nil) + // The provider fails to report: the stored session is retained, on the suggestion. + sessionProvider.discoverErr = errors.New("unreachable") + reconciler.Reconcile(context.Background()) + + if workstreams := st.Projects()[0].Workstreams; len(workstreams) != 0 { + t.Errorf("workstreams = %+v, want the adopted one gone", workstreams) + } + if ids := sessionIDs(suggestionAt(t, st, "/wt/a").Sessions); !slices.Equal(ids, []string{"s1"}) { + t.Errorf("suggestion sessions = %q, want [s1]", ids) + } +} + +func TestReconcileCountsOnlyLiveHiddenSessions(t *testing.T) { + reconciler, st, _, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, map[string][]DiscoveredSession{ + "p1": {ds("/wt/a", "s1", domain.SessionRunning), ds("/wt/a", "s2", domain.SessionDone)}, + }) + reconciler.Reconcile(context.Background()) + + if count := suggestionAt(t, st, "/wt/a").SessionCount; count != 1 { + t.Errorf("session count = %d, want 1 (the ended one not counted)", count) + } +} + +func TestReconcileKeepsHiddenSessionsOnAScopedPass(t *testing.T) { + reconciler, st, _, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, map[string][]DiscoveredSession{ + "p1": {ds("/wt/a", "s1", domain.SessionRunning)}, + }) + reconciler.Reconcile(context.Background()) + reconciler.reconcileOnly(context.Background(), dirtySet{ids: map[string]bool{"core": true}}) + + if ids := sessionIDs(suggestionAt(t, st, "/wt/a").Sessions); !slices.Equal(ids, []string{"s1"}) { + t.Errorf("suggestion sessions after a scoped pass = %q, want [s1]", ids) + } +} + +// A failed listing is no empty listing: the suggestions stay, with the +// sessions hidden there, but one whose folder a workstream took since, as +// the surveyors compare folders, symlinks resolved. +func TestReconcileOnAFailedListing(t *testing.T) { + realDir := t.TempDir() + linkDir := filepath.Join(t.TempDir(), "link") + if err := os.Symlink(realDir, linkDir); err != nil { + t.Fatal(err) + } + tests := []struct { + name string + // folder is the one listed, then suggested. + folder string + // claimedAs is the folder a workstream added since works in; "" when none. + claimedAs string + wantSuggested bool + wantWorkstreams int + }{ + {name: "keeps the suggestion", folder: "/wt/a", wantSuggested: true}, + {name: "drops one claimed since", folder: "/wt/a", claimedAs: "/wt/a", wantWorkstreams: 1}, + {name: "drops one claimed through a symlink", folder: linkDir, claimedAs: realDir, wantWorkstreams: 1}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reconciler, st, surveyor, sessionProvider := surveyedReconciler(t, []domain.Project{surveyedProject()}, + map[string][]DiscoveredSession{"p1": {ds(tt.folder, "s1", domain.SessionRunning)}}) + surveyor.folders["p1"] = []Folder{{Path: tt.folder, Branch: "a"}} + reconciler.Reconcile(context.Background()) + if tt.claimedAs != "" { + if err := st.AddWorkstream("p1", worktreeAt("ws-a", tt.claimedAs)); err != nil { + t.Fatal(err) + } + } + surveyor.listErr = errors.New("git timed out") + // Not reported again either: s1 is the stored one, retained. + sessionProvider.discoverErr = errors.New("unreachable") + reconciler.forgetFolders("p1") + reconciler.Reconcile(context.Background()) + + if surveyor.lists != 2 { + t.Errorf("listings = %d, want 2 (the cache forgotten)", surveyor.lists) + } + suggestions := st.Projects()[0].UntrackedWorktrees + if tt.wantSuggested { + if ids := sessionIDs(suggestionAt(t, st, tt.folder).Sessions); !slices.Equal(ids, []string{"s1"}) { + t.Errorf("suggestion sessions = %q, want [s1] kept", ids) + } + } else if len(suggestions) != 0 { + t.Errorf("suggestions = %+v, want none for a folder a workstream works in", suggestions) + } + workstreams := st.Projects()[0].Workstreams + if len(workstreams) != tt.wantWorkstreams { + t.Fatalf("workstreams = %+v, want %d, none adopted", workstreams, tt.wantWorkstreams) + } + if tt.wantWorkstreams == 1 && tt.claimedAs == tt.folder && + !slices.Equal(sessionIDs(workstreams[0].Sessions), []string{"s1"}) { + t.Errorf("workstreams = %+v, want ws-a holding s1", workstreams) + } + }) + } +} + +func TestReconcileExcludesFoldersOfOtherProjects(t *testing.T) { + projectA := surveyedProject(domain.Workstream{ + ID: adoptedWorkstreamID("/wt/x"), Name: "x", Provider: "prov", WorkingDir: "/wt/x", Sessions: []domain.Session{}, + }) + projectB := domain.Project{ID: "p2", Name: "p2", Path: "/p2", GitBacked: true, Workstreams: []domain.Workstream{ + {ID: "ws-b", Name: "b", Provider: "core", WorkingDir: "/wt/a", Sessions: []domain.Session{}}, + {ID: PlaceholderIDPrefix + "1", Name: "c", Provider: "core", WorkingDir: "/wt/c", Sessions: []domain.Session{}}, + }} + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{projectA, projectB}, nil) + if err := reconciler.ReconcileProject(context.Background(), "p1"); err != nil { + t.Fatal(err) + } + + if got := st.Projects()[0].UntrackedWorktrees; len(got) != 0 { + t.Errorf("suggestions = %+v, want none (/wt/a is project B's)", got) + } + for _, want := range []string{"/p1", "/p2", "/wt/a"} { + if !slices.Contains(surveyor.claimed, want) { + t.Errorf("claimed = %q, want it to hold %s", surveyor.claimed, want) + } + } + for _, unwanted := range []string{"/wt/x", "/wt/c"} { + if slices.Contains(surveyor.claimed, unwanted) { + t.Errorf("claimed = %q, want no adopted or placeholder folder %s", surveyor.claimed, unwanted) + } + } +} + +func TestReconcileSkipsSurveyOutsideGit(t *testing.T) { + project := surveyedProject() + project.GitBacked = false + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{project}, nil) + reconciler.Reconcile(context.Background()) + + if surveyor.lists != 0 { + t.Errorf("listings = %d, want none outside git", surveyor.lists) + } + if got := st.Projects()[0].UntrackedWorktrees; len(got) != 0 { + t.Errorf("suggestions = %+v, want none", got) + } +} + +// A workstream that took a suggested folder takes the sessions hidden there, +// even those whose provider is not asked this pass. +func TestReconcileHandsHiddenSessionsToAWorkstreamAddedInTheFolder(t *testing.T) { + reconciler, st, _, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, map[string][]DiscoveredSession{ + "p1": {ds("/wt/a", "s1", domain.SessionRunning)}, + }) + reconciler.Reconcile(context.Background()) + if err := st.AddWorkstream("p1", domain.Workstream{ + ID: "ws-a", Name: "a", Provider: "core", WorkingDir: filepath.Join("/wt", "a"), Sessions: []domain.Session{}, + }); err != nil { + t.Fatal(err) + } + reconciler.reconcileOnly(context.Background(), dirtySet{ids: map[string]bool{"core": true}}) + + if got := st.Projects()[0].UntrackedWorktrees; len(got) != 0 { + t.Errorf("suggestions = %+v, want none once added", got) + } + workstreams := st.Projects()[0].Workstreams + if len(workstreams) != 1 || !slices.Equal(sessionIDs(workstreams[0].Sessions), []string{"s1"}) { + t.Errorf("workstreams = %+v, want ws-a holding s1", workstreams) + } +} + +// worktreeAt is a workstream of the surveyor's working in dir. +func worktreeAt(id, dir string) domain.Workstream { + return domain.Workstream{ID: id, Name: id, Provider: "core", WorkingDir: dir, Sessions: []domain.Session{}} +} + +// retirements records the workstreams a reconciler retires. +func retirements(reconciler *Reconciler) *[]string { + var retired []string + reconciler.retire = func(_ context.Context, ref WorkstreamRef) { + retired = append(retired, ref.WorkstreamID) + } + return &retired +} + +// A vanished workstream is retired, and stays listed with the sessions in its +// folder until its removal drops it: no pass adopts them meanwhile. +func TestReconcileRetiresAVanishedWorktreeWorkstream(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject(worktreeAt("ws-1", "/wt/gone"))}, + map[string][]DiscoveredSession{"p1": {ds("/wt/gone", "s1", domain.SessionRunning)}}) + surveyor.vanished = []string{"ws-1"} + retired := retirements(reconciler) + // The first pass lists before it first sees ws-1; the second lists anew. + reconciler.Reconcile(context.Background()) + reconciler.Reconcile(context.Background()) + reconciler.Reconcile(context.Background()) + + if !slices.Equal(*retired, []string{"ws-1", "ws-1"}) { + t.Errorf("retired = %q, want ws-1 by each pass from a listing newer than it", *retired) + } + workstreams := st.Projects()[0].Workstreams + if len(workstreams) != 1 || workstreams[0].ID != "ws-1" || + !slices.Equal(sessionIDs(workstreams[0].Sessions), []string{"s1"}) { + t.Errorf("workstreams = %+v, want ws-1 holding s1 until removed, none adopted", workstreams) + } +} + +// A listing may predate a workstream's worktree: the workstream vanishes only +// from one begun after a pass first saw it. +func TestReconcileVanishesNothingFromAListingOlderThanTheWorkstream(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, nil) + retired := retirements(reconciler) + reconciler.Reconcile(context.Background()) + if err := st.AddWorkstream("p1", worktreeAt("ws-new", "/wt/new")); err != nil { + t.Fatal(err) + } + surveyor.vanished = []string{"ws-new"} + + reconciler.Reconcile(context.Background()) + if len(*retired) != 0 || surveyor.lists != 1 { + t.Fatalf("retired = %q after %d listings, want none from the listing cached before ws-new", + *retired, surveyor.lists) + } + reconciler.Reconcile(context.Background()) + if !slices.Equal(*retired, []string{"ws-new"}) || surveyor.lists != 2 { + t.Errorf("retired = %q after %d listings, want ws-new from the listing taken anew", + *retired, surveyor.lists) + } +} + +func TestReconcileVanishesNothingWhenTheListingFails(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject(worktreeAt("ws-1", "/wt/gone"))}, nil) + surveyor.vanished = []string{"ws-1"} + surveyor.listErr = errors.New("git timed out") + retired := retirements(reconciler) + reconciler.Reconcile(context.Background()) + reconciler.Reconcile(context.Background()) + + if len(*retired) != 0 { + t.Errorf("retired = %q, want none on a failed listing", *retired) + } + if workstreams := st.Projects()[0].Workstreams; len(workstreams) != 1 || workstreams[0].ID != "ws-1" { + t.Errorf("workstreams = %+v, want ws-1 kept", workstreams) + } +} + +// A workstream landing while git lists, as one promoted then does, is surveyed: +// the listing is no older than the workstreams it is held against. +func TestReconcileSurveysAfterReReadingTheStore(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, nil) + surveyor.onList = func() { + if err := st.AddWorkstream("p1", worktreeAt("ws-new", "/wt/new")); err != nil { + t.Fatal(err) + } + } + reconciler.Reconcile(context.Background()) + + if !slices.ContainsFunc(surveyor.surveyed, func(workstream domain.Workstream) bool { return workstream.ID == "ws-new" }) { + t.Errorf("surveyed workstreams = %+v, want ws-new, added while listing", surveyor.surveyed) + } +} + +func TestRefreshListsWorktreesOncePerProject(t *testing.T) { + projectB := domain.Project{ID: "p2", Name: "p2", Path: "/p2", GitBacked: true, Workstreams: []domain.Workstream{}} + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject(), projectB}, nil) + surveyor.folders["p2"] = []Folder{{Path: "/wt/b", Branch: "b"}} + + if !reconciler.foldersChanged(context.Background(), st.Projects(), surveyor) { + t.Fatal("foldersChanged = false, want true for listings not held yet") + } + reconciler.reconcileOnly(context.Background(), dirtySet{ids: map[string]bool{"core": true}}) + + if surveyor.projectList["p1"] != 1 || surveyor.projectList["p2"] != 1 { + t.Errorf("listings per project = %v, want one each", surveyor.projectList) + } +} + +// A refresh reports a change when a listing differs from the one held, or +// none was held; not when it is the same, or fails. +func TestRefreshReportsAChangedListing(t *testing.T) { + tests := []struct { + name string + change func(surveyor *stubSurveyor, reconciler *Reconciler) + changed bool + }{ + {name: "same listing", change: func(*stubSurveyor, *Reconciler) {}}, + {name: "folder gone", changed: true, change: func(surveyor *stubSurveyor, _ *Reconciler) { + surveyor.folders["p1"] = nil + }}, + {name: "branch moved", changed: true, change: func(surveyor *stubSurveyor, _ *Reconciler) { + surveyor.folders["p1"] = []Folder{{Path: "/wt/a", Branch: "b"}} + }}, + {name: "none held", changed: true, change: func(_ *stubSurveyor, reconciler *Reconciler) { + reconciler.forgetFolders("p1") + }}, + {name: "listing fails", change: func(surveyor *stubSurveyor, _ *Reconciler) { + surveyor.listErr = errors.New("git timed out") + }}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, nil) + reconciler.Reconcile(context.Background()) + tt.change(surveyor, reconciler) + if got := reconciler.foldersChanged(context.Background(), st.Projects(), surveyor); got != tt.changed { + t.Errorf("foldersChanged = %v, want %v", got, tt.changed) + } + }) + } +} + +// A refresh whose listing fails keeps the cached one. +func TestRefreshKeepsTheCachedListingWhenListingFails(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, nil) + reconciler.Reconcile(context.Background()) + surveyor.listErr = errors.New("git timed out") + reconciler.foldersChanged(context.Background(), st.Projects(), surveyor) + surveyor.listErr = nil + reconciler.Reconcile(context.Background()) + + if surveyor.lists != 2 { + t.Errorf("listings = %d, want 2 (the pass after the failed refresh served from the cache)", surveyor.lists) + } + if got := st.Projects()[0].UntrackedWorktrees; len(got) != 1 { + t.Errorf("suggestions = %+v, want the cached one kept", got) + } +} + +func TestReconcilerRunPicksUpAWorktreeAddedOutsideHomeShell(t *testing.T) { + repo := gitProject(t) + st := store.NewTestStore(t, []domain.Project{{ID: "p1", Path: repo, GitBacked: true, Workstreams: []domain.Workstream{}}}) + reg := NewRegistry() + if err := reg.RegisterWorkstreamProvider(&core{root: t.TempDir()}); err != nil { + t.Fatal(err) + } + reconciler := NewReconciler(st, reg, 0) + reconciler.refreshEvery = 20 * time.Millisecond + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go reconciler.Run(ctx) + + dir := addWorktree(t, repo, "wt-a") + awaitSuggestions(t, st, func(got []domain.UntrackedWorktree) bool { + return len(got) == 1 && got[0].Name == "wt-a" && got[0].Path == dir + }, "the worktree wt-a suggested") + + if out, err := exec.Command("git", "-C", repo, "worktree", "remove", dir).CombinedOutput(); err != nil { + t.Fatalf("git worktree remove: %v\n%s", err, out) + } + awaitSuggestions(t, st, func(got []domain.UntrackedWorktree) bool { return len(got) == 0 }, "no suggestion") +} + +// awaitSuggestions waits for the suggestions of the store's first project to +// satisfy ok. +func awaitSuggestions(t *testing.T, st *store.Store, ok func([]domain.UntrackedWorktree) bool, want string) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for time.Now().Before(deadline) { + if ok(st.Projects()[0].UntrackedWorktrees) { + return + } + time.Sleep(10 * time.Millisecond) + } + t.Fatalf("suggestions = %+v, want %s", st.Projects()[0].UntrackedWorktrees, want) +} + +// A vanished workstream is removed with its sessions, unless a removal of it +// is under way already, or it is gone. +func TestServiceRetiresAVanishedWorkstreamOnce(t *testing.T) { + workstream := worktreeAt("ws-1", "/wt/gone") + workstream.Sessions = []domain.Session{{ID: "s1", Provider: "prov", State: domain.SessionRunning}} + ref := WorkstreamRef{ProjectID: "p1", WorkstreamID: "ws-1"} + tests := []struct { + name string + stored []domain.Workstream + removing bool + wantRemoved []string + wantLeft int + }{ + {name: "alone", stored: []domain.Workstream{workstream}, wantRemoved: []string{"s1"}}, + {name: "while removed", stored: []domain.Workstream{workstream}, removing: true, wantLeft: 1}, + {name: "gone already"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sessionProvider := &stubSessionProvider{id: "prov", available: true} + reg := NewRegistry() + if err := reg.RegisterSessionProvider(sessionProvider); err != nil { + t.Fatal(err) + } + st := store.NewTestStore(t, []domain.Project{surveyedProject(tt.stored...)}) + svc := NewService(st, reg, 0) + if tt.removing { + svc.beginWorkstreamRemoval(ref) + defer svc.endWorkstreamRemoval(ref) + } + svc.retireVanished(context.Background(), ref) + svc.background.Wait() + + if !slices.Equal(sessionProvider.removed, tt.wantRemoved) { + t.Errorf("removed sessions = %q, want %q", sessionProvider.removed, tt.wantRemoved) + } + if left := st.Projects()[0].Workstreams; len(left) != tt.wantLeft { + t.Errorf("workstreams = %+v, want %d", left, tt.wantLeft) + } + }) + } +} + +// Removing a workstream whose record is gone fails as not found, as Remove's +// callers are told. +func TestRemoveWorkstreamOfAGoneRecordIsNotFound(t *testing.T) { + svc := NewService(store.NewTestStore(t, []domain.Project{surveyedProject()}), NewRegistry(), 0) + ref := WorkstreamRef{ProjectID: "p1", WorkstreamID: "ws-1"} + err := svc.removeWorkstream(context.Background(), ref, worktreeAt("ws-1", "/wt/gone"), RemoveOptions{Force: true}) + if !errors.Is(err, ErrWorkstreamNotFound) { + t.Errorf("removeWorkstream = %v, want ErrWorkstreamNotFound", err) + } +} + +// passOnTrigger runs the pass the reconciler was asked for, as Run would, and +// fails when none was asked for. +func passOnTrigger(t *testing.T, reconciler *Reconciler) { + t.Helper() + select { + case <-reconciler.trigger: + case <-time.After(time.Second): + t.Fatal("no pass was asked for") + } + reconciler.reconcileOnly(context.Background(), reconciler.takeDirty()) +} + +// drainTrigger drops a pass asked for so far. +func drainTrigger(reconciler *Reconciler) { + select { + case <-reconciler.trigger: + default: + } + reconciler.takeDirty() +} + +// gitService is a service over a git project p1 with one linked worktree, +// whose path it returns. +func gitService(t *testing.T, workstreams ...domain.Workstream) (*Service, *store.Store, string) { + t.Helper() + repo := gitProject(t) + dir := addWorktree(t, repo, "wt-a") + st := store.NewTestStore(t, []domain.Project{{ID: "p1", Name: "p1", Path: repo, GitBacked: true, + Workstreams: workstreams}}) + return NewService(st, NewRegistry(), 0), st, dir +} + +// A worktree whose workstream is removed with its files kept is suggested +// again, though a refresh tick held a listing from before the removal: the +// listing is the same, but the removal asks for a pass itself. +func TestRemoveKeepingFilesSuggestsTheWorktreeAgain(t *testing.T) { + svc, st, dir := gitService(t) + svc.Reconcile(context.Background()) + folder := suggestionAt(t, st, dir) + workstream := (&core{}).TrackFolder(folder, nil) + workstream.ID, workstream.Provider = "ws-a", CoreProviderID + if err := st.AddWorkstream("p1", workstream); err != nil { + t.Fatal(err) + } + svc.Reconcile(context.Background()) + if got := st.Projects()[0].UntrackedWorktrees; len(got) != 0 { + t.Fatalf("suggestions = %+v, want none while ws-a works there", got) + } + key := listingKey{providerID: CoreProviderID, projectID: "p1"} + tickListing, _ := svc.reconciler.cachedListings.get(key) + drainTrigger(svc.reconciler) + + ref := WorkstreamRef{ProjectID: "p1", WorkstreamID: "ws-a"} + if err := svc.Remove(context.Background(), ref, RemoveOptions{KeepFiles: true}); err != nil { + t.Fatal(err) + } + // The tick listed before the removal and holds its listing after it. + svc.reconciler.cachedListings.put(key, folderListing{folders: tickListing.folders, takenAt: time.Now()}) + passOnTrigger(t, svc.reconciler) + + suggestionAt(t, st, folder.Path) +} + +// A project added in a linked worktree of another's repository claims that +// folder, and frees it once removed: the other project is re-projected both +// times, though its listing stays the same. +func TestAddingAndRemovingAProjectReprojectsTheOthers(t *testing.T) { + svc, st, dir := gitService(t) + svc.Reconcile(context.Background()) + suggestionAt(t, st, dir) + drainTrigger(svc.reconciler) + + if err := svc.AddProject(context.Background(), &domain.Project{ID: "p2", Name: "p2", Path: dir}); err != nil { + t.Fatal(err) + } + passOnTrigger(t, svc.reconciler) + if got := st.Projects()[0].UntrackedWorktrees; len(got) != 0 { + t.Fatalf("suggestions = %+v, want none once a project is at %s", got, dir) + } + + if err := svc.RemoveProject(context.Background(), "p2"); err != nil { + t.Fatal(err) + } + passOnTrigger(t, svc.reconciler) + suggestionAt(t, st, dir) +} + +// A Remove arriving while a retire tears the workstream down waits for it, +// and tears down nothing again; it does the removal itself only when the +// retire left the workstream behind. +func TestRemoveWaitsForARetireUnderWay(t *testing.T) { + tests := []struct { + name string + retireDrops bool + wantRemoved []string + }{ + {name: "retire done", retireDrops: true}, + {name: "retire failed", wantRemoved: []string{"s1"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sessionProvider := &stubSessionProvider{id: "prov", available: true} + reg := NewRegistry() + if err := reg.RegisterSessionProvider(sessionProvider); err != nil { + t.Fatal(err) + } + workstream := worktreeAt("ws-1", "/wt/gone") + workstream.Sessions = []domain.Session{{ID: "s1", Provider: "prov", State: domain.SessionDone}} + st := store.NewTestStore(t, []domain.Project{surveyedProject(workstream)}) + svc := NewService(st, reg, 0) + ref := WorkstreamRef{ProjectID: "p1", WorkstreamID: "ws-1"} + if !svc.tryBeginRetire(ref) { + t.Fatal("the retire was refused") + } + + removed := make(chan error, 1) + go func() { removed <- svc.Remove(context.Background(), ref, RemoveOptions{}) }() + select { + case err := <-removed: + t.Fatalf("Remove returned %v during the retire, want it to wait", err) + case <-time.After(50 * time.Millisecond): + } + if tt.retireDrops { + if err := st.RemoveWorkstream("p1", "ws-1"); err != nil { + t.Fatal(err) + } + } + svc.endWorkstreamRemoval(ref) + + if err := <-removed; err != nil { + t.Errorf("Remove = %v, want success", err) + } + if !slices.Equal(sessionProvider.removed, tt.wantRemoved) { + t.Errorf("removed sessions = %q, want %q", sessionProvider.removed, tt.wantRemoved) + } + if left := st.Projects()[0].Workstreams; len(left) != 0 { + t.Errorf("workstreams = %+v, want none", left) + } + }) + } +} + +// Nothing is kept of a removed project: not by its removal, nor from a pass +// in flight that listed it meanwhile. +func TestReconcilerKeepsNothingOfARemovedProject(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, nil) + reconciler.Reconcile(context.Background()) + reconciler.forgetProject("p1") + gone := listingKey{providerID: "core", projectID: "gone"} + reconciler.cachedListings.put(gone, folderListing{takenAt: time.Now()}) + reconciler.foldersChanged(context.Background(), st.Projects(), surveyor) + + if _, kept := reconciler.projects["p1"]; kept { + t.Error("the pass state of p1 is kept after its removal") + } + if _, held := reconciler.cachedListings.get(gone); held { + t.Error("a listing of a project gone is held after a refresh") + } +} + +// A pass leaves the listing's bound to the surveyor, which bounds a refresh's +// tightly. The user's Add waits on its listing, which gets a create's bound. +func TestUntrackedListsUnderACreatesBound(t *testing.T) { + reconciler, st, surveyor, _ := surveyedReconciler(t, []domain.Project{surveyedProject()}, nil) + reconciler.Reconcile(context.Background()) + if !surveyor.listDeadline.IsZero() { + t.Errorf("a pass listed with deadline %v, want none: the surveyor bounds it", surveyor.listDeadline) + } + + start := time.Now() + if _, err := reconciler.untracked(context.Background(), st.Projects()[0]); err != nil { + t.Fatal(err) + } + if got := surveyor.listDeadline.Sub(start); (got - createCheckTimeout).Abs() > time.Second { + t.Errorf("an Add listed within %v, want %v", got, createCheckTimeout) + } +} diff --git a/daemon/internal/provider/reconcile_test.go b/daemon/internal/provider/reconcile_test.go index 14b7bf4..d38a476 100644 --- a/daemon/internal/provider/reconcile_test.go +++ b/daemon/internal/provider/reconcile_test.go @@ -54,7 +54,7 @@ func TestReconcileDeduplicatesStoredAndDiscoveredSessionByID(t *testing.T) { // does not store it, so it loads as "". When the owning provider rediscovers // the same session by ID, the reconciler must replace the stored copy with the // live one, not keep both. Otherwise the session appears twice after a daemon - // restart (and ReplaceWorkstreams hits a UNIQUE constraint on sessions.id). + // restart (and ReplaceProjection hits a UNIQUE constraint on sessions.id). st := store.NewTestStore(t, []domain.Project{{ID: "p1", Path: "/p1", Workstreams: []domain.Workstream{ {ID: "ws1", Provider: "core", WorkingDir: "/p1/w", Sessions: []domain.Session{ // Provider intentionally omitted (== ""), reproducing a store-loaded session. diff --git a/daemon/internal/provider/restart.go b/daemon/internal/provider/restart.go index d22a9b9..5042dd6 100644 --- a/daemon/internal/provider/restart.go +++ b/daemon/internal/provider/restart.go @@ -604,7 +604,9 @@ func (s *Service) beginProjectRemoval(projectID string) { // it was started in: a container listed under two projects sharing a directory // keeps the other project's restart, and one started in this project is left to // finish, as the other project still lists its session; it then shows in the -// project the store moved the session's records to. +// project the store moved the session's records to. One the other project only +// hides on an untracked worktree is cancelled: no workstream there could show +// how it went. // // Under s.mu, store call included, so a restart is either cancelled before it // runs or caught running (markRestartRunning), and records its launch under a @@ -621,7 +623,7 @@ func (s *Service) removeProjectFromStore(projectID string) ([]restartInFlight, e if restart.ref.ProjectID != projectID { continue } - if home, ok := rehomed[sessionID]; ok { + if home, ok := rehomed[sessionID]; ok && !home.Hidden() { // Its slot moves with it, for the workstream it now shows in to count it. if restart.reservedSlot { s.releaseSlotLocked(concurrencyKey(restart.ref.ProjectID, restart.ref.WorkstreamID)) diff --git a/daemon/internal/provider/service.go b/daemon/internal/provider/service.go index d73cf31..df8fb64 100644 --- a/daemon/internal/provider/service.go +++ b/daemon/internal/provider/service.go @@ -113,7 +113,9 @@ type Service struct { // // removing counts the removals in progress per session ID, during which a // restart is refused (beginRemoval); removingProjects does the same per project - // ID for its session creates and restarts (beginProjectRemoval). + // ID for its session creates and restarts (beginProjectRemoval); + // removingWorkstreams per workstream (concurrencyKey), during which a vanished + // one is not retired (tryBeginRetire), and a Remove waits for a retire. // // mu is held across a store call in places (removeProjectFromStore); the // store never calls back into the service, so the order cannot invert. @@ -123,6 +125,8 @@ type Service struct { restarting map[string]restartInFlight removing map[string]int removingProjects map[string]int + // removingWorkstreams: see removing above. + removingWorkstreams map[string]*workstreamRemoval // createMu serializes the synchronous half of workstream creates. createMu sync.Mutex @@ -146,6 +150,9 @@ var ( ErrProjectNotFound = errors.New("project not found") ErrWorkstreamNotFound = errors.New("workstream not found") ErrSessionNotFound = errors.New("session not found") + // ErrNotUntrackedWorktree refuses to add a folder that is none of the + // project's untracked worktrees now. + ErrNotUntrackedWorktree = errors.New("not an untracked worktree of this project") // ErrConcurrencyLimit is returned when creating or restarting a session would // exceed the provider's MaxConcurrentSessions for the workstream. ErrConcurrencyLimit = errors.New("concurrent session limit reached") @@ -278,17 +285,18 @@ func NewService(st *store.Store, reg *Registry, poll time.Duration, opts ...Serv _ = reg.RegisterWorkstreamProvider(c) } svc := &Service{ - registry: reg, - reconciler: NewReconciler(st, reg, poll), - store: st, - core: c, - commands: NewOSCommandRunner(), - opener: opener.NewOSOpener(), - inflight: map[string]int{}, - creating: map[string]createInFlight{}, - restarting: map[string]restartInFlight{}, - removing: map[string]int{}, - removingProjects: map[string]int{}, + registry: reg, + reconciler: NewReconciler(st, reg, poll), + store: st, + core: c, + commands: NewOSCommandRunner(), + opener: opener.NewOSOpener(), + inflight: map[string]int{}, + creating: map[string]createInFlight{}, + restarting: map[string]restartInFlight{}, + removing: map[string]int{}, + removingProjects: map[string]int{}, + removingWorkstreams: map[string]*workstreamRemoval{}, } svc.lifetime, svc.cancelLifetime = context.WithCancel(context.Background()) svc.term = terminal.NewOSLauncher(svc.settings.TerminalPreference) @@ -298,6 +306,7 @@ func NewService(st *store.Store, reg *Registry, poll time.Duration, opts ...Serv // The reconciler projects the derived workstream fields; only the service // knows how to derive them (it holds the settings and the host). svc.reconciler.annotate = svc.annotate + svc.reconciler.retire = svc.retireVanished return svc } @@ -467,6 +476,9 @@ func (s *Service) AddProject(ctx context.Context, project *domain.Project) error if err := s.store.AddProject(project); err != nil { return err } + // Its folder is claimed now: a project sharing its repository suggests it + // no more. Its own pass runs below. + defer s.reconciler.resurvey() defaultWorkstream, err := s.AddDefaultWorkstream(ctx, project.ID) if err != nil { slog.Warn("adding the default workstream failed", "project", project.ID, "error", err) @@ -533,6 +545,67 @@ func (s *Service) addDefaultWorkstream(ctx context.Context, projectID string, pr return created, nil } +// AddUntrackedWorktree makes a workstream of the project's untracked worktree +// at path, creating nothing on disk: named like its suggestion, numbered when +// that name is taken. The sessions hidden there attach to it. A folder that is +// not suggested now, checked against a fresh listing, is refused with +// ErrNotUntrackedWorktree; outside git nothing is suggested. +func (s *Service) AddUntrackedWorktree(ctx context.Context, projectID, path string) (domain.Workstream, error) { + added, err := s.addUntrackedWorktree(ctx, projectID, path) + if err != nil { + return domain.Workstream{}, err + } + return s.reconciledWorkstream(ctx, projectID, added.ID) +} + +// addUntrackedWorktree stores a workstream for the folder at path the +// project's surveyors suggest now. Under createMu, so a create cannot claim +// the folder in between. +func (s *Service) addUntrackedWorktree(ctx context.Context, projectID, path string) (domain.Workstream, error) { + s.createMu.Lock() + defer s.createMu.Unlock() + project, err := s.store.Project(projectID) + if err != nil { + return domain.Workstream{}, ErrProjectNotFound + } + suggestions, err := s.reconciler.untracked(ctx, project) + if err != nil { + return domain.Workstream{}, err + } + index := slices.IndexFunc(suggestions, func(suggestion untrackedSuggestion) bool { + return domain.SameFolder(suggestion.folder.Path, path) + }) + if index < 0 { + return domain.Workstream{}, ErrNotUntrackedWorktree + } + suggestion := suggestions[index] + nameTaken := func(name string) bool { return s.store.NameUser(projectID, name) != "" } + added, err := stampCreated(suggestion.surveyor.TrackFolder(suggestion.folder, nameTaken), suggestion.surveyor.ID()) + if err != nil { + return domain.Workstream{}, err + } + if err := s.store.AddWorkstream(projectID, added); err != nil { + return domain.Workstream{}, err + } + return added, nil +} + +// reconciledWorkstream reconciles the project a workstream was just added to +// and returns the workstream as stored. A pass persists it and attaches the +// sessions already running in its folder; the workstream exists either way, +// so a failed pass is logged and left to the next. +func (s *Service) reconciledWorkstream(ctx context.Context, projectID, workstreamID string) (domain.Workstream, error) { + if err := s.reconciler.ReconcileProject(ctx, projectID); err != nil { + slog.Warn("reconciling after adding a workstream failed", "project", projectID, "error", err) + } + workstream, err := s.store.Workstream(projectID, workstreamID) + if err != nil { + // Removed meanwhile, or its project was. + return domain.Workstream{}, fmt.Errorf("%w: %v", ErrWorkstreamNotFound, err) + } + return workstream, nil +} + // stampCreated gives a workstream a provider created what the daemon assigns: // its ID and its provider. func stampCreated(created domain.Workstream, providerID string) (domain.Workstream, error) { @@ -601,8 +674,12 @@ func (s *Service) createRequest(input CreateInput, project ProjectRef) CreateReq // finishCreate runs the workstream provider's creation in the background. On // success the placeholder is replaced by the real workstream; on failure it -// transitions to error state so the user sees what went wrong. +// transitions to error state so the user sees what went wrong. The provider's +// claim on the folder is held until then: a pass in between must not suggest +// the worktree being created, nor an Add take it. func (s *Service) finishCreate(ctx context.Context, in CreateInput, ref ProjectRef, wp WorkstreamProvider, placeholderID string) { + releaseClaim := func() {} + defer func() { releaseClaim() }() created, err := wp.Create(ctx, CreateRequest{ Project: ref, Name: in.Name, @@ -613,6 +690,7 @@ func (s *Service) finishCreate(ctx context.Context, in CreateInput, ref ProjectR OnProgress: func(msg string) { _ = s.store.SetStatusMessage(in.ProjectID, placeholderID, msg) }, + HoldClaim: func(release func()) { releaseClaim = release }, }) if err != nil { slog.Warn("workstream creation failed", @@ -1292,6 +1370,23 @@ func (s *Service) Remove(ctx context.Context, ref WorkstreamRef, opts RemoveOpti return nil } + // Marked before the workstream is read: a retire starting once it is read + // leaves it to this removal. One under way already is waited for instead. + for { + retire := s.beginWorkstreamRemoval(ref) + if retire == nil { + break + } + select { + case <-retire: + case <-ctx.Done(): + return ctx.Err() + } + if _, err := s.store.Workstream(ref.ProjectID, ref.WorkstreamID); err != nil { + return nil // the retire removed it + } + } + defer s.endWorkstreamRemoval(ref) ws, err := s.store.Workstream(ref.ProjectID, ref.WorkstreamID) if err != nil { return ErrWorkstreamNotFound @@ -1307,6 +1402,98 @@ func (s *Service) Remove(ctx context.Context, ref WorkstreamRef, opts RemoveOpti } } + return s.removeWorkstream(ctx, ref, ws, opts) +} + +// retireVanished starts removing, in the background, a workstream whose +// worktree a reconcile pass found gone: its sessions stopped and torn down, +// forced, files kept as there are none. Passes keep it listed until its record +// goes, as they do one a Remove tears down. One a removal is under way for +// already, a Remove's or an earlier retire's, is left to it; a Remove arriving +// meanwhile waits for it. Why: a provider +// would otherwise keep reporting its sessions and they would be adopted back. +func (s *Service) retireVanished(ctx context.Context, ref WorkstreamRef) { + if !s.tryBeginRetire(ref) { + return + } + // Not on ctx, which may be a request's: the teardown must outlive it. + detached := context.WithoutCancel(ctx) + s.goBackground(func() { + defer s.endWorkstreamRemoval(ref) + workstream, err := s.store.Workstream(ref.ProjectID, ref.WorkstreamID) + if err != nil { + return // removed since the pass saw it + } + slog.Info("removing a workstream whose worktree is gone", + "project", ref.ProjectID, "workstream", ref.WorkstreamID, "dir", workstream.WorkingDir) + err = s.removeWorkstream(detached, ref, workstream, RemoveOptions{Force: true, KeepFiles: true}) + if err != nil && !errors.Is(err, ErrWorkstreamNotFound) { + slog.Warn("removing a vanished workstream failed", + "project", ref.ProjectID, "workstream", ref.WorkstreamID, "error", err) + } + }) +} + +// workstreamRemoval is a removal of a workstream under way. +type workstreamRemoval struct { + // removals counts the Removes tearing it down. + removals int + // retired is a retire's, closed once it is done; nil for Removes. A retire + // holds the workstream alone. + retired chan struct{} +} + +// beginWorkstreamRemoval marks a workstream as being torn down by a Remove +// until endWorkstreamRemoval. While a retire holds it, nothing is marked and +// the retire's completion is returned instead, for the Remove to wait for. +func (s *Service) beginWorkstreamRemoval(ref WorkstreamRef) (retire <-chan struct{}) { + key := concurrencyKey(ref.ProjectID, ref.WorkstreamID) + s.mu.Lock() + defer s.mu.Unlock() + removal, ok := s.removingWorkstreams[key] + if !ok { + removal = &workstreamRemoval{} + s.removingWorkstreams[key] = removal + } + if removal.retired != nil { + return removal.retired + } + removal.removals++ + return nil +} + +// tryBeginRetire marks a workstream as being retired until +// endWorkstreamRemoval, refused (false) while a removal of it is under way. +func (s *Service) tryBeginRetire(ref WorkstreamRef) bool { + key := concurrencyKey(ref.ProjectID, ref.WorkstreamID) + s.mu.Lock() + defer s.mu.Unlock() + if _, ok := s.removingWorkstreams[key]; ok { + return false + } + s.removingWorkstreams[key] = &workstreamRemoval{removals: 1, retired: make(chan struct{})} + return true +} + +// endWorkstreamRemoval lifts a beginWorkstreamRemoval or tryBeginRetire mark. +func (s *Service) endWorkstreamRemoval(ref WorkstreamRef) { + key := concurrencyKey(ref.ProjectID, ref.WorkstreamID) + s.mu.Lock() + defer s.mu.Unlock() + removal := s.removingWorkstreams[key] + if removal.removals--; removal.removals > 0 { + return + } + delete(s.removingWorkstreams, key) + if removal.retired != nil { + close(removal.retired) + } +} + +// removeWorkstream tears workstream down, its sessions then its directory, +// and drops its record: ErrWorkstreamNotFound when that is gone already. +func (s *Service) removeWorkstream(ctx context.Context, ref WorkstreamRef, workstream domain.Workstream, + opts RemoveOptions) error { // Stop anything still being built before tearing the sessions down. A create // runs detached from the request that asked for it, so without this the build // outlives the workstream: `enclave run` keeps going, finishes into a store @@ -1314,13 +1501,13 @@ func (s *Service) Remove(ctx context.Context, ref WorkstreamRef, opts RemoveOpti // is the half of the force contract ("busy sessions are stopped and torn down // with it") that a placeholder needs, because the row a build is showing on is // busy but is not yet a session any provider could be asked to stop. - if cancelled := s.cancelCreates(ws); cancelled > 0 { + if cancelled := s.cancelCreates(workstream); cancelled > 0 { slog.Info("cancelled in-flight session creation for a workstream being removed", "workstream", ref.WorkstreamID, "creates", cancelled) } // Tear down the sessions first, via each session's owning provider. - for _, sess := range ws.Sessions { + for _, sess := range workstream.Sessions { // Held until the workstream is gone, so no restart starts on the way out. s.beginRemoval(sess.ID) defer s.endRemoval(sess.ID) @@ -1342,9 +1529,13 @@ func (s *Service) Remove(ctx context.Context, ref WorkstreamRef, opts RemoveOpti } } - if wp, ok := s.registry.WorkstreamProvider(ws.Provider); ok { + if wp, ok := s.registry.WorkstreamProvider(workstream.Provider); ok { if project, ok := s.projectRef(ref.ProjectID); ok { - if err := wp.Remove(ctx, project, ws, opts); err != nil { + err := wp.Remove(ctx, project, workstream, opts) + // Before the record goes, so a worktree deleted with it is never + // suggested from a listing cached before. + s.reconciler.forgetFolders(ref.ProjectID) + if err != nil { return err } } @@ -1353,6 +1544,9 @@ func (s *Service) Remove(ctx context.Context, ref WorkstreamRef, opts RemoveOpti if err := s.store.RemoveWorkstream(ref.ProjectID, ref.WorkstreamID); err != nil { return ErrWorkstreamNotFound } + // A folder it kept is suggested again: a refresh tick may have held a + // listing from before the forget above, and would see nothing change. + s.reconciler.resurvey() return nil } @@ -1375,6 +1569,9 @@ func (s *Service) RemoveProject(ctx context.Context, projectID string) error { if err != nil { return ErrProjectNotFound } + s.reconciler.forgetProject(projectID) + // Its folders are free now: a project sharing its repository suggests them. + s.reconciler.resurvey() if len(running) > 0 { // Not waited for here: a cancelled restart may still take a while to stop // what it started, and the teardown must outlive the request. @@ -1669,10 +1866,9 @@ func (s *Service) releaseSlotLocked(key string) { } func (s *Service) projectRef(projectID string) (ProjectRef, bool) { - for _, p := range s.store.Projects() { - if p.ID == projectID { - return ProjectRef{ID: p.ID, Path: p.Path, GitBacked: p.GitBacked}, true - } + project, err := s.store.Project(projectID) + if err != nil { + return ProjectRef{}, false } - return ProjectRef{}, false + return projectRefOf(project), true } diff --git a/daemon/internal/provider/service_remove_test.go b/daemon/internal/provider/service_remove_test.go index f4a476a..7c6a7db 100644 --- a/daemon/internal/provider/service_remove_test.go +++ b/daemon/internal/provider/service_remove_test.go @@ -2,14 +2,20 @@ package provider_test import ( "context" + "encoding/json" "errors" + "os" + "os/exec" + "path/filepath" "slices" "strings" "testing" + "time" "github.com/eclipsesource/homeshell/daemon/internal/domain" "github.com/eclipsesource/homeshell/daemon/internal/provider" "github.com/eclipsesource/homeshell/daemon/internal/provider/fake" + "github.com/eclipsesource/homeshell/daemon/internal/settings" "github.com/eclipsesource/homeshell/daemon/internal/store" ) @@ -241,3 +247,94 @@ func TestServiceRemovePlaceholderIsNotGuarded(t *testing.T) { t.Error("placeholder survived removal") } } + +// git runs git in dir, failing the test on error. +func git(t *testing.T, dir string, args ...string) { + t.Helper() + cmd := exec.Command("git", append([]string{"-C", dir}, args...)...) + cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null", + "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com", + "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } +} + +// A worktree removed outside HomeShell takes its workstream with it, and the +// sessions there are stopped rather than adopted into a workstream of their own. +func TestServiceRetiresAVanishedWorktreeWithItsSessions(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + repo := t.TempDir() + git(t, repo, "init", "-q", "-b", "main") + git(t, repo, "commit", "-q", "--allow-empty", "-m", "initial") + st := store.NewTestStore(t, []domain.Project{{ + ID: "p1", Name: "p1", Path: repo, GitBacked: true, Workstreams: []domain.Workstream{}, + }}) + reg := provider.NewRegistry() + fp := fake.New(fake.Config{}) + if err := reg.RegisterSessionProvider(fp); err != nil { + t.Fatal(err) + } + svc := provider.NewService(st, reg, 0) + settingsStore, err := settings.Load(filepath.Join(t.TempDir(), "settings.json")) + if err != nil { + t.Fatal(err) + } + next := settingsStore.Get() + next.Providers.RefreshIntervalSec = settings.MinRefreshSec + if _, err := settingsStore.Set(next); err != nil { + t.Fatal(err) + } + reg.InjectSettings(settingsStore) + + dir := filepath.Join(t.TempDir(), "wt") + config, _ := json.Marshal(map[string]string{"strategy": "worktree", "path": dir}) + if _, err := svc.Create(context.Background(), provider.CreateInput{ + ProjectID: "p1", ProviderID: provider.CoreProviderID, Name: "feature", Config: config, + }); err != nil { + t.Fatalf("Create: %v", err) + } + ws := awaitWorkstream(t, st, "p1") + if _, err := svc.CreateSession(context.Background(), provider.CreateSessionInput{ + ProjectID: "p1", WorkstreamID: ws.ID, ProviderID: "fake", OptionID: "claude", + }); err != nil { + t.Fatalf("CreateSession: %v", err) + } + sess := awaitSession(t, st, "p1", ws.ID, "Claude Code") + sref := provider.SessionRef{ProjectID: "p1", WorkstreamID: ws.ID, SessionID: sess.ID} + if err := svc.StopSession(context.Background(), sref); err != nil { + t.Fatalf("StopSession: %v", err) + } + if err := svc.StartSession(context.Background(), sref); err != nil { + t.Fatalf("StartSession: %v", err) + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go svc.Run(ctx) + git(t, repo, "worktree", "remove", "--force", dir) + + deadline := time.Now().Add(5 * time.Second) + for len(st.Projects()[0].Workstreams) != 0 && time.Now().Before(deadline) { + time.Sleep(10 * time.Millisecond) + } + if workstreams := st.Projects()[0].Workstreams; len(workstreams) != 0 { + t.Fatalf("workstreams = %+v, want the vanished one gone and none adopted", workstreams) + } + for time.Now().Before(deadline) { + if _, err := fp.SessionState(context.Background(), sref); err != nil { + break + } + time.Sleep(10 * time.Millisecond) + } + if _, err := fp.SessionState(context.Background(), sref); err == nil { + t.Error("the provider still runs the session of the vanished workstream") + } + // Passes after the retirement adopt nothing either. + svc.Reconcile(context.Background()) + if workstreams := st.Projects()[0].Workstreams; len(workstreams) != 0 { + t.Errorf("workstreams after another pass = %+v, want none", workstreams) + } +} diff --git a/daemon/internal/provider/service_restart_test.go b/daemon/internal/provider/service_restart_test.go index da5674f..1410320 100644 --- a/daemon/internal/provider/service_restart_test.go +++ b/daemon/internal/provider/service_restart_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "os/exec" "path/filepath" "reflect" "slices" @@ -498,6 +499,83 @@ func TestRemoveProjectLeavesARestartOfASessionAnotherProjectLists(t *testing.T) } } +// surveyingFake is the fake, as a workstream provider that suggests the folder +// in folder, whoever claims it. +type surveyingFake struct { + *fake.Provider + folder string +} + +func (surveying *surveyingFake) ListFolders(context.Context, provider.ProjectRef) ([]provider.Folder, error) { + return []provider.Folder{{Path: surveying.folder}}, nil +} + +func (surveying *surveyingFake) Survey(_ provider.ProjectRef, folders []provider.Folder, _ []domain.Workstream, + _ []string) provider.Survey { + var survey provider.Survey + for _, folder := range folders { + survey.Untracked = append(survey.Untracked, domain.UntrackedWorktree{Name: "wt", Path: folder.Path}) + } + return survey +} + +func (surveying *surveyingFake) TrackFolder(domain.UntrackedWorktree, func(string) bool) domain.Workstream { + return domain.Workstream{} +} + +// Removing a project cancels the restart of a session another project only +// hides on an untracked worktree: there is no workstream to show it in. +func TestRemoveProjectCancelsARestartOfASessionOnlyHiddenElsewhere(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + fakeProvider := fake.New(fake.Config{SessionStartup: 300 * time.Millisecond}) + surveying := &surveyingFake{Provider: fakeProvider} + st := store.NewTestStore(t, []domain.Project{{ID: "p1", Name: "p1", Path: t.TempDir(), Workstreams: []domain.Workstream{}}}) + reg := provider.NewRegistry() + if err := reg.RegisterWorkstreamProvider(surveying); err != nil { + t.Fatal(err) + } + if err := reg.RegisterSessionProvider(fakeProvider); err != nil { + t.Fatal(err) + } + svc := provider.NewService(st, reg, 0) + svc.Reconcile(context.Background()) + restarted := sandboxedSession(t, svc, st, `{}`) + project := st.Projects()[0] + workstream, err := st.Workstream(restarted.ProjectID, restarted.WorkstreamID) + if err != nil { + t.Fatal(err) + } + // A git project holding p1's folder, its workstream's folder a worktree of it. + surveying.folder = workstream.WorkingDir + parent := filepath.Dir(project.Path) + if out, err := exec.Command("git", "init", "-q", parent).CombinedOutput(); err != nil { + t.Fatalf("git init: %v\n%s", err, out) + } + if err := st.AddProject(&domain.Project{ID: "p2", Name: "p2", Path: parent, GitBacked: true}); err != nil { + t.Fatalf("AddProject: %v", err) + } + svc.Reconcile(context.Background()) + if hidden := st.Projects()[1].UntrackedWorktrees; len(hidden) != 1 || len(hidden[0].Sessions) == 0 { + t.Fatalf("the other project does not hide the session: %+v", st.Projects()[1]) + } + + if _, err := svc.RestartSession(context.Background(), provider.RestartSessionInput{Session: restarted}); err != nil { + t.Fatalf("RestartSession: %v", err) + } + if err := svc.RemoveProject(context.Background(), restarted.ProjectID); err != nil { + t.Fatalf("RemoveProject: %v", err) + } + // The interrupted restart's session is removed in the background. + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + svc.Shutdown(ctx) + if left := discoveredIDs(t, fakeProvider, project); len(left) != 0 { + t.Errorf("sessions left running = %q; want the restarted one removed", left) + } +} + // failingFake is the fake, whose restarts wait for release, then fail without // touching the session. type failingFake struct { diff --git a/daemon/internal/provider/service_untracked_test.go b/daemon/internal/provider/service_untracked_test.go new file mode 100644 index 0000000..8e74768 --- /dev/null +++ b/daemon/internal/provider/service_untracked_test.go @@ -0,0 +1,229 @@ +package provider + +import ( + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "slices" + "testing" + + "github.com/eclipsesource/homeshell/daemon/internal/domain" + "github.com/eclipsesource/homeshell/daemon/internal/store" +) + +// untrackedService is a service over a git project p1 with one linked +// worktree on branch wt-a, a session s1 running there, and a project p2 +// outside git. It returns the repository and the worktree's paths. +func untrackedService(t *testing.T) (service *Service, projectStore *store.Store, repo, dir string) { + t.Helper() + repo = gitProject(t) + dir = addWorktree(t, repo, "wt-a") + projectStore = store.NewTestStore(t, []domain.Project{ + {ID: "p1", Name: "p1", Path: repo, GitBacked: true, Workstreams: []domain.Workstream{}}, + {ID: "p2", Name: "p2", Path: t.TempDir(), Workstreams: []domain.Workstream{}}, + }) + registry := NewRegistry() + sessionProvider := &stubSessionProvider{id: "prov", available: true, discovered: map[string][]DiscoveredSession{ + "p1": {ds(dir, "s1", domain.SessionRunning)}, + }} + if err := registry.RegisterSessionProvider(sessionProvider); err != nil { + t.Fatal(err) + } + service = NewService(projectStore, registry, 0) + service.Reconcile(context.Background()) + return service, projectStore, repo, dir +} + +func mustProject(t *testing.T, projectStore *store.Store, projectID string) domain.Project { + t.Helper() + project, err := projectStore.Project(projectID) + if err != nil { + t.Fatal(err) + } + return project +} + +func TestServiceAddUntrackedWorktree(t *testing.T) { + tests := []struct { + name string + // takenName, when set, is held by another workstream beforehand. + takenName string + wantName string + }{ + {name: "named like its suggestion", wantName: "wt-a"}, + {name: "numbered when that name is taken", takenName: "wt-a", wantName: "wt-a-2"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + service, projectStore, _, dir := untrackedService(t) + if tt.takenName != "" { + if err := projectStore.AddWorkstream("p1", domain.Workstream{ID: "ws-other", Name: tt.takenName, + Provider: CoreProviderID, WorkingDir: t.TempDir(), Sessions: []domain.Session{}}); err != nil { + t.Fatal(err) + } + } + + added, err := service.AddUntrackedWorktree(context.Background(), "p1", dir) + if err != nil { + t.Fatalf("AddUntrackedWorktree: %v", err) + } + if added.Name != tt.wantName || added.Branch != "wt-a" || added.StrategyID != "worktree" || + added.Provider != CoreProviderID || added.WorkingDir != dir { + t.Errorf("added = %+v, want %s on branch wt-a, a core worktree in %s", added, tt.wantName, dir) + } + if ids := sessionIDs(added.Sessions); !slices.Equal(ids, []string{"s1"}) { + t.Errorf("sessions = %q, want [s1]", ids) + } + if suggestions := mustProject(t, projectStore, "p1").UntrackedWorktrees; len(suggestions) != 0 { + t.Errorf("suggestions = %+v, want none once added", suggestions) + } + }) + } +} + +func TestServiceAddUntrackedWorktreeRefuses(t *testing.T) { + tests := []struct { + name string + // setup prepares the refusal and returns the project and path to add. + setup func(t *testing.T, service *Service, repo, dir string) (projectID, path string) + }{ + {name: "the project folder", setup: func(_ *testing.T, _ *Service, repo, _ string) (string, string) { + return "p1", repo + }}, + {name: "a folder no worktree", setup: func(t *testing.T, _ *Service, _, _ string) (string, string) { + return "p1", t.TempDir() + }}, + {name: "a worktree already added", setup: func(t *testing.T, service *Service, _, dir string) (string, string) { + if _, err := service.AddUntrackedWorktree(context.Background(), "p1", dir); err != nil { + t.Fatal(err) + } + return "p1", dir + }}, + {name: "a worktree removed since it was listed", + setup: func(t *testing.T, _ *Service, repo, dir string) (string, string) { + if out, err := exec.Command("git", "-C", repo, "worktree", "remove", dir).CombinedOutput(); err != nil { + t.Fatalf("git worktree remove: %v\n%s", err, out) + } + return "p1", dir + }}, + {name: "a project outside git", setup: func(_ *testing.T, _ *Service, _, dir string) (string, string) { + return "p2", dir + }}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + service, projectStore, repo, dir := untrackedService(t) + projectID, path := tt.setup(t, service, repo, dir) + before := len(mustProject(t, projectStore, projectID).Workstreams) + + _, err := service.AddUntrackedWorktree(context.Background(), projectID, path) + if !errors.Is(err, ErrNotUntrackedWorktree) { + t.Errorf("AddUntrackedWorktree = %v, want ErrNotUntrackedWorktree", err) + } + if after := len(mustProject(t, projectStore, projectID).Workstreams); after != before { + t.Errorf("workstreams = %d, want %d: nothing added", after, before) + } + }) + } + + t.Run("an unknown project", func(t *testing.T) { + service, _, _, dir := untrackedService(t) + if _, err := service.AddUntrackedWorktree(context.Background(), "nope", dir); !errors.Is(err, ErrProjectNotFound) { + t.Errorf("AddUntrackedWorktree = %v, want ErrProjectNotFound", err) + } + }) +} + +// An added worktree removed with its files is gone, and suggested no more. +// Kept, it is suggested again (TestRemoveKeepingFilesSuggestsTheWorktreeAgain). +func TestRemoveDeletingFilesSuggestsTheWorktreeNoMore(t *testing.T) { + service, projectStore, _, dir := untrackedService(t) + added, err := service.AddUntrackedWorktree(context.Background(), "p1", dir) + if err != nil { + t.Fatal(err) + } + + workstreamRef := WorkstreamRef{ProjectID: "p1", WorkstreamID: added.ID} + if err := service.Remove(context.Background(), workstreamRef, RemoveOptions{Force: true}); err != nil { + t.Fatalf("Remove: %v", err) + } + service.Reconcile(context.Background()) + + if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) { + t.Errorf("stat %s = %v, want the worktree deleted", dir, err) + } + if suggestions := mustProject(t, projectStore, "p1").UntrackedWorktrees; len(suggestions) != 0 { + t.Errorf("suggestions = %+v, want none", suggestions) + } +} + +// windowProbe is the core, running probe once its Create has made the +// worktree, before the service stores the workstream. +type windowProbe struct { + *core + probe func() +} + +func (p windowProbe) Create(ctx context.Context, req CreateRequest) (domain.Workstream, error) { + created, err := p.core.Create(ctx, req) + if err == nil { + p.probe() + } + return created, err +} + +// Between git making a worktree and the service storing its workstream, no +// workstream holds the folder: the core's claim does, so a pass in that window +// does not suggest it, and an Add there is refused. +func TestServiceCreateClaimsTheWorktreeUntilStored(t *testing.T) { + repo := gitProject(t) + projectStore := store.NewTestStore(t, []domain.Project{ + {ID: "p1", Name: "p1", Path: repo, GitBacked: true, Workstreams: []domain.Workstream{}}, + }) + registry := NewRegistry() + provider := windowProbe{core: &core{root: t.TempDir()}} + if err := registry.RegisterWorkstreamProvider(&provider); err != nil { + t.Fatal(err) + } + service := NewService(projectStore, registry, 0) + dest := filepath.Join(t.TempDir(), "wt") + var suggestedInWindow []domain.UntrackedWorktree + var addErr error + probed := make(chan struct{}) + provider.probe = func() { + defer close(probed) + if err := service.reconciler.ReconcileProject(context.Background(), "p1"); err != nil { + t.Errorf("ReconcileProject: %v", err) + } + suggestedInWindow = mustProject(t, projectStore, "p1").UntrackedWorktrees + _, addErr = service.AddUntrackedWorktree(context.Background(), "p1", dest) + } + + config := []byte(`{"strategy":"worktree","branch":"wt-b","path":"` + dest + `"}`) + if _, err := service.Create(context.Background(), CreateInput{ProjectID: "p1", Config: config}); err != nil { + t.Fatalf("Create: %v", err) + } + <-probed + waitFor(t, "the created workstream", func() bool { + return slices.ContainsFunc(mustProject(t, projectStore, "p1").Workstreams, func(workstream domain.Workstream) bool { + return workstream.WorkingDir == dest && !isPlaceholder(workstream) + }) + }) + + if len(suggestedInWindow) != 0 { + t.Errorf("suggestions while creating = %+v, want none", suggestedInWindow) + } + if !errors.Is(addErr, ErrNotUntrackedWorktree) { + t.Errorf("AddUntrackedWorktree while creating = %v, want ErrNotUntrackedWorktree", addErr) + } + if _, err := os.Stat(dest); err != nil { + t.Errorf("stat %s = %v, want the created worktree kept", dest, err) + } + waitFor(t, "the claim's release", func() bool { + provider.claimMu.Lock() + defer provider.claimMu.Unlock() + return len(provider.claimed) == 0 + }) +} diff --git a/daemon/internal/provider/stub_test.go b/daemon/internal/provider/stub_test.go index 92fd7dc..d443320 100644 --- a/daemon/internal/provider/stub_test.go +++ b/daemon/internal/provider/stub_test.go @@ -19,6 +19,7 @@ type stubSessionProvider struct { discoverErr error caps Capabilities sessionSchemas []SessionSchemaOption + removed []string // the session IDs RemoveSession was asked for } func (s *stubSessionProvider) ID() string { return s.id } @@ -38,7 +39,10 @@ func (s *stubSessionProvider) CreateSession(_ context.Context, _ CreateSessionRe return domain.Session{}, nil } -func (s *stubSessionProvider) RemoveSession(_ context.Context, _ SessionRef) error { return nil } +func (s *stubSessionProvider) RemoveSession(_ context.Context, ref SessionRef) error { + s.removed = append(s.removed, ref.SessionID) + return nil +} func (s *stubSessionProvider) SessionState(context.Context, SessionRef) (domain.Session, error) { return domain.Session{}, nil diff --git a/daemon/internal/provider/survey.go b/daemon/internal/provider/survey.go new file mode 100644 index 0000000..e25d6b7 --- /dev/null +++ b/daemon/internal/provider/survey.go @@ -0,0 +1,77 @@ +package provider + +import ( + "context" + + "github.com/eclipsesource/homeshell/daemon/internal/domain" + "github.com/eclipsesource/homeshell/daemon/internal/git" +) + +// Folder is one folder of a FolderSurveyor's listing. +type Folder struct { + Path string + Branch string // the branch checked out there; "" when none + // Missing says the folder is not there, deleted or out of reach: there is + // nothing to suggest adding. + Missing bool + // Gone says the folder was deleted, though the listing still names it: its + // workstream vanishes. A folder only out of reach is Missing, not Gone. + Gone bool +} + +// Survey is what a FolderSurveyor makes of a listing: the folders no one +// claims, suggested as workstreams, and the IDs of its workstreams whose +// folder is gone. +type Survey struct { + Untracked []domain.UntrackedWorktree + Vanished []string +} + +// FolderSurveyor is a workstream provider whose workstreams sit in folders something outside HomeShell +// also tracks (a repository's worktrees), which appear and vanish on their own. +type FolderSurveyor interface { + WorkstreamProvider + // ListFolders is the provider's one external query per project per refresh. A caller's deadline + // bounds it; without one, the provider bounds it as a refresh's. + ListFolders(ctx context.Context, project ProjectRef) ([]Folder, error) + // Survey is pure apart from resolving paths: which listed folders no one claims, and which of the + // provider's own workstreams in workstreams lost their folder. claimed are the folders any project + // or workstream already holds. + Survey(project ProjectRef, folders []Folder, workstreams []domain.Workstream, claimed []string) Survey + // TrackFolder makes a workstream of an untracked folder, creating nothing on disk, under a + // name nameTaken does not report taken. + TrackFolder(folder domain.UntrackedWorktree, nameTaken func(name string) bool) domain.Workstream +} + +// folderSet is a set of folders compared with symlinks resolved (git.SamePath), +// each path resolved once: resolving one stats each of its ancestors. +type folderSet struct { + resolved map[string]string + members map[string]bool +} + +// newFolderSet is the set of the folders in each of folderLists. +func newFolderSet(folderLists ...[]string) *folderSet { + set := &folderSet{resolved: map[string]string{}, members: map[string]bool{}} + for _, folders := range folderLists { + for _, folder := range folders { + set.members[set.realPath(folder)] = true + } + } + return set +} + +// realPath is folder with symlinks resolved (git.RealPath). +func (set *folderSet) realPath(folder string) string { + resolvedPath, ok := set.resolved[folder] + if !ok { + resolvedPath = git.RealPath(folder) + set.resolved[folder] = resolvedPath + } + return resolvedPath +} + +// has reports whether folder is one of the set's. +func (set *folderSet) has(folder string) bool { + return set.members[set.realPath(folder)] +} diff --git a/daemon/internal/store/ephemeral_session_test.go b/daemon/internal/store/ephemeral_session_test.go index 31d19b3..5850136 100644 --- a/daemon/internal/store/ephemeral_session_test.go +++ b/daemon/internal/store/ephemeral_session_test.go @@ -122,11 +122,11 @@ func TestPromoteEphemeralSessionAlreadyListedByAProjection(t *testing.T) { t.Fatalf("AddEphemeralSession() error = %v", err) } projected := domain.Session{ID: "sess-1", Agent: "Claude", State: domain.SessionIdle} - if err := testStore.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := testStore.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{projected}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams() error = %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection() error = %v", err) } real := domain.Session{ID: "sess-1", Agent: "Claude", State: domain.SessionRunning} @@ -298,7 +298,7 @@ func TestFailEphemeralSessionReportsAnAbsentSession(t *testing.T) { // them deliberately), so the persistence path has to be the thing that refuses // them — otherwise the never-persisted contract holds only until the next // reconcile, and a restart brings the placeholder back as an immortal row. -func TestReplaceWorkstreamsDoesNotPersistAPlaceholder(t *testing.T) { +func TestReplaceProjectionDoesNotPersistAPlaceholder(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Session{ ID: StartingIDPrefix + "abc", Title: "Claude session", @@ -309,11 +309,11 @@ func TestReplaceWorkstreamsDoesNotPersistAPlaceholder(t *testing.T) { t.Fatalf("AddEphemeralSession: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{real, placeholder}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if got, want := persistedIDs(t, s), []string{"hs-real"}; !slices.Equal(got, want) { @@ -335,7 +335,7 @@ func TestReplaceWorkstreamsDoesNotPersistAPlaceholder(t *testing.T) { // and replacing the state wholesale would drop the row the user is watching — // for the rest of a multi-minute build, with nothing to bring it back, because // no provider reports a placeholder. -func TestReplaceWorkstreamsKeepsAPlaceholderAddedDuringTheCycle(t *testing.T) { +func TestReplaceProjectionKeepsAPlaceholderAddedDuringTheCycle(t *testing.T) { s := ephemeralFixture(t) // What the reconciler saw when it started: one workstream, no sessions. @@ -354,8 +354,8 @@ func TestReplaceWorkstreamsKeepsAPlaceholderAddedDuringTheCycle(t *testing.T) { } // The projection, built from the stale snapshot, finally lands. - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } ws, err := s.Workstream("p1", "ws-1") @@ -372,7 +372,7 @@ func TestReplaceWorkstreamsKeepsAPlaceholderAddedDuringTheCycle(t *testing.T) { // The carry-over must not double a placeholder the projection already has — the // common case, since the reconciler retains the ones it saw. -func TestReplaceWorkstreamsDoesNotDuplicateARetainedPlaceholder(t *testing.T) { +func TestReplaceProjectionDoesNotDuplicateARetainedPlaceholder(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Session{ ID: StartingIDPrefix + "abc", Provider: "enclave", State: domain.SessionStarting, @@ -380,11 +380,11 @@ func TestReplaceWorkstreamsDoesNotDuplicateARetainedPlaceholder(t *testing.T) { if err := s.AddEphemeralSession("p1", "ws-1", placeholder); err != nil { t.Fatalf("AddEphemeralSession: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{placeholder}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } ws, err := s.Workstream("p1", "ws-1") if err != nil { @@ -397,7 +397,7 @@ func TestReplaceWorkstreamsDoesNotDuplicateARetainedPlaceholder(t *testing.T) { // A promoted session replaces its placeholder before the reconcile that follows, // so the carry-over must not resurrect the placeholder it just retired. -func TestReplaceWorkstreamsDoesNotResurrectAPromotedPlaceholder(t *testing.T) { +func TestReplaceProjectionDoesNotResurrectAPromotedPlaceholder(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Session{ ID: StartingIDPrefix + "abc", Provider: "enclave", State: domain.SessionStarting, @@ -409,11 +409,11 @@ func TestReplaceWorkstreamsDoesNotResurrectAPromotedPlaceholder(t *testing.T) { if err := s.PromoteEphemeralSession("p1", "ws-1", placeholder.ID, real, domain.LaunchConfig{}); err != nil { t.Fatalf("PromoteEphemeralSession: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{real}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } ws, err := s.Workstream("p1", "ws-1") if err != nil { @@ -427,7 +427,7 @@ func TestReplaceWorkstreamsDoesNotResurrectAPromotedPlaceholder(t *testing.T) { // The workstream-level half of the same race: a create that starts while a // reconcile is in flight must not have its placeholder card wiped by the // projection that raced it. -func TestReplaceWorkstreamsKeepsAWorkstreamPlaceholderAddedDuringTheCycle(t *testing.T) { +func TestReplaceProjectionKeepsAWorkstreamPlaceholderAddedDuringTheCycle(t *testing.T) { s := ephemeralFixture(t) snapshot := []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", @@ -440,8 +440,8 @@ func TestReplaceWorkstreamsKeepsAWorkstreamPlaceholderAddedDuringTheCycle(t *tes if err := s.AddWorkstream("p1", placeholder); err != nil { t.Fatalf("AddWorkstream: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if _, err := s.Workstream("p1", placeholder.ID); err != nil { t.Errorf("the creation placeholder is gone after a stale projection: %v", err) @@ -451,7 +451,7 @@ func TestReplaceWorkstreamsKeepsAWorkstreamPlaceholderAddedDuringTheCycle(t *tes // A workstream create that lands while a projection is being computed replaces // a placeholder the projection still holds: the projection must carry the real // workstream rather than restore the placeholder and drop what replaced it. -func TestReplaceWorkstreamsKeepsAWorkstreamPromotedDuringTheCycle(t *testing.T) { +func TestReplaceProjectionKeepsAWorkstreamPromotedDuringTheCycle(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Workstream{ ID: PlaceholderIDPrefix + "abc", Name: "feature/x", Provider: "core", @@ -468,8 +468,8 @@ func TestReplaceWorkstreamsKeepsAWorkstreamPromotedDuringTheCycle(t *testing.T) if err := s.PromoteWorkstream("p1", placeholder.ID, real); err != nil { t.Fatalf("PromoteWorkstream: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } var ids []string @@ -487,7 +487,7 @@ func TestReplaceWorkstreamsKeepsAWorkstreamPromotedDuringTheCycle(t *testing.T) // A placeholder removed while a projection was being computed — its create // failed or was cancelled — must stay gone: the snapshot's copy is stale. -func TestReplaceWorkstreamsDoesNotResurrectARemovedWorkstreamPlaceholder(t *testing.T) { +func TestReplaceProjectionDoesNotResurrectARemovedWorkstreamPlaceholder(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Workstream{ ID: PlaceholderIDPrefix + "abc", Name: "feature/x", Provider: "core", @@ -503,8 +503,8 @@ func TestReplaceWorkstreamsDoesNotResurrectARemovedWorkstreamPlaceholder(t *test if err := s.RemoveWorkstream("p1", placeholder.ID); err != nil { t.Fatalf("RemoveWorkstream: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if ids, want := workstreamIDs(s, "p1"), []string{"ws-1"}; !slices.Equal(ids, want) { t.Errorf("workstreams = %v, want %v", ids, want) @@ -513,7 +513,7 @@ func TestReplaceWorkstreamsDoesNotResurrectARemovedWorkstreamPlaceholder(t *test // A workstream deleted after its placeholder was promoted must not bring the // placeholder back as a card stuck in "creating" forever. -func TestReplaceWorkstreamsDoesNotResurrectThePlaceholderOfARemovedWorkstream(t *testing.T) { +func TestReplaceProjectionDoesNotResurrectThePlaceholderOfARemovedWorkstream(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Workstream{ ID: PlaceholderIDPrefix + "abc", Name: "feature/x", Provider: "core", @@ -533,8 +533,8 @@ func TestReplaceWorkstreamsDoesNotResurrectThePlaceholderOfARemovedWorkstream(t if err := s.RemoveWorkstream("p1", "ws-2"); err != nil { t.Fatalf("RemoveWorkstream: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if ids, want := workstreamIDs(s, "p1"), []string{"ws-1"}; !slices.Equal(ids, want) { t.Errorf("workstreams = %v, want %v", ids, want) @@ -543,7 +543,7 @@ func TestReplaceWorkstreamsDoesNotResurrectThePlaceholderOfARemovedWorkstream(t // A placeholder still in flight keeps the live copy, not the snapshot's: its // status message has moved on since the snapshot was taken. -func TestReplaceWorkstreamsKeepsTheLiveCopyOfAWorkstreamPlaceholder(t *testing.T) { +func TestReplaceProjectionKeepsTheLiveCopyOfAWorkstreamPlaceholder(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Workstream{ ID: PlaceholderIDPrefix + "abc", Name: "feature/x", Provider: "core", @@ -559,8 +559,8 @@ func TestReplaceWorkstreamsKeepsTheLiveCopyOfAWorkstreamPlaceholder(t *testing.T if err := s.UpdateWorkstream("p1", progressed); err != nil { t.Fatalf("UpdateWorkstream: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } got, err := s.Workstream("p1", placeholder.ID) if err != nil { @@ -589,7 +589,7 @@ func workstreamIDs(s *Store, projectID string) []string { // working directory and nothing saying what it was — a card nothing can finish, // refresh or explain. AddWorkstream does not write one; the projection must not // write it either, and it carries placeholders through every cycle. -func TestReplaceWorkstreamsDoesNotPersistAWorkstreamPlaceholder(t *testing.T) { +func TestReplaceProjectionDoesNotPersistAWorkstreamPlaceholder(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Workstream{ ID: PlaceholderIDPrefix + "abc", Name: "feature/x", Provider: "core", @@ -599,11 +599,11 @@ func TestReplaceWorkstreamsDoesNotPersistAWorkstreamPlaceholder(t *testing.T) { if err := s.AddWorkstream("p1", placeholder); err != nil { t.Fatalf("AddWorkstream: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{ {ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{}}, placeholder, - }); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } ids := []string{} @@ -647,11 +647,11 @@ func TestPromotedSessionNamesItsPlaceholder(t *testing.T) { } assertLink("promoted") - if err := testStore.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := testStore.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{real}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams() error = %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection() error = %v", err) } assertLink("reprojected") } @@ -721,11 +721,11 @@ func TestPromotedSessionListedElsewhereIsNotRetainedTwice(t *testing.T) { t.Fatalf("PromoteEphemeralSession: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{ {ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place"}, {ID: "ws-2", Name: "other", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{promoted}}, - }); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } listings := 0 for _, workstream := range s.Projects()[0].Workstreams { @@ -743,7 +743,7 @@ func TestPromotedSessionListedElsewhereIsNotRetainedTwice(t *testing.T) { // A projection whose snapshot still held a placeholder, promoted while the // providers were being asked, must not bring the placeholder back beside the // session that replaced it: nothing would ever finish or remove it. -func TestReplaceWorkstreamsDropsAPlaceholderPromotedDuringTheCycle(t *testing.T) { +func TestReplaceProjectionDropsAPlaceholderPromotedDuringTheCycle(t *testing.T) { s := ephemeralFixture(t) placeholder := domain.Session{ ID: StartingIDPrefix + "abc", Provider: "enclave", State: domain.SessionStarting, @@ -759,8 +759,8 @@ func TestReplaceWorkstreamsDropsAPlaceholderPromotedDuringTheCycle(t *testing.T) if err := s.PromoteEphemeralSession("p1", "ws-1", placeholder.ID, real, domain.LaunchConfig{}); err != nil { t.Fatalf("PromoteEphemeralSession: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } ws, err := s.Workstream("p1", "ws-1") if err != nil { @@ -774,7 +774,7 @@ func TestReplaceWorkstreamsDropsAPlaceholderPromotedDuringTheCycle(t *testing.T) // A workstream created from start to finish while a projection was being // computed is in neither the projection nor, as a placeholder, the live state: // it must still survive the swap, or it is lost and its name freed for another. -func TestReplaceWorkstreamsKeepsAWorkstreamCreatedDuringTheCycle(t *testing.T) { +func TestReplaceProjectionKeepsAWorkstreamCreatedDuringTheCycle(t *testing.T) { s := ephemeralFixture(t) snapshot := []domain.Workstream{ {ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", Sessions: []domain.Session{}}, @@ -790,8 +790,8 @@ func TestReplaceWorkstreamsKeepsAWorkstreamCreatedDuringTheCycle(t *testing.T) { if err := s.PromoteWorkstream("p1", placeholder.ID, real); err != nil { t.Fatalf("PromoteWorkstream: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, snapshot); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, snapshot, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if ids, want := workstreamIDs(s, "p1"), []string{"ws-1", "ws-2"}; !slices.Equal(ids, want) { t.Errorf("workstreams = %v, want %v", ids, want) diff --git a/daemon/internal/store/launchconfig_test.go b/daemon/internal/store/launchconfig_test.go index fbcd7e4..b43fc8d 100644 --- a/daemon/internal/store/launchconfig_test.go +++ b/daemon/internal/store/launchconfig_test.go @@ -18,11 +18,11 @@ func TestLaunchConfigRoundTripsAndSurvivesReprojection(t *testing.T) { t.Fatalf("SetLaunchConfig: %v", err) } // The session is re-reported by its provider, as every reconcile cycle does. - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/tmp/p1", Sessions: []domain.Session{{ID: "sess-1", Title: "claude session", Agent: "claude", Provider: "enclave"}}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } got, ok := s.LaunchConfig("sess-1") if !ok { @@ -45,10 +45,10 @@ func TestReprojectionForgetsLaunchConfigsOfVanishedSessions(t *testing.T) { if err := s.SetLaunchConfig("p1", "sess-1", domain.LaunchConfig{ProviderID: "enclave", OptionID: "claude"}); err != nil { t.Fatalf("SetLaunchConfig: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/tmp/p1", - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if _, ok := s.LaunchConfig("sess-1"); ok { t.Error("LaunchConfig of a session no provider reports any more: want it forgotten") @@ -98,11 +98,11 @@ func TestRemoveProjectMovesTheRecordsOfASessionAnotherProjectLists(t *testing.T) {ID: "p2", Name: "p2", Path: "/tmp/shared"}, }) // Projected into the other project sharing its directory, as discovery does. - if err := s.ReplaceWorkstreams("p2", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p2", nil, []domain.Workstream{{ ID: "ws-2", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/tmp/shared", Sessions: []domain.Session{shared}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if err := s.SetLaunchConfig("p1", "sess-1", domain.LaunchConfig{ProviderID: "enclave", OptionID: "claude"}); err != nil { t.Fatalf("SetLaunchConfig: %v", err) @@ -128,11 +128,11 @@ func TestRemoveProjectMovesTheRecordsOfASessionAnotherProjectLists(t *testing.T) t.Errorf("persisted title = %+v; want it moved to p2", titles["sess-1"]) } // The other project's next projection shows the title, and keeps both. - if err := s.ReplaceWorkstreams("p2", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p2", nil, []domain.Workstream{{ ID: "ws-2", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/tmp/shared", Sessions: []domain.Session{shared}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if title := sessionTitle(t, s, "sess-1"); title != "named" { t.Errorf("title = %q; want the custom one kept", title) @@ -202,8 +202,8 @@ func TestStaleProjectionKeepsAJustPromotedSessionsLaunchConfig(t *testing.T) { t.Fatalf("PromoteEphemeralSession: %v", err) } - if err := s.ReplaceWorkstreams("p1", nil, staleProjection()); err != nil { - t.Fatalf("ReplaceWorkstreams (stale): %v", err) + if err := s.ReplaceProjection("p1", nil, staleProjection(), nil); err != nil { + t.Fatalf("ReplaceProjection (stale): %v", err) } if got, ok := s.LaunchConfig("sess-1"); !ok || string(got.Config) != `{"model":"opus"}` { t.Errorf("LaunchConfig after a stale projection = %+v, %v; want the recorded one", got, ok) @@ -216,8 +216,8 @@ func TestStaleProjectionKeepsAJustPromotedSessionsLaunchConfig(t *testing.T) { } // The session has really gone: the next projection drops it, and its record. - if err := s.ReplaceWorkstreams("p1", nil, staleProjection()); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, staleProjection(), nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if _, ok := s.LaunchConfig("sess-1"); ok { t.Error("LaunchConfig of a session no projection lists: want it forgotten") diff --git a/daemon/internal/store/projection_persistence_test.go b/daemon/internal/store/projection_persistence_test.go index 065180c..0ab985c 100644 --- a/daemon/internal/store/projection_persistence_test.go +++ b/daemon/internal/store/projection_persistence_test.go @@ -28,7 +28,7 @@ func TestPreviewAndStateProjectionDoesNotRewriteStateFile(t *testing.T) { }}, }} } - if err := s.ReplaceWorkstreams("p", nil, projection("session", "first", domain.SessionRunning)); err != nil { + if err := s.ReplaceProjection("p", nil, projection("session", "first", domain.SessionRunning), nil); err != nil { t.Fatal(err) } written, err := os.Stat(path) @@ -36,7 +36,7 @@ func TestPreviewAndStateProjectionDoesNotRewriteStateFile(t *testing.T) { t.Fatal(err) } for _, state := range []domain.SessionState{domain.SessionRunning, domain.SessionIdle, domain.SessionWaiting} { - if err := s.ReplaceWorkstreams("p", nil, projection("session", string(state)+" preview", state)); err != nil { + if err := s.ReplaceProjection("p", nil, projection("session", string(state)+" preview", state), nil); err != nil { t.Fatal(err) } } @@ -51,7 +51,7 @@ func TestPreviewAndStateProjectionDoesNotRewriteStateFile(t *testing.T) { if got := s.Projects()[0].Workstreams[0].Sessions[0]; got.State != domain.SessionWaiting || got.Preview != "waiting preview" { t.Fatalf("live projection not updated: %+v", got) } - if err := s.ReplaceWorkstreams("p", nil, projection("new title", "latest", domain.SessionIdle)); err != nil { + if err := s.ReplaceProjection("p", nil, projection("new title", "latest", domain.SessionIdle), nil); err != nil { t.Fatal(err) } restored, err := persist.LoadState(path) @@ -100,7 +100,7 @@ func TestOnlyDurableFieldsRewriteStateFile(t *testing.T) { t.Run(tc.name, func(t *testing.T) { path := filepath.Join(t.TempDir(), "state.json") s := New([]domain.Project{{ID: "p", Name: "project", Path: "/tmp/p"}}, persist.NewFileSaver(path)) - if err := s.ReplaceWorkstreams("p", nil, base()); err != nil { + if err := s.ReplaceProjection("p", nil, base(), nil); err != nil { t.Fatal(err) } before, err := os.Stat(path) @@ -109,7 +109,7 @@ func TestOnlyDurableFieldsRewriteStateFile(t *testing.T) { } next := base() tc.change(&next[0], &next[0].Sessions[0]) - if err := s.ReplaceWorkstreams("p", nil, next); err != nil { + if err := s.ReplaceProjection("p", nil, next, nil); err != nil { t.Fatal(err) } after, err := os.Stat(path) diff --git a/daemon/internal/store/session_title_test.go b/daemon/internal/store/session_title_test.go index 1c43141..9dadf89 100644 --- a/daemon/internal/store/session_title_test.go +++ b/daemon/internal/store/session_title_test.go @@ -116,8 +116,8 @@ func TestRenameSurvivesReprojection(t *testing.T) { State: domain.SessionRunning, }}, }} - if err := s.ReplaceWorkstreams("p1", nil, reprojected); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, reprojected, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if got := sessionTitle(t, s, "sess-1"); got != "Port the CLI" { @@ -156,11 +156,11 @@ func TestResetSessionTitle(t *testing.T) { } // The provider's title returns with the next cycle — that is when the provider // next says what it is. - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/tmp/p1", Sessions: []domain.Session{{ID: "sess-1", Title: "claude session", Agent: "claude", Provider: "enclave"}}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if got := sessionTitle(t, s, "sess-1"); got != "claude session" { t.Errorf("title after a reset = %q; want the provider's own title back", got) @@ -189,11 +189,11 @@ func TestReprojectionForgetsTitlesOfVanishedSessions(t *testing.T) { t.Fatalf("RenameSession: %v", err) } // The container is torn down: the provider reports nothing for this directory. - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/tmp/p1", Sessions: []domain.Session{}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if len(s.sessionTitles) != 0 { t.Errorf("sessionTitles = %+v; want the vanished session's name forgotten", s.sessionTitles) @@ -202,11 +202,11 @@ func TestReprojectionForgetsTitlesOfVanishedSessions(t *testing.T) { // A session that later reappears under the same ID (a provider that had simply // gone quiet is a different case — the reconciler retains those) is a new // session as far as naming goes. - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/tmp/p1", Sessions: []domain.Session{{ID: "sess-1", Title: "claude session", Agent: "claude", Provider: "enclave"}}, - }}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + }}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if got := sessionTitle(t, s, "sess-1"); got != "claude session" { t.Errorf("title = %q; want the provider's title for a session that came back", got) @@ -235,8 +235,8 @@ func TestReprojectionKeepsOtherProjectsTitles(t *testing.T) { t.Fatalf("RenameSession: %v", err) } // p1 re-projects with no sessions at all; p2's name must be untouched. - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{}); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{}, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } if got := sessionTitle(t, s, "sess-2"); got != "the other project" { t.Errorf("other project's title = %q; want it kept", got) @@ -411,3 +411,62 @@ func TestRenameSessionAllowsItsOwnTitle(t *testing.T) { t.Errorf("renaming a session to its own title: %v", err) } } + +// Project hands out one project as Projects does: a copy, down to its sessions. +func TestProjectReturnsACopyOfOneProject(t *testing.T) { + s := titledStore(t) + got, err := s.Project("p1") + if err != nil { + t.Fatal(err) + } + got.Workstreams[0].Sessions[0].Title = "scribbled" + if title := sessionTitle(t, s, "sess-1"); title != "claude session" { + t.Errorf("a caller's edit reached the store: title %q", title) + } + if _, err := s.Project("nope"); err == nil { + t.Error("Project of an unknown ID succeeded, want an error") + } +} + +// A projection computed before a workstream was removed does not bring it +// back; the one after that decides alone. +func TestReplaceProjectionDoesNotRestoreAWorkstreamRemovedMeanwhile(t *testing.T) { + s := titledStore(t) + inFlight := s.Projects()[0].Workstreams + if err := s.RemoveWorkstream("p1", "ws-1"); err != nil { + t.Fatal(err) + } + if err := s.ReplaceProjection("p1", nil, inFlight, nil); err != nil { + t.Fatal(err) + } + if got := s.Projects()[0].Workstreams; len(got) != 0 { + t.Fatalf("workstreams = %+v, want the removed one not brought back", got) + } + if err := s.ReplaceProjection("p1", nil, s.Projects()[0].Workstreams, nil); err != nil { + t.Fatal(err) + } + if err := s.AddWorkstream("p1", domain.Workstream{ID: "ws-1", Name: "ws", WorkingDir: "/tmp/p1"}); err != nil { + t.Fatal(err) + } + if err := s.ReplaceProjection("p1", nil, s.Projects()[0].Workstreams, nil); err != nil { + t.Fatal(err) + } + if got := s.Projects()[0].Workstreams; len(got) != 1 { + t.Errorf("workstreams = %+v, want one listed again kept", got) + } +} + +// A projection computed before a workstream was added does not drop it. +func TestReplaceProjectionKeepsAWorkstreamAddedMeanwhile(t *testing.T) { + s := titledStore(t) + inFlight := s.Projects()[0].Workstreams + if err := s.AddWorkstream("p1", domain.Workstream{ID: "ws-2", Name: "two", WorkingDir: "/tmp/p1-two"}); err != nil { + t.Fatal(err) + } + if err := s.ReplaceProjection("p1", nil, inFlight, nil); err != nil { + t.Fatal(err) + } + if got := s.Projects()[0].Workstreams; len(got) != 2 { + t.Errorf("workstreams = %+v, want ws-2, added meanwhile, kept", got) + } +} diff --git a/daemon/internal/store/store.go b/daemon/internal/store/store.go index 6949241..f881de0 100644 --- a/daemon/internal/store/store.go +++ b/daemon/internal/store/store.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "iter" "log/slog" "maps" "reflect" @@ -30,7 +31,7 @@ var ErrDuplicateWorkingDir = errors.New("working directory taken") // same project already answers to. Per project, trimmed and case-insensitive. // // It guards the doors a *user* comes through (AddWorkstream, UpdateWorkstream) -// and not ReplaceWorkstreams: an adopted workstream is named after its +// and not ReplaceProjection: an adopted workstream is named after its // directory's base, so two discovered directories can collide, and refusing to // project one would hide a live session behind a label clash. var ErrDuplicateName = errors.New("a workstream with this name already exists in this project") @@ -53,7 +54,7 @@ const PlaceholderIDPrefix = "creating-" // StartingIDPrefix marks the placeholder session the daemon holds while a provider // brings the real one up. Two of the store's rules turn on it, both in -// ReplaceWorkstreams: a placeholder is never written to disk, and a projection +// ReplaceProjection: a placeholder is never written to disk, and a projection // that does not know about one must not drop it. provider.StartingIDPrefix is // defined as this. const StartingIDPrefix = "starting-" @@ -164,12 +165,18 @@ type Store struct { // ID (launchconfig.go). Loaded once at startup; written on create and restart. launchConfigs map[string]persist.LaunchConfig // unprojectedPromotions are the sessions and workstreams promoted from a - // placeholder since their project was last re-projected, keyed by their ID to - // their project's. The projection in flight when one was promoted may predate - // it, so the next swap carries it over (retainInFlightLocked) rather than - // dropping it — and a session what it was started with, a workstream the name - // it holds; the swap after that decides. + // placeholder, and the workstreams added, since their project was last + // re-projected, keyed by their ID to their project's. The projection in + // flight when one was promoted or added may predate it, so the next swap + // carries it over (retainInFlightLocked) rather than dropping it — and a + // session what it was started with, a workstream the name it holds; the swap + // after that decides. unprojectedPromotions map[string]string + // unprojectedRemovals are the workstreams removed since their project was + // last re-projected, keyed by their ID to their project's. The projection in + // flight when one was removed may still hold it, so the next swap drops it + // rather than bringing it back. + unprojectedRemovals map[string]string // now is the clock that stamps Session.EndedAt; tests replace it. now func() time.Time // unobserved are the IDs of sessions restored from disk that no projection has @@ -192,6 +199,7 @@ func New(projects []domain.Project, saver persist.Saver, opts ...Option) *Store sessionTitles: map[string]persist.CustomTitle{}, launchConfigs: map[string]persist.LaunchConfig{}, unprojectedPromotions: map[string]string{}, + unprojectedRemovals: map[string]string{}, now: time.Now, unobserved: map[string]bool{}, } @@ -216,11 +224,10 @@ func Restore(snapshot persist.Snapshot, saver persist.Saver, opts ...Option) *St // projection the reconciler has not run yet. No lock is taken (or needed), // nothing else holds this store until it is returned. for i := range restored.projects { - for j := range restored.projects[i].Workstreams { - restored.applyTitlesLocked(restored.projects[i].Workstreams[j].Sessions) - for _, session := range restored.projects[i].Workstreams[j].Sessions { - restored.unobserved[session.ID] = true - } + sessions := projectSessions(&restored.projects[i]) + restored.applyTitlesLocked(sessions) + for _, session := range sessions { + restored.unobserved[session.ID] = true } } return restored @@ -300,16 +307,48 @@ func (s *Store) RetryUnsavedWrite() { } // applyTitlesLocked overwrites each session's provider-reported title with the -// user's, where one was set. It edits the slice in place, so callers pass the +// user's, where one was set. It edits the sessions in place, so callers pass the // sessions they are about to store rather than a copy handed to a reader. -func (s *Store) applyTitlesLocked(sessions []domain.Session) { - for i := range sessions { - if custom, ok := s.sessionTitles[sessions[i].ID]; ok { - sessions[i].Title = custom.Title +func (s *Store) applyTitlesLocked(sessions sessionWalk) { + for _, session := range sessions { + if custom, ok := s.sessionTitles[session.ID]; ok { + session.Title = custom.Title + } + } +} + +// sessionWalk visits sessions, each with the ID of the workstream listing it. +type sessionWalk = iter.Seq2[string, *domain.Session] + +// sessionsOf walks every session a project holds: each workstream's, then the +// ones hidden on each untracked worktree, whose workstream ID is "". It yields +// the slices' own sessions, so a caller may edit them in place. Every walk that +// decides whether a session is listed goes through here: a hidden session is +// listed as much as one a workstream shows. +func sessionsOf(workstreams []domain.Workstream, untracked []domain.UntrackedWorktree) sessionWalk { + return func(yield func(string, *domain.Session) bool) { + for i := range workstreams { + for j := range workstreams[i].Sessions { + if !yield(workstreams[i].ID, &workstreams[i].Sessions[j]) { + return + } + } + } + for i := range untracked { + for j := range untracked[i].Sessions { + if !yield("", &untracked[i].Sessions[j]) { + return + } + } } } } +// projectSessions is sessionsOf a project's workstreams and suggestions. +func projectSessions(project *domain.Project) sessionWalk { + return sessionsOf(project.Workstreams, project.UntrackedWorktrees) +} + // Option configures a Store at creation time. type Option func(*Store) @@ -379,16 +418,39 @@ func (s *Store) broadcastLocked() { func (s *Store) Projects() []domain.Project { s.mu.Lock() defer s.mu.Unlock() - out := slices.Clone(s.projects) - for i := range out { - out[i].Workstreams = slices.Clone(out[i].Workstreams) - for j := range out[i].Workstreams { - out[i].Workstreams[j] = cloneWorkstream(out[i].Workstreams[j]) - } + out := make([]domain.Project, len(s.projects)) + for i, project := range s.projects { + out[i] = cloneProject(project) } return out } +// Project returns a copy of one project, like Projects. +func (s *Store) Project(projectID string) (domain.Project, error) { + s.mu.Lock() + defer s.mu.Unlock() + for _, project := range s.projects { + if project.ID == projectID { + return cloneProject(project), nil + } + } + return domain.Project{}, fmt.Errorf("project %q not found", projectID) +} + +// cloneProject gives a project its own workstream and suggestion arrays, down +// to their sessions. +func cloneProject(project domain.Project) domain.Project { + project.Workstreams = slices.Clone(project.Workstreams) + for i := range project.Workstreams { + project.Workstreams[i] = cloneWorkstream(project.Workstreams[i]) + } + project.UntrackedWorktrees = slices.Clone(project.UntrackedWorktrees) + for i := range project.UntrackedWorktrees { + project.UntrackedWorktrees[i].Sessions = slices.Clone(project.UntrackedWorktrees[i].Sessions) + } + return project +} + // Workstream returns a copy of one workstream, down to its sessions. func (s *Store) Workstream(projectID, workstreamID string) (domain.Workstream, error) { s.mu.Lock() @@ -407,15 +469,24 @@ func cloneWorkstream(workstream domain.Workstream) domain.Workstream { return workstream } -// ReplaceWorkstreams swaps a project's workstreams with the given set, -// recomputes derived state, and persists and broadcasts only when the set -// actually changed (a reconcile wave would otherwise overflow a slow SSE -// subscriber). read is the project's workstreams as the set was derived from -// them: what changed in the store since then is kept (keepChangesSinceRead). -// The store takes ownership of workstreams: callers must not reuse it. -func (s *Store) ReplaceWorkstreams(projectID string, read, workstreams []domain.Workstream) error { +// ReplaceProjection swaps a project's workstreams and untracked worktree +// suggestions with the given sets, recomputes derived state, and persists and +// broadcasts only when they actually changed (a reconcile wave would otherwise +// overflow a slow SSE subscriber). read is the project's workstreams as the +// sets were derived from them: what changed in the store since then is kept +// (keepChangesSinceRead). The store takes ownership of both sets: callers must +// not reuse them. The sessions of a suggestion count as listed: their titles +// and launch configs are kept, and they are stamped when they end. +func (s *Store) ReplaceProjection(projectID string, read, workstreams []domain.Workstream, + untracked []domain.UntrackedWorktree) error { s.mu.Lock() defer s.mu.Unlock() + return s.replaceProjectionLocked(projectID, read, workstreams, untracked) +} + +// replaceProjectionLocked is ReplaceProjection with s.mu held. +func (s *Store) replaceProjectionLocked(projectID string, read, workstreams []domain.Workstream, + untracked []domain.UntrackedWorktree) error { for i := range s.projects { if s.projects[i].ID != projectID { continue @@ -423,30 +494,37 @@ func (s *Store) ReplaceWorkstreams(projectID string, read, workstreams []domain. if workstreams == nil { workstreams = []domain.Workstream{} } - workstreams = keepChangesSinceRead(read, s.projects[i].Workstreams, workstreams) + workstreams = keepChangesSinceRead(read, s.projects[i].Workstreams, workstreams, untracked) // Anything the daemon started tracking while this projection was being // computed is missing from it, and must survive the swap. - workstreams = retainInFlightLocked(s.projects[i].Workstreams, workstreams, s.unprojectedPromotions) - s.forgetPromotionsLocked(projectID) + workstreams = slices.DeleteFunc(workstreams, func(workstream domain.Workstream) bool { + return s.unprojectedRemovals[workstream.ID] == projectID + }) + workstreams = retainInFlightLocked(s.projects[i].Workstreams, workstreams, untracked, s.unprojectedPromotions) + untracked = handOverTakenSuggestions(workstreams, untracked) + s.forgetUnprojectedLocked(projectID) + previousSessions := projectSessions(&s.projects[i]) + incomingSessions := sessionsOf(workstreams, untracked) // The providers just reported these sessions under their own titles; a // session the user renamed keeps the name they gave it. Applied before the // snapshot is written so the projection and the file agree. - for j := range workstreams { - s.applyTitlesLocked(workstreams[j].Sessions) - } - keepPlaceholderLinks(s.projects[i].Workstreams, workstreams) - s.stampEndedAtLocked(s.projects[i].Workstreams, workstreams) - listed := sessionIDsIn(workstreams) - s.forgetUnobservedLocked(s.projects[i].Workstreams, listed) + s.applyTitlesLocked(incomingSessions) + keepPlaceholderLinks(previousSessions, incomingSessions) + s.stampEndedAtLocked(previousSessions, incomingSessions) + listed := sessionIDsIn(incomingSessions) + s.forgetUnobservedLocked(previousSessions, listed) pruned := s.pruneTitlesLocked(projectID, listed) pruned = s.pruneLaunchConfigsLocked(projectID, listed) || pruned // Held for the change check below. Recompute normalizes derived fields, so // the comparison has to be made between two recomputed sets; it mutates the - // new slice only, leaving this one to compare against. + // new slices only, leaving these to compare against. previous := s.projects[i].Workstreams + previousUntracked := s.projects[i].UntrackedWorktrees s.projects[i].Workstreams = workstreams + s.projects[i].UntrackedWorktrees = untracked s.projects[i].Recompute() - if pruned || !reflect.DeepEqual(previous, s.projects[i].Workstreams) { + if pruned || !reflect.DeepEqual(previous, s.projects[i].Workstreams) || + !sameSuggestions(previousUntracked, s.projects[i].UntrackedWorktrees) { // Placeholders (workstream and session) are kept out of the snapshot by // snapshotLocked, so the projection carrying them through every cycle // cannot persist the very records a restart has to forget. An unchanged @@ -459,6 +537,15 @@ func (s *Store) ReplaceWorkstreams(projectID string, read, workstreams []domain. return fmt.Errorf("project %q not found", projectID) } +// sameSuggestions reports whether two sets of suggestions look the same to a +// client: the sessions hidden on them are not shown, only how many are live. +func sameSuggestions(previous, current []domain.UntrackedWorktree) bool { + return slices.EqualFunc(previous, current, func(was, now domain.UntrackedWorktree) bool { + return was.Name == now.Name && was.Branch == now.Branch && was.Path == now.Path && + was.SessionCount == now.SessionCount + }) +} + // stampEndedAtLocked sets Session.EndedAt on the incoming sessions from what the // previous projection knew of them. A session that was live and is now terminal // ended just now; one that stays terminal keeps its recorded time; a live one has @@ -469,45 +556,43 @@ func (s *Store) ReplaceWorkstreams(projectID string, read, workstreams []domain. // A restored session stays unobserved until a projection brings it changed: the // reconciler carries a session its provider did not report over as the stored // copy, which is still the restored idle placeholder and says nothing about it. -func (s *Store) stampEndedAtLocked(previous, incoming []domain.Workstream) { - before := sessionsByID(previous) +func (s *Store) stampEndedAtLocked(previous, incoming sessionWalk) { + before := map[string]domain.Session{} + for _, session := range previous { + before[session.ID] = *session + } now := s.now() - for _, workstream := range incoming { - for j := range workstream.Sessions { - session := &workstream.Sessions[j] - prev, known := before[session.ID] - first := s.unobserved[session.ID] - if first && known && sameReport(prev, *session) { - continue - } - delete(s.unobserved, session.ID) - var endedAt *time.Time - switch { - case !session.State.Terminal(): - case !known: - // First seen already ended: a time the provider reports stands. - endedAt = session.EndedAt - case first || prev.EndedAt != nil: - // After a restart the persisted time (nil if none) stands: the first - // look cannot tell when the session ended, and now() would be a lie. - endedAt = prev.EndedAt - case !prev.State.Terminal(): - stamp := now - endedAt = &stamp - } - session.EndedAt = endedAt + for _, session := range incoming { + prev, known := before[session.ID] + first := s.unobserved[session.ID] + if first && known && sameReport(prev, *session) { + continue + } + delete(s.unobserved, session.ID) + var endedAt *time.Time + switch { + case !session.State.Terminal(): + case !known: + // First seen already ended: a time the provider reports stands. + endedAt = session.EndedAt + case first || prev.EndedAt != nil: + // After a restart the persisted time (nil if none) stands: the first + // look cannot tell when the session ended, and now() would be a lie. + endedAt = prev.EndedAt + case !prev.State.Terminal(): + stamp := now + endedAt = &stamp } + session.EndedAt = endedAt } } // forgetUnobservedLocked drops the restored sessions of previous that are no // longer listed from the unobserved set: one that comes back is first seen anew. -func (s *Store) forgetUnobservedLocked(previous []domain.Workstream, listed map[string]bool) { - for _, workstream := range previous { - for _, session := range workstream.Sessions { - if !listed[session.ID] { - delete(s.unobserved, session.ID) - } +func (s *Store) forgetUnobservedLocked(previous sessionWalk, listed map[string]bool) { + for _, session := range previous { + if !listed[session.ID] { + delete(s.unobserved, session.ID) } } } @@ -539,18 +624,19 @@ func sameReport(stored, incoming domain.Session) bool { // whoever owns it (Service.finishCreate, Service.finishCreateSession) ends its // life by promoting or removing it in the live state first. // -// The one exception is a session or workstream promoted from a placeholder since -// the last swap (unprojected): the projection may have been computed before it -// existed, so its absence says nothing yet. It is carried through this one swap — -// and with a session its launch config, which pruning would otherwise delete for -// good — and the next projection, computed after the promotion, settles it like -// any other. +// The one exception is a session or workstream promoted from a placeholder, or a +// workstream added (AddWorkstream), since the last swap (unprojected): the +// projection may have been computed before it existed, so its absence says +// nothing yet. It is carried through this one swap — and with a session its +// launch config, which pruning would otherwise delete for good — and the next +// projection, computed after it was promoted or added, settles it like any other. // // The live state is the authority on placeholders the other way too: a starting // placeholder the projection carries from its snapshot but the live state no // longer holds was promoted or removed meanwhile, and is dropped rather than // brought back as a row that never finishes starting. -func retainInFlightLocked(live, next []domain.Workstream, unprojected map[string]string) []domain.Workstream { +func retainInFlightLocked(live, next []domain.Workstream, nextUntracked []domain.UntrackedWorktree, + unprojected map[string]string) []domain.Workstream { next = settlePlaceholders(live, next) starting := map[string]bool{} for _, was := range live { @@ -567,16 +653,16 @@ func retainInFlightLocked(live, next []domain.Workstream, unprojected map[string }) byID[next[i].ID] = i } - // A session is retained only where no workstream lists it: one the projection - // placed elsewhere would otherwise be listed twice. - listed := sessionIDsIn(next) + // A session is retained only where the projection lists it nowhere: one it + // placed elsewhere, or hid on a suggestion, would otherwise be listed twice. + listed := sessionIDsIn(sessionsOf(next, nextUntracked)) for _, was := range live { i, ok := byID[was.ID] _, promoted := unprojected[was.ID] if !ok && (strings.HasPrefix(was.ID, PlaceholderIDPrefix) || promoted) { byID[was.ID] = len(next) next = append(next, was) - maps.Copy(listed, sessionIDsIn([]domain.Workstream{was})) + maps.Copy(listed, sessionIDsIn(sessionsOf([]domain.Workstream{was}, nil))) continue } if !ok || strings.HasPrefix(was.ID, PlaceholderIDPrefix) { @@ -610,13 +696,14 @@ func retainInFlightLocked(live, next []domain.Workstream, unprojected map[string // // Placeholders are left to retainInFlightLocked, and a nil read (no read to // compare with) keeps the projection as it is. -func keepChangesSinceRead(read, live, next []domain.Workstream) []domain.Workstream { +func keepChangesSinceRead(read, live, next []domain.Workstream, + untracked []domain.UntrackedWorktree) []domain.Workstream { if read == nil { return next } readWorkstreams, liveWorkstreams := workstreamsByID(read), workstreamsByID(live) readSessions, liveSessions := sessionsByID(read), sessionsByID(live) - listed := sessionIDsIn(next) + listed := sessionIDsIn(sessionsOf(next, untracked)) kept := make([]domain.Workstream, 0, len(next)) for _, workstream := range next { was, wasRead := readWorkstreams[workstream.ID] @@ -710,14 +797,37 @@ func sessionsByID(workstreams []domain.Workstream) map[string]domain.Session { return byID } -// forgetPromotionsLocked drops a project's unprojected promotions, once a -// projection has been swapped in over them. -func (s *Store) forgetPromotionsLocked(projectID string) { +// handOverTakenSuggestions drops each suggestion whose folder a workstream +// works in, and gives that workstream the sessions hidden there. Why: a +// projection computed before a workstream was added or promoted in a folder +// may still suggest it, and the view must not show both. +func handOverTakenSuggestions(workstreams []domain.Workstream, + untracked []domain.UntrackedWorktree) []domain.UntrackedWorktree { + return slices.DeleteFunc(untracked, func(suggestion domain.UntrackedWorktree) bool { + i := slices.IndexFunc(workstreams, func(workstream domain.Workstream) bool { + return domain.SameFolder(workstream.WorkingDir, suggestion.Path) + }) + if i < 0 { + return false + } + for _, session := range suggestion.Sessions { + if !hasSession(workstreams[i].Sessions, session.ID) { + workstreams[i].Sessions = append(workstreams[i].Sessions, session) + } + } + return true + }) +} + +// forgetUnprojectedLocked drops a project's unprojected promotions and +// removals, once a projection has been swapped in over them. +func (s *Store) forgetUnprojectedLocked(projectID string) { for id, promotedIn := range s.unprojectedPromotions { if promotedIn == projectID { delete(s.unprojectedPromotions, id) } } + maps.DeleteFunc(s.unprojectedRemovals, func(_, removedFrom string) bool { return removedFrom == projectID }) } // settlePlaceholders replaces each creation placeholder a projection carries from @@ -754,20 +864,16 @@ func settlePlaceholders(live, next []domain.Workstream) []domain.Workstream { // keepPlaceholderLinks carries each live session's PlaceholderID onto the // projection's entry for it, which its provider reports without one. -func keepPlaceholderLinks(live, next []domain.Workstream) { +func keepPlaceholderLinks(live, next sessionWalk) { links := map[string]string{} - for _, workstream := range live { - for _, sess := range workstream.Sessions { - if sess.PlaceholderID != "" { - links[sess.ID] = sess.PlaceholderID - } + for _, session := range live { + if session.PlaceholderID != "" { + links[session.ID] = session.PlaceholderID } } - for _, workstream := range next { - for j := range workstream.Sessions { - if link := links[workstream.Sessions[j].ID]; link != "" && workstream.Sessions[j].PlaceholderID != link { - workstream.Sessions[j].PlaceholderID = link - } + for _, session := range next { + if link := links[session.ID]; link != "" { + session.PlaceholderID = link } } } @@ -782,13 +888,11 @@ func hasSession(sessions []domain.Session, id string) bool { return false } -// sessionIDsIn is the set of the IDs of the sessions workstreams list. -func sessionIDsIn(workstreams []domain.Workstream) map[string]bool { +// sessionIDsIn is the set of the IDs of sessions. +func sessionIDsIn(sessions sessionWalk) map[string]bool { ids := map[string]bool{} - for _, workstream := range workstreams { - for _, listed := range workstream.Sessions { - ids[listed.ID] = true - } + for _, session := range sessions { + ids[session.ID] = true } return ids } @@ -1012,6 +1116,7 @@ func (s *Store) AddWorkstream(projectID string, workstream domain.Workstream) er return err } s.projects[i].Workstreams = append(s.projects[i].Workstreams, workstream) + s.unprojectedPromotions[workstream.ID] = projectID s.projects[i].Recompute() s.persistLocked() s.broadcastLocked() @@ -1107,6 +1212,7 @@ func (s *Store) RemoveWorkstream(projectID, workstreamID string) error { s.forgetLaunchConfigLocked(sess.ID) } s.projects[i].Workstreams = append(s.projects[i].Workstreams[:j], s.projects[i].Workstreams[j+1:]...) + s.unprojectedRemovals[workstreamID] = projectID s.projects[i].Recompute() s.persistLocked() s.broadcastLocked() @@ -1455,10 +1561,15 @@ func (s *Store) RenameProject(projectID, name string) error { // SessionHome is a project, and a workstream in it, that lists a session. type SessionHome struct { - ProjectID string + ProjectID string + // WorkstreamID is "" for a session hidden on an untracked worktree. WorkstreamID string } +// Hidden reports whether the project only hides the session on an untracked +// worktree: no workstream there lists it. +func (home SessionHome) Hidden() bool { return home.WorkstreamID == "" } + // RemoveProject deletes a project and broadcasts the new state. The // underlying directory is not touched. It returns where each of the project's // sessions another project still lists moved to (rehomeSessionRecordsLocked). @@ -1470,7 +1581,7 @@ func (s *Store) RemoveProject(projectID string) (map[string]SessionHome, error) removed := s.projects[i] s.projects = append(s.projects[:i], s.projects[i+1:]...) rehomed := s.rehomeSessionRecordsLocked(removed) - s.forgetPromotionsLocked(projectID) + s.forgetUnprojectedLocked(projectID) s.persistLocked() s.broadcastLocked() return rehomed, nil @@ -1488,11 +1599,9 @@ func (s *Store) RemoveProject(projectID string) (map[string]SessionHome, error) // persists. func (s *Store) rehomeSessionRecordsLocked(removed domain.Project) map[string]SessionHome { listedBy := map[string]SessionHome{} - for _, project := range s.projects { - for _, workstream := range project.Workstreams { - for _, listed := range workstream.Sessions { - listedBy[listed.ID] = SessionHome{ProjectID: project.ID, WorkstreamID: workstream.ID} - } + for i := range s.projects { + for workstreamID, listed := range projectSessions(&s.projects[i]) { + listedBy[listed.ID] = SessionHome{ProjectID: s.projects[i].ID, WorkstreamID: workstreamID} } } rehomed := map[string]SessionHome{} @@ -1503,10 +1612,8 @@ func (s *Store) rehomeSessionRecordsLocked(removed domain.Project) map[string]Se } return home, listed } - for _, workstream := range removed.Workstreams { - for _, listed := range workstream.Sessions { - rehome(listed.ID) - } + for _, listed := range projectSessions(&removed) { + rehome(listed.ID) } for sessionID, custom := range s.sessionTitles { if custom.ProjectID != removed.ID { diff --git a/daemon/internal/store/store_test.go b/daemon/internal/store/store_test.go index d9ad361..24db944 100644 --- a/daemon/internal/store/store_test.go +++ b/daemon/internal/store/store_test.go @@ -681,7 +681,7 @@ func TestRefreshGitBackedRejectsUnknownID(t *testing.T) { t.Error("unknown project not rejected") } } -func TestReplaceWorkstreamsPreservesSessionProviderData(t *testing.T) { +func TestReplaceProjectionPreservesSessionProviderData(t *testing.T) { providerData := json.RawMessage(`{"pid":42}`) seed := []domain.Project{{ ID: "p1", @@ -706,7 +706,7 @@ func TestReplaceWorkstreamsPreservesSessionProviderData(t *testing.T) { // Replace workstreams with the same set — simulates a refresh cycle. replacement := seed[0].Workstreams - if err := store.ReplaceWorkstreams("p1", nil, replacement); err != nil { + if err := store.ReplaceProjection("p1", nil, replacement, nil); err != nil { t.Fatal(err) } @@ -760,12 +760,12 @@ func TestSnapshotHoldsRealWorkstreamsAndNoPlaceholders(t *testing.T) { same := func() []domain.Workstream { return []domain.Workstream{cloneWorkstream(real), {ID: "ws-2", Name: "other", Provider: "core", StrategyID: "in-place", WorkingDir: "/p1/other"}} } - if err := s.ReplaceWorkstreams("p1", nil, same()); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", nil, same(), nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } saves := saver.Saves - if err := s.ReplaceWorkstreams("p1", nil, same()); err != nil { - t.Fatalf("ReplaceWorkstreams(again): %v", err) + if err := s.ReplaceProjection("p1", nil, same(), nil); err != nil { + t.Fatalf("ReplaceProjection(again): %v", err) } if saver.Saves != saves { t.Errorf("an unchanged projection saved %d times, want none", saver.Saves-saves) @@ -781,8 +781,8 @@ func TestSnapshotHoldsRealWorkstreamsAndNoPlaceholders(t *testing.T) { } withoutSession := same() withoutSession[0].Sessions = nil - if err := s.ReplaceWorkstreams("p1", nil, withoutSession); err != nil { - t.Fatalf("ReplaceWorkstreams(without session): %v", err) + if err := s.ReplaceProjection("p1", nil, withoutSession, nil); err != nil { + t.Fatalf("ReplaceProjection(without session): %v", err) } if _, ok := saver.Last.SessionTitles["sess-1"]; ok { t.Error("a title whose session is gone survived in the snapshot") @@ -868,8 +868,8 @@ func TestRestoreRoundTrips(t *testing.T) { if err := s.AddProject(&project); err != nil { t.Fatal(err) } - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/alpha", - Sessions: []domain.Session{{ID: "sess-1", Title: "t", Agent: "claude", Provider: "enclave", State: domain.SessionRunning}}}}); err != nil { + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", WorkingDir: "/alpha", + Sessions: []domain.Session{{ID: "sess-1", Title: "t", Agent: "claude", Provider: "enclave", State: domain.SessionRunning}}}}, nil); err != nil { t.Fatal(err) } if err := s.RenameSession("p1", "ws-1", "sess-1", "named"); err != nil { @@ -904,7 +904,7 @@ func endedAtWorkstream(state domain.SessionState) domain.Workstream { // observeEndedAt reports state for s1 and returns the EndedAt the store keeps. func observeEndedAt(t *testing.T, s *Store, state domain.SessionState) *time.Time { t.Helper() - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{endedAtWorkstream(state)}); err != nil { + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{endedAtWorkstream(state)}, nil); err != nil { t.Fatal(err) } return s.Projects()[0].Workstreams[0].Sessions[0].EndedAt @@ -919,7 +919,7 @@ var ( stampClock = time.Date(2026, 6, 7, 8, 9, 10, 0, time.UTC) ) -func TestReplaceWorkstreamsStampsEndedAt(t *testing.T) { +func TestReplaceProjectionStampsEndedAt(t *testing.T) { tests := []struct { name string before domain.SessionState // "": the session is new to this projection @@ -949,7 +949,7 @@ func TestReplaceWorkstreamsStampsEndedAt(t *testing.T) { s.now = func() time.Time { return stampClock } workstream := endedAtWorkstream(tt.after) workstream.Sessions[0].EndedAt = tt.reported - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{workstream}); err != nil { + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{workstream}, nil); err != nil { t.Fatal(err) } if got := s.Projects()[0].Workstreams[0].Sessions[0].EndedAt; !sameTime(got, tt.want) { @@ -973,7 +973,7 @@ func TestEndedAtStampDoesNotMove(t *testing.T) { } select { case event := <-events: - t.Errorf("an identical ReplaceWorkstreams broadcast %q", event.Type) + t.Errorf("an identical ReplaceProjection broadcast %q", event.Type) default: } } @@ -1018,7 +1018,7 @@ func TestRestoredSessionKeepsItsPersistedEnd(t *testing.T) { // stored session as is, with the projection's derived fields filled in. carried := s.Projects()[0].Workstreams carried[0].Sessions[0].Restartable = true - if err := s.ReplaceWorkstreams("p1", nil, carried); err != nil { + if err := s.ReplaceProjection("p1", nil, carried, nil); err != nil { t.Fatal(err) } if got := observeEndedAt(t, s, domain.SessionStopped); !sameTime(got, &persistedEnd) { @@ -1041,7 +1041,7 @@ func TestRestoredSessionKeepsItsPersistedEnd(t *testing.T) { // A projection is derived from a read of the store. What changed in the store // since that read is newer than the projection and is kept; what nobody changed // is the projection's to settle. -func TestReplaceWorkstreamsKeepsChangesSinceTheRead(t *testing.T) { +func TestReplaceProjectionKeepsChangesSinceTheRead(t *testing.T) { seed := func() []domain.Project { return []domain.Project{{ID: "p1", Path: "/p1", Workstreams: []domain.Workstream{{ ID: "ws1", Name: "feature", Branch: "feature", WorkingDir: "/p1", StatusMessage: "Restarting failed", @@ -1164,8 +1164,8 @@ func TestReplaceWorkstreamsKeepsChangesSinceTheRead(t *testing.T) { projected := s.Projects()[0].Workstreams projected[0].Name, projected[0].Branch = "main", "main" tt.change(t, s) - if err := s.ReplaceWorkstreams("p1", read, projected); err != nil { - t.Fatalf("ReplaceWorkstreams: %v", err) + if err := s.ReplaceProjection("p1", read, projected, nil); err != nil { + t.Fatalf("ReplaceProjection: %v", err) } tt.check(t, s) }) diff --git a/daemon/internal/store/untracked_worktree_test.go b/daemon/internal/store/untracked_worktree_test.go new file mode 100644 index 0000000..e1d56f9 --- /dev/null +++ b/daemon/internal/store/untracked_worktree_test.go @@ -0,0 +1,256 @@ +//go:build dev + +package store + +import ( + "encoding/json" + "fmt" + "strings" + "testing" + + "github.com/eclipsesource/homeshell/daemon/internal/domain" +) + +func suggestion(sessions ...domain.Session) domain.UntrackedWorktree { + return domain.UntrackedWorktree{Name: "wt", Branch: "feature", Path: "/tmp/p1-wt", Sessions: sessions} +} + +func TestReplaceProjectionBroadcastsChangedSuggestions(t *testing.T) { + s := titledStore(t) + events, cancel := s.Subscribe() + defer cancel() + + replace := func() { + t.Helper() + workstreams := s.Projects()[0].Workstreams + if err := s.ReplaceProjection("p1", nil, workstreams, []domain.UntrackedWorktree{suggestion()}); err != nil { + t.Fatalf("ReplaceProjection: %v", err) + } + } + replace() + event := receive(t, events) + got := findProject(t, decodeProjects(t, event.Data), "p1").UntrackedWorktrees + if len(got) != 1 || got[0].Path != "/tmp/p1-wt" || got[0].Branch != "feature" { + t.Errorf("event suggestions = %+v, want the one added", got) + } + + replace() + select { + case event := <-events: + t.Errorf("an unchanged projection broadcast: %s", event.Data) + default: + } +} + +func TestReplaceProjectionKeepsHiddenSessionsListed(t *testing.T) { + s := titledStore(t) + if err := s.RenameSession("p1", "ws-1", "sess-1", "Port the CLI"); err != nil { + t.Fatal(err) + } + launch := domain.LaunchConfig{ProviderID: "enclave", OptionID: "claude"} + if err := s.SetLaunchConfig("p1", "sess-1", launch); err != nil { + t.Fatal(err) + } + hidden := domain.Session{ID: "sess-1", Title: "claude session", Agent: "claude", Provider: "enclave"} + if err := s.ReplaceProjection("p1", nil, nil, []domain.UntrackedWorktree{suggestion(hidden)}); err != nil { + t.Fatal(err) + } + if _, ok := s.LaunchConfig("sess-1"); !ok { + t.Error("launch config forgotten while the session is hidden in a suggestion") + } + got := s.Projects()[0].UntrackedWorktrees[0].Sessions[0].Title + if got != "Port the CLI" { + t.Errorf("hidden session title = %q, want the custom one", got) + } + + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{}, nil); err != nil { + t.Fatal(err) + } + if _, ok := s.LaunchConfig("sess-1"); ok { + t.Error("launch config of a session listed nowhere was not pruned") + } +} + +func TestSnapshotLeavesOutSuggestions(t *testing.T) { + s := titledStore(t) + if err := s.ReplaceProjection("p1", nil, s.Projects()[0].Workstreams, + []domain.UntrackedWorktree{suggestion()}); err != nil { + t.Fatal(err) + } + encoded, err := json.Marshal(Saved(t, s)) + if err != nil { + t.Fatal(err) + } + data := string(encoded) + if strings.Contains(data, "untrackedWorktrees") || strings.Contains(data, "/tmp/p1-wt") { + t.Errorf("saved state carries a suggestion: %s", data) + } +} + +// Projects hands out copies: a reader holding one must not race the store's +// setters, which write suggestions and their sessions in place. +func TestProjectsDoesNotShareSuggestionsWithMutations(t *testing.T) { + s := titledStore(t) + hidden := domain.Session{ID: "sess-h", Title: "hidden", State: domain.SessionRunning} + if err := s.ReplaceProjection("p1", nil, s.Projects()[0].Workstreams, + []domain.UntrackedWorktree{suggestion(hidden)}); err != nil { + t.Fatal(err) + } + + done := make(chan struct{}) + go func() { + defer close(done) + for i := 0; i < 200; i++ { + _ = s.RenameSession("p1", "ws-1", "sess-1", fmt.Sprintf("name %d", i)) + project := s.Projects()[0] + _ = s.ReplaceProjection("p1", nil, project.Workstreams, project.UntrackedWorktrees) + } + }() + for i := 0; i < 200; i++ { + for _, project := range s.Projects() { + for _, worktree := range project.UntrackedWorktrees { + _ = worktree.SessionCount + for _, session := range worktree.Sessions { + _ = session.Title + } + } + } + } + <-done + + got := s.Projects()[0] + got.UntrackedWorktrees[0].Sessions[0].Title = "scribbled" + got.UntrackedWorktrees[0].SessionCount = 99 + again := s.Projects()[0].UntrackedWorktrees[0] + if again.Sessions[0].Title != "hidden" || again.SessionCount != 1 { + t.Errorf("a caller's edit reached the store: %+v", again) + } +} + +// Two projects at one folder list the same sessions; one may hide a session +// on a suggestion that the other shows in a workstream. Removing the one that +// shows it moves its records to the one that still lists it. +func TestRemoveProjectRehomesASessionHiddenOnASuggestion(t *testing.T) { + s := titledStore(t) + if err := s.AddProject(&domain.Project{ID: "p2", Name: "other", Path: "/tmp/p2"}); err != nil { + t.Fatal(err) + } + if err := s.RenameSession("p1", "ws-1", "sess-1", "Port the CLI"); err != nil { + t.Fatal(err) + } + launch := domain.LaunchConfig{ProviderID: "enclave", OptionID: "claude"} + if err := s.SetLaunchConfig("p1", "sess-1", launch); err != nil { + t.Fatal(err) + } + hidden := domain.Session{ID: "sess-1", Title: "claude session", State: domain.SessionRunning} + if err := s.ReplaceProjection("p2", nil, nil, []domain.UntrackedWorktree{suggestion(hidden)}); err != nil { + t.Fatal(err) + } + + rehomed, err := s.RemoveProject("p1") + if err != nil { + t.Fatal(err) + } + if home := rehomed["sess-1"]; home != (SessionHome{ProjectID: "p2"}) { + t.Errorf("rehomed to %+v, want the suggestion in p2", home) + } + if got, ok := s.LaunchConfig("sess-1"); !ok || got.OptionID != "claude" { + t.Errorf("launch config = %+v (%v), want kept", got, ok) + } + // p2 now holds the records: its next projection keeps them. + if err := s.ReplaceProjection("p2", nil, nil, []domain.UntrackedWorktree{suggestion(hidden)}); err != nil { + t.Fatal(err) + } + if got := s.Projects()[0].UntrackedWorktrees[0].Sessions[0].Title; got != "Port the CLI" { + t.Errorf("hidden session title = %q, want the custom one", got) + } + if _, ok := s.LaunchConfig("sess-1"); !ok { + t.Error("launch config pruned by the projection of the project it moved to") + } +} + +// A suggestion's sessions are not shown, so a change only to them is not news +// for subscribers; a change to how many are live is. +func TestReplaceProjectionBroadcastsOnlyWhatClientsSee(t *testing.T) { + s := titledStore(t) + running := domain.Session{ID: "sess-h", Title: "first", State: domain.SessionRunning} + replace := func(hidden domain.Session) { + t.Helper() + err := s.ReplaceProjection("p1", nil, s.Projects()[0].Workstreams, []domain.UntrackedWorktree{suggestion(hidden)}) + if err != nil { + t.Fatal(err) + } + } + replace(running) + events, cancel := s.Subscribe() + defer cancel() + + retitled := running + retitled.Title = "second" + replace(retitled) + select { + case event := <-events: + t.Errorf("a change to a hidden session's title broadcast: %s", event.Data) + default: + } + + ended := retitled + ended.State = domain.SessionDone + replace(ended) + event := receive(t, events) + if got := findProject(t, decodeProjects(t, event.Data), "p1").UntrackedWorktrees; got[0].SessionCount != 0 { + t.Errorf("broadcast sessionCount = %d, want 0", got[0].SessionCount) + } +} + +// A session promoted since the last swap is carried through a projection that +// lacks it, but not one that hides it on a suggestion: it would be listed twice. +func TestReplaceProjectionDoesNotCarryAPromotedSessionItHides(t *testing.T) { + s := titledStore(t) + if err := s.AddEphemeralSession("p1", "ws-1", domain.Session{ + ID: StartingIDPrefix + "x", State: domain.SessionStarting, + }); err != nil { + t.Fatal(err) + } + promoted := domain.Session{ID: "sess-new", State: domain.SessionRunning} + if err := s.PromoteEphemeralSession("p1", "ws-1", StartingIDPrefix+"x", promoted, domain.LaunchConfig{}); err != nil { + t.Fatal(err) + } + stale := []domain.Workstream{{ID: "ws-1", Name: "ws", Provider: "core", StrategyID: "in-place", + WorkingDir: "/tmp/p1", Sessions: []domain.Session{}}} + if err := s.ReplaceProjection("p1", nil, stale, []domain.UntrackedWorktree{suggestion(promoted)}); err != nil { + t.Fatal(err) + } + if sessions := s.Projects()[0].Workstreams[0].Sessions; hasSession(sessions, "sess-new") { + t.Errorf("workstream sessions = %+v, want the hidden session left on the suggestion", sessions) + } +} + +// A projection computed before a workstream was added in a folder may still +// suggest that folder. The workstream takes the folder and its hidden sessions, +// so the view never shows both. +func TestReplaceProjectionHandsASuggestionToTheWorkstreamAddedThere(t *testing.T) { + s := titledStore(t) + stale := s.Projects()[0].Workstreams + added := domain.Workstream{ID: "ws-added", Name: "wt", Provider: "core", StrategyID: "worktree", + WorkingDir: "/tmp/p1-wt", Sessions: []domain.Session{}} + if err := s.AddWorkstream("p1", added); err != nil { + t.Fatal(err) + } + hidden := domain.Session{ID: "sess-h", Title: "hidden", State: domain.SessionRunning} + if err := s.ReplaceProjection("p1", nil, stale, []domain.UntrackedWorktree{suggestion(hidden)}); err != nil { + t.Fatal(err) + } + + project := s.Projects()[0] + if len(project.UntrackedWorktrees) != 0 { + t.Errorf("suggestions = %+v, want none beside the workstream added there", project.UntrackedWorktrees) + } + workstream, err := s.Workstream("p1", "ws-added") + if err != nil { + t.Fatal(err) + } + if !hasSession(workstream.Sessions, "sess-h") || len(workstream.Sessions) != 1 { + t.Errorf("added workstream sessions = %+v, want the hidden one", workstream.Sessions) + } +} diff --git a/daemon/internal/store/workstream_name_test.go b/daemon/internal/store/workstream_name_test.go index 3b6774c..eecf90c 100644 --- a/daemon/internal/store/workstream_name_test.go +++ b/daemon/internal/store/workstream_name_test.go @@ -147,10 +147,10 @@ func TestPromoteWorkstreamStillRefusesAnotherWorkstreamsName(t *testing.T) { // directory, which arrives through the projection rather than the user-facing // door and so is not bound by the name rule (the placeholder rides along, // carried over as an in-flight create). - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{{ ID: "ws-other", Name: "feature", Provider: "core", WorkingDir: "/work/other", Sessions: []domain.Session{}, - }}); err != nil { + }}, nil); err != nil { t.Fatalf("projecting the interloper: %v", err) } real := domain.Workstream{ @@ -270,10 +270,10 @@ func TestAPlaceholderMayTakeItsErrorStateUnderATakenName(t *testing.T) { // Stage the state the race produces: the placeholder was admitted, then the // winner landed under the same name, and the placeholder's create has just // failed. - if err := s.ReplaceWorkstreams("p1", nil, []domain.Workstream{ + if err := s.ReplaceProjection("p1", nil, []domain.Workstream{ {ID: "ws-winner", Name: "feature", Provider: "core", WorkingDir: "/work/a", Sessions: []domain.Session{}}, {ID: placeholderID, Name: "feature", Provider: "core", RuntimeState: domain.RuntimeCreating, Sessions: []domain.Session{}}, - }); err != nil { + }, nil); err != nil { t.Fatalf("staging the race: %v", err) } diff --git a/docs/API.md b/docs/API.md index 953a33e..8e2e1cf 100644 --- a/docs/API.md +++ b/docs/API.md @@ -234,6 +234,10 @@ Paths are under `/api`. Endpoints without a response listed answer `204` with no - **Remove** stops tracking the project. Its sessions are left running; in-flight creates and restarts for it are cancelled. - **Refresh** re-detects whether the project is a git repository. +- **Untracked worktrees.** Every project has `untrackedWorktrees: [{name, branch?, path, + sessionCount}]`, the [untracked worktrees](../CONTEXT.md#working-directory) HomeShell suggests + adding. Derived, not stored; refreshed every `providers.refreshIntervalSec`; empty outside git. + `sessionCount` counts the sessions there not ended, hidden until the worktree is added. ### Workstreams @@ -245,6 +249,7 @@ Paths are under `/api/projects/{projectId}`; `{ws}` stands for `/workstreams/{wo | `POST /workstream-draft` | `{providerId, name?, config}` | `{name, config}` | | `POST /workstreams` | `{providerId, name?, config}` | `201`, the `creating` placeholder | | `POST /default-workstream` | | `201`, the default workstream | +| `POST /worktree-workstreams` | `{path}` | `201`, the workstream | | `PATCH {ws}` | `{name}` | | | `DELETE {ws}` | `{keepFiles?, force?}`, or `?force=true` | | | `DELETE {ws}/status` | | | @@ -259,6 +264,10 @@ Paths are under `/api/projects/{projectId}`; `{ws}` stands for `/workstreams/{wo - **Default workstream** gives the project the workstream a new one comes with: core, in place, named like a blank create. It is created at once, with no placeholder. `409` while the project folder is taken. +- **Add an untracked worktree** makes a workstream of the one at `path`, which must be one of the + project's `untrackedWorktrees` paths, else `409`; a blank `path` is `400`. It is a `worktree` + workstream named like the suggestion, numbered (`name-2`, …) when that name is taken, with the + sessions hidden there. Nothing is created on disk. - **Strategies offered.** The core provider's `strategy` property has `enum` and a parallel `enumNames` of display names: `in-place` "In place", `worktree` "Worktree", `copy` "Folder: a copy of the project". While the project folder is taken, `in-place` and its name @@ -273,7 +282,9 @@ Paths are under `/api/projects/{projectId}`; `{ws}` stands for `/workstreams/{wo - **Draft** answers what a create with the same body would derive for its blanks (name, branch, path) and creates nothing. - **Remove** ([Workstream lifecycle](ARCHITECTURE.md#workstream-lifecycle)): `keepFiles` keeps - the directory; a busy workstream needs `force` (see [Errors](#errors)). + the directory, and a worktree kept is suggested again in `untrackedWorktrees`; a busy + workstream needs `force` (see [Errors](#errors)). A workstream already removed is `404`. One being + removed because its worktree vanished is waited for, and the Remove answers `204`. - **Clearing the status** discards the workstream's status message for every client. `409` for a failed create. Why: there the message is the only account of what went wrong. - **Open** runs the default open mode; **open-with** runs `mode`, one of the workstream's diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e824cd3..c667326 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -212,6 +212,7 @@ stateDiagram-v2 Removed --> Workstream: creation finishes anyway Removed --> [*]: creation fails Workstream --> [*]: remove (forced if any session is busy) + Workstream --> [*]: worktree removed outside HomeShell (forced remove, files kept) ``` - **Default workstream.** Adding a project creates its `in-place` workstream directly, with no @@ -226,10 +227,19 @@ stateDiagram-v2 - **Failed create.** The placeholder stays in `error` with the cause; it offers only removal and no longer reserves its name. - **Adopt.** The reconciler makes up a workstream (`disc-…` ID) for sessions a provider reports - in a directory no workstream claims (`provider/reconcile.go` `adoptedWorkstream`). + in a directory no workstream claims (`provider/reconcile.go` `adoptedWorkstream`). A directory + that is an untracked worktree is suggested instead, not adopted: its sessions are hidden on the + suggestion and counted there, and a workstream adopted there earlier is dropped. - **Remove.** `provider.Service.Remove` refuses while a session is busy unless forced, removes each session through its provider, then lets the workstream provider discard the directory. - The reconciler never removes a workstream on its own. + The workstream stays listed, holding its sessions, until its record is dropped at the end. +- **Vanish.** A core `worktree` workstream whose worktree git no longer lists, or whose folder + is deleted, is removed like a forced Remove that keeps files (`Service.retireVanished`): a + pass finds it gone, never on a failed listing, and starts the removal in the background once + the pass has let go of the project, unless a removal of it is under way; a Remove arriving + meanwhile waits for it. Until then it stays listed, so no pass adopts a session in its + folder. Why: a provider would otherwise keep reporting its sessions and they would be adopted + back. ### State aggregation @@ -288,15 +298,36 @@ flowchart TB workstreams as it is now (the branch checked out in its folder); a name it gives that another workstream in the project has is not taken. - **Matching.** Sessions group by cleaned working directory: existing workstreams take theirs, - unclaimed directories get an adopted workstream. A session ID reported again supersedes the - stored one wherever it was. + untracked worktrees hide theirs, other unclaimed directories get an adopted workstream. A + session ID reported again supersedes the stored one wherever it was. +- **Untracked worktrees.** For a git project, each pass has every workstream provider that is a + `FolderSurveyor` survey its folder listing: listed folders that no project and no workstream of + any project holds (adopted ones aside) become the project's `untrackedWorktrees`. + - **Cache.** The listing is cached per project (`listingCache`), so a pass costs no + `git worktree list` while one is held, and it lists before taking the project's lock. Each + refresh tick lists every git project anew, replaces the cache and re-projects when a listing + differs from the one held (`Reconciler.foldersChanged`). Removing a workstream (once its + files are gone) or a project drops the project's cached listing, so a deleted worktree is + not suggested. Removing a workstream, and adding or removing a project, have every project + surveyed again as a tick does (`Reconciler.resurvey`, no session provider asked), so a folder + kept, claimed or freed shows in each project sharing the repository. Adding an untracked + worktree lists anew, replacing the cache, and adds only a folder that listing suggests + (`Reconciler.untracked`, for `Service.AddUntrackedWorktree`). + - **Freshness.** A workstream vanishes only from a listing begun after a pass first saw it; an + older listing that misses it is dropped and listed anew. Why: a listing older than a + worktree workstream does not name its worktree. + - **Failed listing.** It keeps the last suggestions and their hidden sessions, except one whose + folder a workstream took since, and vanishes nothing. Why: git not answering is no proof the + worktrees are gone. + - **Taken meanwhile.** A suggestion for a folder a workstream was added or promoted in while the + pass ran is dropped, and its hidden sessions go to that workstream (`store.ReplaceProjection`). - **Retention.** A stored session nobody reported this pass survives only if its provider is registered and failed to report. Placeholders are always kept. Why: a transient provider error must not flush sessions, but a session nothing can confirm must not linger as a ghost. - Per-project locks serialize passes; the store is re-read after discovery so concurrent creates survive. - **Changes during a pass win.** A pass writes back with the read it started from, and - `store.ReplaceWorkstreams` keeps what changed in the store since (`keepChangesSinceRead`): a + `store.ReplaceProjection` keeps what changed in the store since (`keepChangesSinceRead`): a workstream or session changed meanwhile is taken from the store whole, one removed stays removed, one added stays, and a name the pass gave that another workstream now has is not taken. The next pass brings the kept entries up to date. Why: a rename, a dismissed message, a diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index c751679..21406ea 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -95,7 +95,7 @@ The agents themselves are configured in the [agent catalog](API.md#agent-catalog | Key | Default | Effect | |---|---|---| -| `providers.refreshIntervalSec` | `0.5` | Seconds between checks of session states and branches (0.1–86400) | +| `providers.refreshIntervalSec` | `0.5` | Seconds between checks of session states, branches and worktrees, 0.1–86400 | ### Open with diff --git a/docs/PROVIDERS.md b/docs/PROVIDERS.md index 650329e..c02e7eb 100644 --- a/docs/PROVIDERS.md +++ b/docs/PROVIDERS.md @@ -72,6 +72,7 @@ Capabilities are what clients gate on. Two kinds: | `DraftResolver` | Workstream | Names and fills what a blank create would derive | | `StrategyTeller` | Workstream | A create's strategy, for its placeholder to carry | | `Refresher` | Workstream | Brings a workstream up to date on each projection, e.g. its branch | +| `FolderSurveyor` | Workstream | Lists folders; says which are untracked and which workstreams lost theirs | | `WorkstreamStartStopper` | Workstream | Only the fake has it and the daemon never calls it; `startStop` is false | - Why `ConfigValidator`: JSON Schema cannot state git's branch-name rules, and a rule checked in @@ -181,6 +182,17 @@ strategies ([CONTEXT.md](../CONTEXT.md#working-directory)) through `provider/wor core gave (`coreCreatorData.GivenName`: the branch, or the folder's or a numbered name where the branch was missing or taken) and no other workstream in the project has it; a renamed workstream keeps its name. A detached HEAD clears the branch and keeps the name. +- **Untracked worktrees.** Core is a `FolderSurveyor`: one `git worktree list` per project per + refresh (bounded to 500 ms–1 s; an Add's own listing to 5 s, as a user waits on it). Every entry + but git's first, the main worktree, whose folder is there and that no project or workstream + holds, nor a create in flight, is suggested, named after its branch or, detached, its folder. A + create holds its folder until its workstream is stored (`CreateRequest.HoldClaim`), so a pass in + between does not suggest it. A listing without even the main worktree counts as failed + (`git.LinkedWorktrees`), so it vanishes nothing. A `worktree` workstream vanishes once its folder + is unlisted or deleted. A folder only out of reach (git keeps it registered, e.g. on an unmounted + drive) is not suggested, and its workstream stays. Adding a suggestion (`TrackFolder`) creates + nothing on disk and makes a `worktree` workstream like a created one, its name numbered past + those taken (`name-2`, …). - Removing an `in-place` workstream never deletes the project directory; `worktree` needs a git-backed project. - Managed worktree and copy directories go under the `workstreams.location` setting, read live, From ced1a46910b6a1c3423707b0b08fc34fea5d89d7 Mon Sep 17 00:00:00 2001 From: Philip Langer Date: Thu, 12 Nov 2026 15:53:32 +0100 Subject: [PATCH 02/13] feat(ui): untracked worktrees in the project view A fold under Separate folders lists the untracked worktrees, each with its branch, folder and sessions, and Add makes a workstream of one. The fold and its rows are items of the workstream list: the arrow keys reach them, Right opens the fold, Left closes it or goes back to it, and Enter on a row adds it. Focus on one leaves nothing selected, so that no Alt letter acts on a card or session the user has moved away from. --- webapp/apps/web/src/app/app.spec.tsx | 2 + .../src/app/keyboard/command-palette.spec.tsx | 1 + .../app/keyboard/shortcuts-dialog.spec.tsx | 4 +- webapp/apps/web/src/app/project-requests.ts | 21 ++ webapp/apps/web/src/app/project-view.spec.tsx | 130 +++++++++++- webapp/apps/web/src/app/project-view.tsx | 50 +++-- .../web/src/app/use-session-attention.spec.ts | 1 + webapp/packages/components/src/index.ts | 1 + .../components/src/lib/copyable-path.tsx | 4 + .../components/src/lib/fold.module.css | 29 +++ .../components/src/lib/list-navigation.ts | 16 +- .../components/src/lib/project-list.spec.tsx | 3 + .../src/lib/session-list.module.css | 29 +-- .../components/src/lib/session-list.tsx | 5 +- .../src/lib/untracked-worktrees.module.css | 28 +++ .../src/lib/untracked-worktrees.spec.tsx | 186 ++++++++++++++++++ .../src/lib/untracked-worktrees.tsx | 129 ++++++++++++ webapp/packages/model/src/lib/types.ts | 13 ++ 18 files changed, 601 insertions(+), 51 deletions(-) create mode 100644 webapp/packages/components/src/lib/fold.module.css create mode 100644 webapp/packages/components/src/lib/untracked-worktrees.module.css create mode 100644 webapp/packages/components/src/lib/untracked-worktrees.spec.tsx create mode 100644 webapp/packages/components/src/lib/untracked-worktrees.tsx diff --git a/webapp/apps/web/src/app/app.spec.tsx b/webapp/apps/web/src/app/app.spec.tsx index a722e0c..43bffcb 100644 --- a/webapp/apps/web/src/app/app.spec.tsx +++ b/webapp/apps/web/src/app/app.spec.tsx @@ -56,6 +56,7 @@ const projectsFixture: Project[] = [ state: 'waiting', runtimeState: 'running', effectiveState: 'waiting', + untrackedWorktrees: [], workstreams: [workstream], }, { @@ -66,6 +67,7 @@ const projectsFixture: Project[] = [ state: 'idle', runtimeState: 'idle', effectiveState: 'idle', + untrackedWorktrees: [], workstreams: [], }, ]; diff --git a/webapp/apps/web/src/app/keyboard/command-palette.spec.tsx b/webapp/apps/web/src/app/keyboard/command-palette.spec.tsx index 4823d2a..82bf3cd 100644 --- a/webapp/apps/web/src/app/keyboard/command-palette.spec.tsx +++ b/webapp/apps/web/src/app/keyboard/command-palette.spec.tsx @@ -15,6 +15,7 @@ const projects: Project[] = [ state: 'idle', runtimeState: 'running', effectiveState: 'idle', + untrackedWorktrees: [], workstreams: [ { id: 'w1', diff --git a/webapp/apps/web/src/app/keyboard/shortcuts-dialog.spec.tsx b/webapp/apps/web/src/app/keyboard/shortcuts-dialog.spec.tsx index 7469a4d..7d41b8e 100644 --- a/webapp/apps/web/src/app/keyboard/shortcuts-dialog.spec.tsx +++ b/webapp/apps/web/src/app/keyboard/shortcuts-dialog.spec.tsx @@ -47,12 +47,12 @@ describe('ShortcutsDialog', () => { }); it.each([ - ['Move between workstreams and sessions', '↑↓'], + ['Move between workstreams, sessions and untracked worktrees', '↑↓'], ['Go to the first or last item', 'HomeEnd'], ['Expand, or go to the first session', '→'], ['Collapse, or go to the workstream', '←'], [ - "Open the focused workstream or session, or show an ended one's details", + "Open the focused workstream or session, show an ended one's details, or add an untracked worktree", 'Enter', ], ['Rename the focused workstream or session', 'F2'], diff --git a/webapp/apps/web/src/app/project-requests.ts b/webapp/apps/web/src/app/project-requests.ts index fc84624..3d405ac 100644 --- a/webapp/apps/web/src/app/project-requests.ts +++ b/webapp/apps/web/src/app/project-requests.ts @@ -71,6 +71,27 @@ export async function addDefaultWorkstream( projectId: string, ): Promise { const result = await postWorkstream(projectId, 'default-workstream'); + return refusalMessage(result); +} + +/** + * Gives the project a workstream in the untracked git worktree at `path`; the + * daemon refuses a path that is not one (any more). + */ +export async function addUntrackedWorktree( + projectId: string, + path: string, +): Promise { + const result = await postWorkstream(projectId, 'worktree-workstreams', { + path, + }); + return refusalMessage(result); +} + +/** A refusal as the message it carries, which is all these callers show. */ +function refusalMessage( + result: Workstream | string | Refusal, +): Workstream | string { return typeof result !== 'string' && 'message' in result ? result.message : result; diff --git a/webapp/apps/web/src/app/project-view.spec.tsx b/webapp/apps/web/src/app/project-view.spec.tsx index 0f01d43..e8ffcaa 100644 --- a/webapp/apps/web/src/app/project-view.spec.tsx +++ b/webapp/apps/web/src/app/project-view.spec.tsx @@ -39,6 +39,7 @@ const project: Project = { state: 'running', runtimeState: 'running', effectiveState: 'running', + untrackedWorktrees: [], workstreams: [ { id: 'w1', @@ -153,9 +154,13 @@ const session = ( /** * Answers what the view loads — one creating provider, one session option, * and a provider that can stop and start — and each create with `created`, - * once `answered` has settled. + * once `answered` has settled, or refuses each with `refusal`. */ -function stubCreates(created: () => unknown, answered = Promise.resolve()) { +function stubCreates( + created: () => unknown, + answered = Promise.resolve(), + refusal?: string, +) { vi.stubGlobal( 'fetch', vi.fn(async (url: string, init?: RequestInit) => { @@ -170,6 +175,13 @@ function stubCreates(created: () => unknown, answered = Promise.resolve()) { } if (init?.method === 'POST') { await answered; + if (refusal) { + return { + ok: false, + status: 409, + json: async () => ({ error: refusal }), + }; + } return json(created(), 201); } if (url.endsWith('/workstream-providers')) { @@ -308,6 +320,120 @@ describe('ProjectView sections', () => { ); }); + describe('untracked worktrees', () => { + const untracked = (shown: Project): Project => ({ + ...shown, + untrackedWorktrees: [ + { name: 'wt-a', branch: 'wt-a', path: '/tmp/wt-a', sessionCount: 0 }, + ], + }); + + it('shows them at the end of Separate folders, outside its count', async () => { + stubDaemon(); + await renderLive(untracked(withCards(worktree('w2', 'b')))); + + const separate = section(/^Separate folders · 1$/); + const fold = within(separate).getByRole('button', { + name: '1 untracked worktree', + }); + const cardRegion = within(separate).getByRole('region', { + name: 'Workstream b', + }); + expect( + cardRegion.compareDocumentPosition(fold) & + Node.DOCUMENT_POSITION_FOLLOWING, + ).toBeTruthy(); + }); + + it('posts the path of the one added', async () => { + stubCreates(() => worktree('w3', 'wt-a')); + await renderLive(untracked(withCards(worktree('w2', 'b')))); + + fireEvent.click( + screen.getByRole('button', { name: '1 untracked worktree' }), + ); + fireEvent.click(screen.getByRole('button', { name: 'Add wt-a' })); + + const post = await waitFor(() => { + const found = vi + .mocked(fetch) + .mock.calls.find(([, init]) => init?.method === 'POST'); + expect(found).toBeDefined(); + return found!; + }); + expect(post[0]).toBe('/api/projects/p1/worktree-workstreams'); + expect(JSON.parse(String(post[1]?.body))).toEqual({ path: '/tmp/wt-a' }); + }); + + it('says why the daemon refused an add', async () => { + stubCreates(() => ({}), undefined, 'Not an untracked worktree.'); + await renderLive(untracked(withCards(worktree('w2', 'b')))); + + fireEvent.click( + screen.getByRole('button', { name: '1 untracked worktree' }), + ); + fireEvent.click(screen.getByRole('button', { name: 'Add wt-a' })); + + expect((await screen.findByRole('alert')).textContent).toContain( + 'Not an untracked worktree.', + ); + }); + + it('lets no second add or Work here start while one is in flight', async () => { + let answer: () => void = () => undefined; + stubCreates( + () => worktree('w3', 'wt-a'), + new Promise((resolve) => { + answer = resolve; + }), + ); + await renderLive(untracked(withCards(worktree('w2', 'b')))); + + fireEvent.click( + screen.getByRole('button', { name: '1 untracked worktree' }), + ); + const add = screen.getByRole('button', { name: 'Add wt-a' }); + fireEvent.click(add); + fireEvent.click(add); + expect(add.getAttribute('aria-busy')).toBe('true'); + await act(async () => answer()); + + expect( + vi + .mocked(fetch) + .mock.calls.filter(([, init]) => init?.method === 'POST'), + ).toHaveLength(1); + }); + + it('blocks the adds while Work here is in flight', async () => { + stubCreates(() => card('w1', 'here'), new Promise(() => undefined)); + await renderLive(untracked(withCards())); + + fireEvent.click(screen.getByRole('button', { name: 'Work here' })); + fireEvent.click( + screen.getByRole('button', { name: '1 untracked worktree' }), + ); + fireEvent.click(screen.getByRole('button', { name: 'Add wt-a' })); + + expect( + vi + .mocked(fetch) + .mock.calls.filter(([, init]) => init?.method === 'POST'), + ).toHaveLength(1); + }); + + it('does not say there are no separate folders above a fold of them', async () => { + stubDaemon(); + await renderLive(untracked(withCards())); + + expect( + within(section(/^Separate folders/)).queryByText( + 'No separate folders yet.', + ), + ).toBeNull(); + }); + }); + // One list: the arrows and the tab stop follow the page, not the daemon. it('walks from the project folder into the separate folders', async () => { stubDaemon(); diff --git a/webapp/apps/web/src/app/project-view.tsx b/webapp/apps/web/src/app/project-view.tsx index 6b1fab7..f62aede 100644 --- a/webapp/apps/web/src/app/project-view.tsx +++ b/webapp/apps/web/src/app/project-view.tsx @@ -25,6 +25,7 @@ import { useListTabStop, useRenamableTitle, useReturnFocus, + UntrackedWorktrees, WorkstreamCard, type WorkstreamCardHandle, } from '@homeshell/components'; @@ -50,6 +51,7 @@ import { import { usePendingFocus } from './keyboard/use-pending-focus'; import { addDefaultWorkstream, + addUntrackedWorktree, createSession, createWorkstream, deleteWorkstream, @@ -77,6 +79,9 @@ import { useSettings } from './settings/use-settings'; import { useProjectCommands } from './use-project-commands'; import { useSessionAttention } from './use-session-attention'; +/** What `creating` holds for Work here: a worktree path is never this. */ +const WORK_HERE = 'work-here'; + const LIST_KEYS_HINT = describeListKeys(); export interface ProjectViewProps { @@ -305,18 +310,27 @@ export function ProjectView({ return null; }; - // One create at a time: a second would only be refused, the folder taken. - const [workingHere, setWorkingHere] = useState(false); - const handleWorkHere = async () => { - setWorkingHere(true); + // The one create in flight: the worktree path of an Add, or WORK_HERE. A + // second would only be refused, the folder taken. + const [creating, setCreating] = useState(null); + const createHere = async ( + key: string, + request: () => Promise, + ) => { + if (creating !== null) return; + setCreating(key); try { - const result = await addDefaultWorkstream(project.id); + const result = await request(); if (typeof result === 'string') setError(result); else focusWhenListed(result.id); } finally { - setWorkingHere(false); + setCreating(null); } }; + const handleWorkHere = () => + createHere(WORK_HERE, () => addDefaultWorkstream(project.id)); + const handleAddWorktree = (path: string) => + createHere(path, () => addUntrackedWorktree(project.id, path)); const handleCreateSession = async ( workstreamId: string, @@ -705,8 +719,8 @@ export function ProjectView({ onClick={handleWorkHere} disabled={blocked} // Not disabled: that would drop focus to the page meanwhile. - aria-disabled={workingHere || undefined} - aria-busy={workingHere || undefined} + aria-disabled={creating !== null || undefined} + aria-busy={creating === WORK_HERE || undefined} title={ disabledReason ?? 'Create a workstream in the project folder' } @@ -725,9 +739,10 @@ export function ProjectView({ · {separate.length} )} - {separate.length === 0 && ( -

No separate folders yet.

- )} + {separate.length === 0 && + project.untrackedWorktrees.length === 0 && ( +

No separate folders yet.

+ )} + {expanded && ( +
    + {worktrees.map((worktree, index) => ( +
  • handleRowKeyDown(event, worktree.path)} + > + {worktree.name} + + {worktree.branch ?? 'detached'} + + + {worktree.sessionCount > 0 && ( + + {worktree.sessionCount}{' '} + {worktree.sessionCount === 1 ? 'session' : 'sessions'} + + )} + +
  • + ))} +
+ )} + + ); +} diff --git a/webapp/packages/model/src/lib/types.ts b/webapp/packages/model/src/lib/types.ts index 60aa20d..7741242 100644 --- a/webapp/packages/model/src/lib/types.ts +++ b/webapp/packages/model/src/lib/types.ts @@ -282,6 +282,19 @@ export interface Project { /** Combined display state (runtime + session), derived by the daemon. */ effectiveState: EffectiveState; workstreams: Workstream[]; + /** Git worktrees of the project that no workstream works in, offered for adding. */ + untrackedWorktrees: UntrackedWorktree[]; +} + +/** A git worktree of a project's repository that HomeShell does not track yet. */ +export interface UntrackedWorktree { + /** The name Add would give the workstream: the branch, else the folder's name. */ + name: string; + /** Absent when HEAD is detached. */ + branch?: string; + path: string; + /** Sessions found running in the folder. */ + sessionCount: number; } /** From a1dedd40acf5711dbab01baa5154ee2c453a601a Mon Sep 17 00:00:00 2001 From: Philip Langer Date: Thu, 12 Nov 2026 15:53:32 +0100 Subject: [PATCH 03/13] test(e2e): untracked worktrees over the API and in the project view --- .../src/api/untracked-worktrees.spec.ts | 326 ++++++++++++++++++ .../web-e2e/src/api/workstream-helpers.ts | 56 ++- webapp/apps/web-e2e/src/git-repo.ts | 38 ++ .../apps/web-e2e/src/ui/keyboard-helpers.ts | 18 +- .../src/ui/untracked-worktrees.spec.ts | 90 +++++ 5 files changed, 526 insertions(+), 2 deletions(-) create mode 100644 webapp/apps/web-e2e/src/api/untracked-worktrees.spec.ts create mode 100644 webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts diff --git a/webapp/apps/web-e2e/src/api/untracked-worktrees.spec.ts b/webapp/apps/web-e2e/src/api/untracked-worktrees.spec.ts new file mode 100644 index 0000000..8f1bd8a --- /dev/null +++ b/webapp/apps/web-e2e/src/api/untracked-worktrees.spec.ts @@ -0,0 +1,326 @@ +import { access } from 'node:fs/promises'; +import type { APIRequestContext } from '@playwright/test'; +import { expect, test, tempProjectDir } from '../fixtures'; +import { addWorktree, git, gitProject, tempGitRepo } from '../git-repo'; +import { addProjectWithDefault } from '../projects'; +import { + addUntrackedWorktree, + awaitRealWorkstream, + createProject, + createSession, + createWorkstream, + listedProjects, + pollFor, + projectOf, + sessionsOf, + untrackedOf, + workstreamOf, + type RealWorkstream, +} from './workstream-helpers'; + +/** + * Black-box tests for untracked worktrees: the worktrees of a project's + * repository that HomeShell did not make, which it suggests adding as + * workstreams. The daemon refreshes the suggestions on its refresh interval, + * so every read of them polls. + */ + +/** Polls until the project suggests the worktree at `path`. */ +const awaitSuggested = ( + request: APIRequestContext, + projectId: string, + path: string, +) => + pollFor(`${path} to be suggested`, async () => + (await untrackedOf(request, projectId)).find((w) => w.path === path), + ); + +/** + * A git project with one untracked worktree, made with `options`, once the + * project suggests it. + */ +async function suggestedWorktree( + request: APIRequestContext, + options?: Parameters[1], +): Promise<{ dir: string; projectId: string; path: string }> { + const { dir, projectId } = await gitProject(request, 'hs-untracked-'); + const path = await addWorktree(dir, options); + await awaitSuggested(request, projectId, path); + return { dir, projectId, path }; +} + +/** Adds the untracked worktree at `path`, expecting a 201, and returns the workstream. */ +async function addedWorktree( + request: APIRequestContext, + projectId: string, + path: string, +): Promise { + const res = await addUntrackedWorktree(request, projectId, path); + expect(res.status(), await res.text()).toBe(201); + return res.json(); +} + +/** + * Waits until a refresh has run that started after now: a new worktree is + * suggested only by one. + */ +async function awaitRefresh( + request: APIRequestContext, + projectId: string, + repo: string, +): Promise { + await awaitSuggested(request, projectId, await addWorktree(repo)); +} + +/** Creates a worktree workstream through the core provider and returns it. */ +async function createOwnWorktree( + request: APIRequestContext, + projectId: string, + name: string, +): Promise { + const res = await request.post(`/api/projects/${projectId}/workstreams`, { + data: { providerId: 'core', name, config: { strategy: 'worktree' } }, + }); + expect(res.status(), await res.text()).toBe(201); + return awaitRealWorkstream(request, projectId, 20_000); +} + +/** Every workstream of every project, as listed right now. */ +async function allWorkstreams( + request: APIRequestContext, +): Promise { + return (await listedProjects(request)).flatMap( + (project) => project.workstreams ?? [], + ); +} + +const exists = (path: string) => + access(path).then( + () => true, + () => false, + ); + +test.describe('untracked worktrees API', () => { + test('a worktree added outside HomeShell is suggested, named after its branch', async ({ + request, + }) => { + const { projectId, path } = await suggestedWorktree(request, { + branch: 'fix-login', + }); + + const [suggested] = await untrackedOf(request, projectId); + expect(suggested).toMatchObject({ + path, + name: 'fix-login', + branch: 'fix-login', + sessionCount: 0, + }); + }); + + test('a detached worktree is suggested under its folder name', async ({ + request, + }) => { + const { projectId, path } = await suggestedWorktree(request, { + detach: true, + }); + + const [suggested] = await untrackedOf(request, projectId); + expect(suggested.name).toBe(path.split('/').pop()); + expect(suggested.branch).toBeUndefined(); + }); + + test("the project folder and HomeShell's own worktrees are not suggested", async ({ + request, + }) => { + const { dir, projectId } = await gitProject(request, 'hs-untracked-'); + const own = await createOwnWorktree(request, projectId, 'own'); + // A refresh that has seen this one has seen the others. + const outside = await addWorktree(dir); + await awaitSuggested(request, projectId, outside); + + const paths = (await untrackedOf(request, projectId)).map((w) => w.path); + expect(paths).toEqual([outside]); + expect(paths).not.toContain(own.workingDir); + }); + + test('adding one makes a worktree workstream that follows its branch', async ({ + request, + }) => { + const { projectId, path } = await suggestedWorktree(request, { + branch: 'topic', + }); + + const added = await addedWorktree(request, projectId, path); + expect(added).toMatchObject({ + name: 'topic', + strategyId: 'worktree', + branch: 'topic', + workingDir: path, + }); + await pollFor('the suggestion to go', async () => + (await untrackedOf(request, projectId)).length === 0 ? true : undefined, + ); + + await git(path, 'checkout', '-q', '-b', 'other'); + const followed = await pollFor('the branch to follow', async () => { + const ws = await workstreamOf(request, projectId, added.id); + return ws?.branch === 'other' ? ws : undefined; + }); + expect(followed.name).toBe('topic'); + }); + + test('added under a name already taken, it is numbered', async ({ + request, + }) => { + const { projectId, path } = await suggestedWorktree(request, { + branch: 'taken', + }); + await createWorkstream(request, projectId, 'taken'); + + const added = await addedWorktree(request, projectId, path); + expect(added).toMatchObject({ name: 'taken-2', branch: 'taken' }); + }); + + test('adding a path that is not an untracked worktree is refused', async ({ + request, + }) => { + const { dir, projectId, path } = await suggestedWorktree(request); + + expect((await addUntrackedWorktree(request, projectId, dir)).status()).toBe( + 409, + ); + const elsewhere = await tempProjectDir('hs-untracked-elsewhere-'); + expect( + (await addUntrackedWorktree(request, projectId, elsewhere)).status(), + ).toBe(409); + + expect( + (await addUntrackedWorktree(request, projectId, path)).status(), + ).toBe(201); + expect( + (await addUntrackedWorktree(request, projectId, path)).status(), + ).toBe(409); + }); + + test('a project outside git suggests nothing, and adds nothing', async ({ + request, + }) => { + const projectId = await createProject(request, 'hs-untracked-plain-'); + const elsewhere = await tempProjectDir('hs-untracked-elsewhere-'); + + const res = await addUntrackedWorktree(request, projectId, elsewhere); + expect(res.status()).toBe(409); + expect(await untrackedOf(request, projectId)).toEqual([]); + }); + + test('adding needs a path, and a known project', async ({ request }) => { + const { projectId } = await gitProject(request, 'hs-untracked-'); + for (const path of ['', ' ']) { + const res = await addUntrackedWorktree(request, projectId, path); + expect(res.status()).toBe(400); + } + const unknown = await addUntrackedWorktree(request, 'nope', '/tmp/x'); + expect(unknown.status()).toBe(404); + }); + + test('removed keeping its files, it is suggested again', async ({ + request, + }) => { + const { projectId, path } = await suggestedWorktree(request); + const added = await addedWorktree(request, projectId, path); + + const removed = await request.delete( + `/api/projects/${projectId}/workstreams/${added.id}`, + { data: { keepFiles: true } }, + ); + expect(removed.status()).toBe(204); + + await awaitSuggested(request, projectId, path); + expect(await exists(path)).toBe(true); + }); + + test('removed with its files, it is gone for good', async ({ request }) => { + const { dir, projectId, path } = await suggestedWorktree(request); + const added = await addedWorktree(request, projectId, path); + + const removed = await request.delete( + `/api/projects/${projectId}/workstreams/${added.id}`, + ); + expect(removed.status()).toBe(204); + expect(await exists(path)).toBe(false); + + // A refresh that has seen this one has run since the removal. + const sentinel = await addWorktree(dir); + await awaitSuggested(request, projectId, sentinel); + expect((await untrackedOf(request, projectId)).map((w) => w.path)).toEqual([ + sentinel, + ]); + }); + + test('a worktree workstream disappears with its sessions when its worktree is removed outside', async ({ + request, + }) => { + const { dir, projectId } = await gitProject(request, 'hs-untracked-'); + // First: createOwnWorktree returns the project's first real workstream. + const made = await createOwnWorktree(request, projectId, 'made'); + const path = await addWorktree(dir); + await awaitSuggested(request, projectId, path); + const added = await addedWorktree(request, projectId, path); + + const sessionIds: string[] = []; + for (const ws of [made, added]) { + sessionIds.push((await createSession(request, projectId, ws.id)).id); + await git(dir, 'worktree', 'remove', '--force', String(ws.workingDir)); + await pollFor(`${ws.name} to go`, async () => + (await workstreamOf(request, projectId, ws.id)) ? undefined : true, + ); + } + + // A refresh after the removals would have adopted them again. + await awaitRefresh(request, projectId, dir); + const listed = await allWorkstreams(request); + expect(listed.map((ws) => ws.workingDir)).not.toContain(made.workingDir); + expect(listed.map((ws) => ws.workingDir)).not.toContain(path); + const listedSessionIds = listed.flatMap((ws) => + (ws.sessions ?? []).map((session) => session.id), + ); + for (const sessionId of sessionIds) { + expect(listedSessionIds).not.toContain(sessionId); + } + }); + + test('sessions in an untracked worktree are counted, not adopted, and attach once it is added', async ({ + request, + }) => { + const dir = await tempGitRepo('hs-untracked-'); + const path = await addWorktree(dir, { inside: true }); + // The default workstream lets the project be added again as the daemon sets it up. + const first = await addProjectWithDefault(request, dir); + await awaitSuggested(request, first.projectId, path); + const added = await addedWorktree(request, first.projectId, path); + await createSession(request, first.projectId, added.id); + + // The session is left running; it is found again with the project. + const removed = await request.delete(`/api/projects/${first.projectId}`); + expect(removed.status()).toBe(204); + const again = await addProjectWithDefault(request, dir); + + await pollFor('the session to be counted', async () => + (await untrackedOf(request, again.projectId)).find( + (w) => w.path === path && w.sessionCount === 1, + ), + ); + await awaitRefresh(request, again.projectId, dir); + const project = await projectOf(request, again.projectId); + expect(project?.workstreams?.map((ws) => ws.workingDir)).not.toContain( + path, + ); + + const readded = await addedWorktree(request, again.projectId, path); + await pollFor('the session to attach', async () => + (await sessionsOf(request, again.projectId, readded.id)).length === 1 + ? true + : undefined, + ); + }); +}); diff --git a/webapp/apps/web-e2e/src/api/workstream-helpers.ts b/webapp/apps/web-e2e/src/api/workstream-helpers.ts index d808116..dec4baa 100644 --- a/webapp/apps/web-e2e/src/api/workstream-helpers.ts +++ b/webapp/apps/web-e2e/src/api/workstream-helpers.ts @@ -1,4 +1,8 @@ -import { expect, type APIRequestContext } from '@playwright/test'; +import { + expect, + type APIRequestContext, + type APIResponse, +} from '@playwright/test'; import { tempProjectDir } from '../fixtures'; import { isPlaceholderSession } from '../placeholders'; @@ -221,3 +225,53 @@ export async function sessionsOf( ): Promise { return (await workstreamOf(request, projectId, workstreamId))?.sessions ?? []; } + +export interface UntrackedWorktree { + name: string; + branch?: string; + path: string; + sessionCount: number; +} + +/** A project as GET /api/projects lists it. */ +export interface ListedProject { + id: string; + workstreams?: RealWorkstream[]; + untrackedWorktrees?: UntrackedWorktree[]; +} + +/** Every project as GET /api/projects lists them right now. */ +export async function listedProjects( + request: APIRequestContext, +): Promise { + return (await (await request.get('/api/projects')).json()).projects ?? []; +} + +/** The project as GET /api/projects lists it right now, if it is listed. */ +export async function projectOf( + request: APIRequestContext, + projectId: string, +): Promise { + return (await listedProjects(request)).find( + (project) => project.id === projectId, + ); +} + +/** The project's untracked worktrees as GET /api/projects lists them right now. */ +export async function untrackedOf( + request: APIRequestContext, + projectId: string, +): Promise { + return (await projectOf(request, projectId))?.untrackedWorktrees ?? []; +} + +/** Asks the daemon to add the untracked worktree at `path` as a workstream. */ +export async function addUntrackedWorktree( + request: APIRequestContext, + projectId: string, + path: string, +): Promise { + return request.post(`/api/projects/${projectId}/worktree-workstreams`, { + data: { path }, + }); +} diff --git a/webapp/apps/web-e2e/src/git-repo.ts b/webapp/apps/web-e2e/src/git-repo.ts index f48c4d9..a42a6f2 100644 --- a/webapp/apps/web-e2e/src/git-repo.ts +++ b/webapp/apps/web-e2e/src/git-repo.ts @@ -1,5 +1,7 @@ import { execFile } from 'node:child_process'; import { promisify } from 'node:util'; +import { mkdir, realpath } from 'node:fs/promises'; +import { join } from 'node:path'; import { type APIRequestContext } from '@playwright/test'; import { tempProjectDir } from './fixtures'; import { addEmptyProject } from './projects'; @@ -46,3 +48,39 @@ export async function gitProject( const dir = await tempGitRepo(prefix); return { dir, projectId: await addEmptyProject(request, dir) }; } + +let worktreeCount = 0; + +/** + * Adds a worktree to `repo` that HomeShell did not make, and returns its path + * with symlinks resolved. It goes into a fresh temp folder, or under + * `/.worktrees` with `inside`, where the fake provider finds the + * sessions of a project added again. It gets a new branch unless `branch` + * names one or it is `detach`ed. + */ +export async function addWorktree( + repo: string, + options: { branch?: string; detach?: boolean; inside?: boolean } = {}, +): Promise { + const n = ++worktreeCount; + const parent = options.inside + ? join(repo, '.worktrees') + : await tempProjectDir('hs-worktree-'); + await mkdir(parent, { recursive: true }); + const target = join(parent, `wt-${n}`); + if (options.detach) { + await git(repo, 'worktree', 'add', '-q', '--detach', target); + } else { + await git( + repo, + 'worktree', + 'add', + '-q', + '-b', + options.branch ?? `wt-branch-${process.pid}-${n}`, + target, + ); + } + // The daemon reports the resolved path (of a symlinked temp dir, say). + return realpath(target); +} diff --git a/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts b/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts index 43180b9..efcad79 100644 --- a/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts +++ b/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts @@ -1,4 +1,4 @@ -import { type Page } from '@playwright/test'; +import { type Locator, type Page } from '@playwright/test'; import { expect } from '../fixtures'; /** @@ -45,3 +45,19 @@ export function readClipboard(page: Page): Promise { ).clipboard.readText(), ); } + +/** + * Presses Tab until `target` has focus, at most `maxPresses` times; fails + * when it never does. + */ +export async function tabUntilFocused( + page: Page, + target: Locator, + maxPresses = 40, +) { + for (let presses = 0; presses < maxPresses; presses++) { + if (await target.evaluate((el) => el.matches(':focus'))) return; + await page.keyboard.press('Tab'); + } + await expect(target).toBeFocused(); +} diff --git a/webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts b/webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts new file mode 100644 index 0000000..dfe58c9 --- /dev/null +++ b/webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts @@ -0,0 +1,90 @@ +import { expect, test } from '../fixtures'; +import { addWorktree, tempGitRepo } from '../git-repo'; +import { tabUntilFocused } from './keyboard-helpers'; +import { selectProjectWithDefault } from './workstream-helpers'; + +/** + * A worktree made outside HomeShell is offered in the project view, under + * Separate folders. The daemon surveys on its refresh interval, so the fold + * is waited for. + */ + +const SUGGESTION_TIMEOUT = 15_000; + +/** A project with a worktree on branch `branch` made outside HomeShell; the fold, awaited. */ +async function projectWithUntracked( + page: Parameters[0], + request: Parameters[1], + branch: string, +) { + const dir = await tempGitRepo('hs-ui-untracked-'); + const path = await addWorktree(dir, { branch }); + await selectProjectWithDefault(page, request, dir); + const separate = page.getByRole('region', { name: /^Separate folders/ }); + const fold = separate.getByRole('button', { name: '1 untracked worktree' }); + await expect(fold).toBeVisible({ timeout: SUGGESTION_TIMEOUT }); + return { path, separate, fold }; +} + +test('a worktree made outside HomeShell can be added from the project view', async ({ + page, + request, +}) => { + const { path, separate, fold } = await projectWithUntracked( + page, + request, + 'wt-ui', + ); + + await fold.click(); + const row = separate.getByRole('listitem').filter({ hasText: 'wt-ui' }); + await expect(row).toContainText(path); + + await row.getByRole('button', { name: 'Add wt-ui' }).click(); + + await expect( + page.getByRole('region', { name: 'Workstream wt-ui' }), + ).toBeVisible(); + await expect(fold).toBeHidden(); +}); + +test('the arrow keys reach the fold and its rows, and Enter adds one', async ({ + page, + request, +}) => { + const { fold } = await projectWithUntracked(page, request, 'wt-arrows'); + + await page.getByRole('region', { name: /^Workstream / }).first().focus(); + await page.keyboard.press('ArrowDown'); + await expect(fold).toBeFocused(); + await page.keyboard.press('ArrowRight'); + await expect(fold).toHaveAttribute('aria-expanded', 'true'); + await page.keyboard.press('ArrowDown'); + const row = page.getByRole('listitem').filter({ hasText: 'wt-arrows' }); + await expect(row).toBeFocused(); + + await page.keyboard.press('Enter'); + await expect( + page.getByRole('region', { name: 'Workstream wt-arrows' }), + ).toBeVisible(); +}); + +test('the fold and Add are reachable by keyboard', async ({ + page, + request, +}) => { + const { fold } = await projectWithUntracked(page, request, 'wt-kbd'); + + await page.getByRole('button', { name: 'New workstream' }).focus(); + await tabUntilFocused(page, fold); + await page.keyboard.press('Enter'); + await expect(fold).toHaveAttribute('aria-expanded', 'true'); + + const add = page.getByRole('button', { name: 'Add wt-kbd' }); + await tabUntilFocused(page, add, 10); + await page.keyboard.press('Enter'); + + await expect( + page.getByRole('region', { name: 'Workstream wt-kbd' }), + ).toBeVisible(); +}); From c3725dcaf410f0cef824bb4f001e1b6274fd7c2d Mon Sep 17 00:00:00 2001 From: Philip Langer Date: Thu, 12 Nov 2026 16:30:27 +0100 Subject: [PATCH 04/13] test(web): wait for the new workstream form to take focus The form mounts once the providers load and focuses its name in an effect, so the name box can be found before it has focus. CI hit that gap. --- webapp/apps/web/src/app/project-view.spec.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/webapp/apps/web/src/app/project-view.spec.tsx b/webapp/apps/web/src/app/project-view.spec.tsx index e8ffcaa..c828dc3 100644 --- a/webapp/apps/web/src/app/project-view.spec.tsx +++ b/webapp/apps/web/src/app/project-view.spec.tsx @@ -983,7 +983,9 @@ describe('ProjectView focus around creating', () => { await renderLive(project); fireEvent.click(opener()); const name = await nameBox(); - expect(document.activeElement).toBe(name); + // The form mounts once the providers load, and focuses its name an + // effect later: the box can be found before it has focus. + await waitFor(() => expect(document.activeElement).toBe(name)); act(() => opener().focus()); expect((opener() as HTMLButtonElement).disabled).toBe(false); From e5ee223a42c024f2f78a5d03e36b2ee75a5a92ee Mon Sep 17 00:00:00 2001 From: Philip Langer Date: Thu, 12 Nov 2026 17:17:01 +0100 Subject: [PATCH 05/13] fix(ui): untracked worktrees keep the list's one tab stop The fold and every Add button were tab stops of their own, and focusing the fold or a row put the list's stop back on the first card. The fold and its rows now take part in the roving tab stop (useListTabStop's trailing items), and an Add takes Tab only from its row, as a card's controls do. Review: https://github.com/eclipse-enclave/homeshell/pull/7#discussion_r4552611951 --- .../apps/web-e2e/src/ui/keyboard-helpers.ts | 18 +------- .../src/ui/untracked-worktrees.spec.ts | 21 ++++++--- webapp/apps/web/src/app/project-view.spec.tsx | 26 +++++++++++ webapp/apps/web/src/app/project-view.tsx | 3 ++ .../src/lib/list-navigation.spec.tsx | 16 ++++++- .../components/src/lib/list-navigation.ts | 35 +++++++++++---- .../src/lib/untracked-worktrees.spec.tsx | 27 ++++++++++++ .../src/lib/untracked-worktrees.tsx | 44 +++++++++++++++++-- 8 files changed, 152 insertions(+), 38 deletions(-) diff --git a/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts b/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts index efcad79..43180b9 100644 --- a/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts +++ b/webapp/apps/web-e2e/src/ui/keyboard-helpers.ts @@ -1,4 +1,4 @@ -import { type Locator, type Page } from '@playwright/test'; +import { type Page } from '@playwright/test'; import { expect } from '../fixtures'; /** @@ -45,19 +45,3 @@ export function readClipboard(page: Page): Promise { ).clipboard.readText(), ); } - -/** - * Presses Tab until `target` has focus, at most `maxPresses` times; fails - * when it never does. - */ -export async function tabUntilFocused( - page: Page, - target: Locator, - maxPresses = 40, -) { - for (let presses = 0; presses < maxPresses; presses++) { - if (await target.evaluate((el) => el.matches(':focus'))) return; - await page.keyboard.press('Tab'); - } - await expect(target).toBeFocused(); -} diff --git a/webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts b/webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts index dfe58c9..a9e0d48 100644 --- a/webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts +++ b/webapp/apps/web-e2e/src/ui/untracked-worktrees.spec.ts @@ -1,6 +1,5 @@ import { expect, test } from '../fixtures'; import { addWorktree, tempGitRepo } from '../git-repo'; -import { tabUntilFocused } from './keyboard-helpers'; import { selectProjectWithDefault } from './workstream-helpers'; /** @@ -69,19 +68,27 @@ test('the arrow keys reach the fold and its rows, and Enter adds one', async ({ ).toBeVisible(); }); -test('the fold and Add are reachable by keyboard', async ({ +test('Tab comes back to the row the arrows left, and on to its Add', async ({ page, request, }) => { const { fold } = await projectWithUntracked(page, request, 'wt-kbd'); + await page.getByRole('region', { name: /^Workstream / }).first().focus(); + await page.keyboard.press('ArrowDown'); + await page.keyboard.press('ArrowRight'); + await page.keyboard.press('ArrowDown'); + const row = page.getByRole('listitem').filter({ hasText: 'wt-kbd' }); + await expect(row).toBeFocused(); + + // The list keeps one tab stop: Tab from above lands on the row, not the fold. await page.getByRole('button', { name: 'New workstream' }).focus(); - await tabUntilFocused(page, fold); - await page.keyboard.press('Enter'); - await expect(fold).toHaveAttribute('aria-expanded', 'true'); + await page.keyboard.press('Tab'); + await expect(row).toBeFocused(); + await expect(fold).toHaveAttribute('tabindex', '-1'); - const add = page.getByRole('button', { name: 'Add wt-kbd' }); - await tabUntilFocused(page, add, 10); + await page.keyboard.press('Tab'); + await expect(page.getByRole('button', { name: 'Add wt-kbd' })).toBeFocused(); await page.keyboard.press('Enter'); await expect( diff --git a/webapp/apps/web/src/app/project-view.spec.tsx b/webapp/apps/web/src/app/project-view.spec.tsx index c828dc3..629bfa4 100644 --- a/webapp/apps/web/src/app/project-view.spec.tsx +++ b/webapp/apps/web/src/app/project-view.spec.tsx @@ -422,6 +422,32 @@ describe('ProjectView sections', () => { ).toHaveLength(1); }); + it('keeps the tab stop on the row the arrows left it on', async () => { + stubDaemon(); + await renderLive(untracked(withCards(worktree('w2', 'b')))); + const card = screen.getByRole('region', { name: 'Workstream b' }); + const fold = screen.getByRole('button', { + name: '1 untracked worktree', + }); + + act(() => card.focus()); + fireEvent.keyDown(card, { key: 'ArrowDown' }); + expect(document.activeElement).toBe(fold); + fireEvent.keyDown(fold, { key: 'ArrowRight' }); + const row = screen + .getAllByRole('listitem') + .find((item) => item.textContent?.startsWith('wt-a'))!; + fireEvent.keyDown(fold, { key: 'ArrowDown' }); + expect(document.activeElement).toBe(row); + + expect([card, fold, row].map((item) => item.tabIndex)).toEqual([ + -1, -1, 0, + ]); + expect( + screen.getByRole('button', { name: 'Add wt-a' }).getAttribute('tabindex'), + ).toBeNull(); + }); + it('does not say there are no separate folders above a fold of them', async () => { stubDaemon(); await renderLive(untracked(withCards())); diff --git a/webapp/apps/web/src/app/project-view.tsx b/webapp/apps/web/src/app/project-view.tsx index f62aede..dcf8c08 100644 --- a/webapp/apps/web/src/app/project-view.tsx +++ b/webapp/apps/web/src/app/project-view.tsx @@ -26,6 +26,7 @@ import { useRenamableTitle, useReturnFocus, UntrackedWorktrees, + untrackedNavIds, WorkstreamCard, type WorkstreamCardHandle, } from '@homeshell/components'; @@ -202,6 +203,7 @@ export function ProjectView({ const emptiedFolder = useRef(null); const list = useListTabStop([...inPlace, ...separate], { resetKey: project.id, + trailing: untrackedNavIds(project.untrackedWorktrees), emptyFocus: () => (emptiedFolder.current === project.id && workHereRef.current) || newWorkstreamRef.current, @@ -777,6 +779,7 @@ export function ProjectView({ {separate.map(renderCard)} { /** `inside`: the list stays on the page, New in it, once it is empty. */ function List({ workstreams, + trailing = [], resetKey = 'p1', inside = false, }: { workstreams: Workstream[]; + trailing?: string[]; resetKey?: string; inside?: boolean; }) { @@ -114,6 +116,7 @@ describe('useListTabStop', () => { const list = useListTabStop(workstreams, { resetKey, emptyFocus: () => empty.current, + trailing, }); const newButton = (