fix(context): use cwd basename for project name in monorepo subdirectories (closes #2882)#3047
Open
rodboev wants to merge 4 commits into
Open
fix(context): use cwd basename for project name in monorepo subdirectories (closes #2882)#3047rodboev wants to merge 4 commits into
rodboev wants to merge 4 commits into
Conversation
Contributor
Greptile SummaryThis PR updates project-name selection for monorepo package directories. The main changes are:
Confidence Score: 4/5The change is narrowly scoped to project-name selection and includes targeted test coverage for the affected monorepo behavior. No blocking issues were identified in the changed files, and the implementation preserves the existing root and worktree behavior described by the change.
What T-Rex did
Reviews (3): Last reviewed commit: "fix(context): walk upward to package.jso..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In a git monorepo,
getProjectName()always returns the repo root basename, so every package shares the same project name. Observations frompackages/apisessions are stored undermonorepo, mixed with observations frompackages/weband every other subdirectory. This PR makesgetProjectName()use the cwd basename when the working directory is a subdirectory of the git root (the monorepo case), while preserving existing behavior for single-repo roots and non-git directories.Closes #2882
Why
src/utils/project-name.ts:47setsnameSource = repoRoot ?? expanded. Whencwdis/work/monorepo/packages/api,findGitRepoRoot()returns/work/monorepo, sonameSourceis always the root andpath.basename(nameSource)=monorepo. PR #2663 introduced this for worktree stability, butgetProjectContext()already handles worktrees separately viadetectWorktree(). Monorepo subdirectories should not share a project name.The fix adds a
samePath()helper (usingrealpathSync+path.resolve()for canonicalization, case-insensitive on Windows, with a fallback to resolve-only whenrealpathSyncthrows) and changes the nameSource logic:When
cwdequals the git root,samePathreturns true and behavior is unchanged. Whencwdis a subdirectory,samePathreturns false andexpanded(the cwd) provides the basename. Non-git directories and drive roots are unaffected. Symlinked repo roots are handled correctly becauserealpathSyncresolves both sides before comparing.Scope
This PR does not change storage key format, observation schema, or worker protocol. Worktree composite naming (
parent/leafviadetectWorktree()) is unchanged. The drive-root andunknown-projectfallback paths are unchanged.Upgrade Impact
Existing monorepo users: Observations stored before this change are keyed by the repo-root basename (e.g.,
monorepo). After upgrade, new sessions in a monorepo subdirectory (e.g.,packages/api) will use the package basename (api) as the project key. Existing observations under the old root-level key remain in storage but will not be returned for the subdirectory cwd. This is the intended behavior: the old mixed-project key was the bug. Users who need continuity can manually reference their stored memories under the old project name or reset their project context.Note:
getProjectContext().allProjectsreturns only[cwdProjectName]for non-worktree subdirectory cwds — it does not include the parent repo-root name. If a future session needs to query observations stored under both the old root-level key and the new package-level key during migration, callers would need to add the repo-root name toallProjectsfor that case.Single-repo users: No change. When
cwdequals the git root,samePathreturns true andnameSourceremains the git root — identical to the previous behavior.Verification
bun test tests/utils/project-name.test.ts— existing tests pass; updated nested-dir test passes; new monorepo package test passesnpm run build— cleannpm run lint:hook-io && npm run lint:spawn-env && npm run strip-comments:check— cleanCloses #2882