-
Notifications
You must be signed in to change notification settings - Fork 23
fix(sessions): strip remote workspace prefix from file paths in chat #2205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
timgl
wants to merge
2
commits into
main
Choose a base branch
from
posthog-code/strip-workspace-prefix-from-file-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
apps/code/src/renderer/features/sessions/hooks/useDisplayRepoPath.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { renderHook } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mockUseCwd = vi.hoisted(() => vi.fn((): string | undefined => undefined)); | ||
| const mockUseTasks = vi.hoisted(() => | ||
| vi.fn((): { data: Array<{ id: string; repository?: string | null }> } => ({ | ||
| data: [], | ||
| })), | ||
| ); | ||
|
|
||
| vi.mock("@features/sidebar/hooks/useCwd", () => ({ useCwd: mockUseCwd })); | ||
| vi.mock("@features/tasks/hooks/useTasks", () => ({ useTasks: mockUseTasks })); | ||
|
|
||
| import { useDisplayRepoPath } from "./useDisplayRepoPath"; | ||
|
|
||
| describe("useDisplayRepoPath", () => { | ||
| beforeEach(() => { | ||
| mockUseCwd.mockReturnValue(undefined); | ||
| mockUseTasks.mockReturnValue({ data: [] }); | ||
| }); | ||
|
|
||
| it("returns local cwd when available (local task)", () => { | ||
| mockUseCwd.mockReturnValue("/Users/me/code/posthog"); | ||
| const { result } = renderHook(() => useDisplayRepoPath("task-1")); | ||
| expect(result.current).toBe("/Users/me/code/posthog"); | ||
| }); | ||
|
|
||
| it("derives remote workspace path from task.repository when cwd is missing", () => { | ||
| mockUseTasks.mockReturnValue({ | ||
| data: [{ id: "task-1", repository: "posthog/posthog.com" }], | ||
| }); | ||
| const { result } = renderHook(() => useDisplayRepoPath("task-1")); | ||
| expect(result.current).toBe("/tmp/workspace/repos/posthog/posthog.com"); | ||
| }); | ||
|
|
||
| it("prefers local cwd over derived remote path when both could resolve", () => { | ||
| mockUseCwd.mockReturnValue("/Users/me/code/posthog"); | ||
| mockUseTasks.mockReturnValue({ | ||
| data: [{ id: "task-1", repository: "posthog/posthog.com" }], | ||
| }); | ||
| const { result } = renderHook(() => useDisplayRepoPath("task-1")); | ||
| expect(result.current).toBe("/Users/me/code/posthog"); | ||
| }); | ||
|
|
||
| it("returns undefined when task has no repository", () => { | ||
| mockUseTasks.mockReturnValue({ data: [{ id: "task-1" }] }); | ||
| const { result } = renderHook(() => useDisplayRepoPath("task-1")); | ||
| expect(result.current).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined when repository is malformed", () => { | ||
| mockUseTasks.mockReturnValue({ | ||
| data: [{ id: "task-1", repository: "not-a-valid-repo-string" }], | ||
| }); | ||
| const { result } = renderHook(() => useDisplayRepoPath("task-1")); | ||
| expect(result.current).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined when task is not found", () => { | ||
| mockUseTasks.mockReturnValue({ | ||
| data: [{ id: "other-task", repository: "posthog/posthog.com" }], | ||
| }); | ||
| const { result } = renderHook(() => useDisplayRepoPath("task-1")); | ||
| expect(result.current).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined when taskId is undefined", () => { | ||
| mockUseTasks.mockReturnValue({ | ||
| data: [{ id: "task-1", repository: "posthog/posthog.com" }], | ||
| }); | ||
| const { result } = renderHook(() => useDisplayRepoPath(undefined)); | ||
| expect(result.current).toBeUndefined(); | ||
| }); | ||
| }); |
27 changes: 27 additions & 0 deletions
27
apps/code/src/renderer/features/sessions/hooks/useDisplayRepoPath.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { useCwd } from "@features/sidebar/hooks/useCwd"; | ||
| import { useTasks } from "@features/tasks/hooks/useTasks"; | ||
| import { parseRepository } from "@utils/repository"; | ||
|
|
||
| const REMOTE_WORKSPACE_PREFIX = "/tmp/workspace/repos"; | ||
|
|
||
| /** | ||
| * Returns the repo root to strip when displaying file paths in tool calls. | ||
| * Cloud tasks have no local cwd, so we derive the conventional sandbox | ||
| * clone location from `task.repository` — otherwise chips would render | ||
| * the full `/tmp/workspace/repos/<owner>/<repo>/...` sandbox path. | ||
| */ | ||
| export function useDisplayRepoPath( | ||
| taskId: string | undefined, | ||
| ): string | undefined { | ||
| const localCwd = useCwd(taskId ?? ""); | ||
| const { data: tasks = [] } = useTasks(); | ||
|
|
||
| if (localCwd) return localCwd; | ||
| if (!taskId) return undefined; | ||
|
|
||
| const task = tasks.find((t) => t.id === taskId); | ||
| const parsed = task?.repository ? parseRepository(task.repository) : null; | ||
| if (!parsed) return undefined; | ||
|
|
||
| return `${REMOTE_WORKSPACE_PREFIX}/${parsed.organization}/${parsed.repoName}`; | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could use a comment here to explain what/why (brief!)