Skip to content

fix(app): retain chosen clone directory - #64672

Closed
stussysenik wants to merge 2 commits into
anomalyco:devfrom
stussysenik:keep-clone-directory
Closed

stussysenik wants to merge 2 commits into
anomalyco:devfrom
stussysenik:keep-clone-directory

Conversation

@stussysenik

@stussysenik stussysenik commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #64644

Type of change

  • Bug fix

What does this PR do?

Home replaced a chosen sibling clone with the project's canonical checkout. The selection changed on add, cleared on reload, and new sessions opened in the canonical folder. The chosen directory now survives add, restore, and new-session creation, while the server's project id still carries identity and worktree inventory.

How did you verify your code works?

  • Three RED→GREEN slices: restored clone keeps its directory (machine test), add keeps the chosen path with the server's project id (new add test), and new-session resolution keeps the chosen path (new start test).
  • Focused tests: 15 pass. Package unit suite: 2774 pass, 0 fail.
  • bun run check 35/35, package typecheck clean, oxlint clean on changed files.

Screenshots / recordings

No layout or paint change; only the selected directory value changes. The three tests above cover the behavior.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #55351

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes only half of #64644. I ran the server and web app from source for the base and for this PR, and ran the issue's Playwright reproduction with two plain clones of one repo:

  • Saved selection (fixed): after adding repo-copy, Home's saved selection is now …/repo-copy, and it stays that way after a reload. On the base it is …/repo.
  • New session (not fixed): New session from Home still opens the draft in …/repo. The footer shows /tmp/opencode/clones/repo, and every request the draft makes (/api/location, /api/config, /api/vcs, …) uses location[directory]=/tmp/opencode/clones/repo. This happens because ctx.projects.forDirectory returns undefined for a sibling clone, so resolveStartProject falls back to the canonical path. The unit test passes only because it stubs forDirectory to return the canonical project, which production never does (see the inline comments).
  • Regression: place now keeps a directory that belongs to no used project whenever the server has any used project. So a selection on a project that was removed while another stays used is no longer cleared.

All 68 tests under packages/app/src/home pass, and tsc -b in packages/app is clean.

Public API: unchanged.

The screenshots show the new-session draft on the base and with this PR. Both still point at …/repo.

Related: #55351

Before

After

Comment thread packages/app/src/home/projects/start.ts Outdated
used: readonly Project[],
last: Project | undefined,
) {
const match = preferred ? forDirectory(preferred) : undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the real app, forDirectory(preferred) returns undefined for a sibling clone. runtime.tsx forDirectory only matches a project's own path, its sandboxes, or folders inside it. So match is undefined and this falls back to last/used[0] with project.worktree, the canonical folder. I confirmed it in the browser: with the selection saved as …/repo-copy, New session from Home still opens the draft in …/repo (all its API requests use location[directory]=…/repo). The clone has to be resolved to its project id some other way. Options: the /api/location result already fetched in add, or keeping the chosen directory whenever the selection is set and the project comes from the selection or the last-used fallback, rather than requiring forDirectory to know about it. The same applies to the touch(directory) calls, which do nothing for a clone.

const canonical = { id: "proj_repo", worktree: "/srv/repo" } as Project
const chosen = "/srv/repo-copy"

expect(resolveStartProject(chosen, () => canonical, [canonical], undefined)).toEqual({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stubs forDirectory to return the canonical project for /srv/repo-copy, but the real forDirectory returns undefined for a sibling clone. So the test passes on a path production never takes (packages/app/AGENTS.md: build inputs the way production builds them). A test that uses the real lookup, or a project list shaped like the server's, would have caught that the new-session draft still opens in the canonical folder.

Comment thread packages/app/src/home/machine.ts Outdated
return { ...fields, status: "ready", server: entry.key, project: findProject(entry, target.directory) }
const project = findProject(entry, target.directory)

// With no used projects at all the directory cannot belong to this server; a sibling clone of a known one can.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This keeps any directory that matches no used project, as long as the server has at least one used project, not only clones of a used project. Regression: with the selection on project B, if B is removed from the used projects while A stays, the base clears the directory, but here the selection stays on B's path. The sessions list then stays filtered to a project that is no longer listed (I checked with a quick transition test: usedProjects [A, B] → [A] keeps directory: B). The existing 'clears it only once the server lists projects' test only passes because its server has no used projects at all. The machine needs to know that the directory resolves to a used project id (e.g. carry the project id with the selection), rather than keeping every unmatched directory.

@stussysenik

Copy link
Copy Markdown
Contributor Author

All three points addressed in 3389de8.

  • New session: the saved Home selection now carries an optional projectID. resolveStartProject uses that identity when forDirectory(preferred) returns undefined for a sibling clone, so the draft and its API calls use the chosen directory instead of the canonical worktree.
  • Regression: place keeps a directory only when it resolves to a used project by path or by the saved project id. A removed project with another project still in use clears the directory. New test: machine.test.ts "clears a directory whose project is no longer used while another remains".
  • Test shape: start.test.ts now stubs forDirectory to return undefined and passes the canonical project from the saved selection, matching production's runtime.tsx lookup.

Verified with the issue's two-clone setup against a source server and source web UI. Base saved selection and new-session footer: /home/senik/repo. With this PR both are /home/senik/repo-copy.

Before:
before

After:
after

Gates: focused 21/21, package unit 2776/0, bun run check 35/35, typecheck clean, oxlint clean on changed lines. Public API unchanged; the layout selection gains one optional persisted field.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit fixes the rest of #64644, and it addresses all three earlier points.

I re-ran the issue's Playwright reproduction against the server and web app from source:

  • The saved Home selection stays on …/repo-copy after adding it and after a reload. It now also stores the project id.
  • New session from Home opens the draft in …/repo-copy. The footer shows it, and all the draft's API requests use location[directory]=…/repo-copy. On the base, both pointed at …/repo.
  • With repo-copy selected, Home's sessions list shows sessions from both clones. I created one empty session in each to check.
  • The removed-project case is now cleared, and the new machine test covers it.

All 74 tests under src/home and src/shell/layout pass, and tsc -b in packages/app is clean.

Public API: the only change is an optional projectID field in the saved Home selection (layout.home.selection, the app's localStorage layout). Older saved data still loads, so it is backwards compatible. Nothing else outside the app changes.

I found nothing new that should block a merge.

Before

After

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Home replaces a selected clone directory with the canonical checkout

1 participant