Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions apps/desktop/src/main/bootstrap/bootstrap-desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
SettingsSnapshot,
} from "@pi-desktop/shared";
import { IPC_CHANNELS } from "@pi-desktop/shared";
import { Effect } from "effect";
import {
app,
BrowserWindow,
Expand Down Expand Up @@ -37,7 +36,6 @@ import {
type ThreadCatalogEntry,
} from "../catalogs/thread-catalog";
import { WorkspaceSessionCatalog } from "../catalogs/workspace-session-catalog";
import { PiError } from "../effect/errors";
import { installDesktopMainRuntime, runEffectVoid } from "../effect/runtime";
import { createGitService, type GitService } from "../git/git-service";
import { createSanitizingHandle } from "../ipc/sanitize-ipc-error";
Expand Down Expand Up @@ -522,19 +520,6 @@ export async function bootstrapDesktop() {
removeWorkspaceSession: (worktreeId) => {
workspaceSessionCatalog.remove(worktreeId);
},
runBestEffortRemoveWorktree: ({ worktreePath, repositoryRoot }) => {
runEffectVoid(
Effect.try({
try: () =>
gitService.removeWorktree({
worktreePath,
repositoryRoot,
}),
catch: () =>
PiError.of("EGIT_FAILED", "Best-effort worktree removal failed"),
}).pipe(Effect.catchAll(() => Effect.void)),
);
},
removeRepository: (repositoryId) => {
repositoryCatalog.remove(repositoryId);
},
Expand Down
25 changes: 12 additions & 13 deletions apps/desktop/src/main/bootstrap/workspace-removal-actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ describe("createWorkspaceRemovalActions", () => {
});
const deleteThread = vi.fn();
const removeWorkspaceSession = vi.fn();
const removeWorktree = vi.fn();
const runBestEffortRemoveWorktree = vi.fn(
(input: { worktreePath: string; repositoryRoot: string }) => {
removeWorktree(input);
},
);
const removeWorktreeFromGit = vi.fn();
const removeRepository = vi.fn();
const removeRepositoryPreferences = vi.fn();
const clearSelection = vi.fn();
Expand All @@ -51,7 +46,6 @@ describe("createWorkspaceRemovalActions", () => {
listThreadsByWorktree: listByWorktree,
deleteThread,
removeWorkspaceSession,
runBestEffortRemoveWorktree,
removeRepository,
removeRepositoryPreferences,
getSelectedRepositoryId: () => "repo-1",
Expand All @@ -63,7 +57,7 @@ describe("createWorkspaceRemovalActions", () => {
resolveDefaultThreadContext: vi.fn(),
switchContextInBackground: vi.fn(),
getSelectedWorktreeId: () => null,
removeWorktreeFromGit: vi.fn(),
removeWorktreeFromGit,
});

await actions.removeRepository("repo-1");
Expand All @@ -81,10 +75,7 @@ describe("createWorkspaceRemovalActions", () => {
["/repos/alpha/worktrees/feature"],
["/repos/alpha"],
]);
expect(runBestEffortRemoveWorktree).toHaveBeenCalledWith({
worktreePath: "/repos/alpha/worktrees/feature",
repositoryRoot: "/repos/alpha",
});
expect(removeWorktreeFromGit).not.toHaveBeenCalled();
expect(removeRepository).toHaveBeenCalledWith("repo-1");
expect(removeRepositoryPreferences).toHaveBeenCalledWith("repo-1");
expect(activateWorkspacePath).toHaveBeenCalledWith(
Expand Down Expand Up @@ -122,7 +113,6 @@ describe("createWorkspaceRemovalActions", () => {
listThreadsByWorktree: () => [{ id: "thread-1" }, { id: "thread-2" }],
deleteThread,
removeWorkspaceSession: vi.fn(),
runBestEffortRemoveWorktree: vi.fn(),
removeRepository: vi.fn(),
removeRepositoryPreferences: vi.fn(),
getSelectedRepositoryId: () => null,
Expand Down Expand Up @@ -154,5 +144,14 @@ describe("createWorkspaceRemovalActions", () => {
);
expect(switchContextInBackground).toHaveBeenCalledWith(threadContext);
expect(notifySessionChanged).not.toHaveBeenCalled();

deleteThread.mockClear();
removeWorktreeFromGit.mockImplementationOnce(() => {
throw new Error("dirty worktree");
});
await expect(
actions.removeWorktree("/repos/alpha/worktrees/feature"),
).rejects.toThrow("dirty worktree");
expect(deleteThread).not.toHaveBeenCalled();
});
});
20 changes: 6 additions & 14 deletions apps/desktop/src/main/bootstrap/workspace-removal-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ type CreateWorkspaceRemovalActionsDependencies = {
listThreadsByWorktree: (worktreeId: string) => ThreadEntry[];
deleteThread: (threadId: string) => void;
removeWorkspaceSession: (worktreeId: string) => void;
runBestEffortRemoveWorktree: (input: {
worktreePath: string;
repositoryRoot: string;
}) => void;
removeRepository: (repositoryId: string) => void;
removeRepositoryPreferences: (repositoryId: string) => void;
getSelectedRepositoryId: () => string | null;
Expand Down Expand Up @@ -61,7 +57,6 @@ export function createWorkspaceRemovalActions(
listThreadsByWorktree,
deleteThread,
removeWorkspaceSession,
runBestEffortRemoveWorktree,
removeRepository,
removeRepositoryPreferences,
getSelectedRepositoryId,
Expand Down Expand Up @@ -96,10 +91,6 @@ export function createWorkspaceRemovalActions(
}

removeWorkspaceSession(worktree.path);
runBestEffortRemoveWorktree({
worktreePath: worktree.path,
repositoryRoot: repository.rootPath,
});
}

const mainThreads = listThreadsByWorktree(repository.rootPath);
Expand Down Expand Up @@ -146,16 +137,17 @@ export function createWorkspaceRemovalActions(
}

const isActiveWorktree = getSelectedWorktreeId() === normalizedWorktreeId;
const threadsInWorktree = listThreadsByWorktree(normalizedWorktreeId);
for (const thread of threadsInWorktree) {
deleteThread(thread.id);
}

removeWorktreeFromGit({
worktreePath: normalizedWorktreeId,
repositoryRoot: repository.rootPath,
});

const threadsInWorktree = listThreadsByWorktree(normalizedWorktreeId);
for (const thread of threadsInWorktree) {
deleteThread(thread.id);
}
removeWorkspaceSession(normalizedWorktreeId);

if (isActiveWorktree) {
const remainingWorktrees = inspectRemainingWorktrees(repository.rootPath);
const nextWorktree = remainingWorktrees.find(
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/main/git/git-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class GitServiceImpl {

this.runCheckedGit(
repositoryRoot,
["worktree", "remove", "--force", worktreePath],
["worktree", "remove", worktreePath],
"remove worktree",
);
this.runGit(repositoryRoot, ["worktree", "prune"]);
Expand Down
16 changes: 12 additions & 4 deletions apps/desktop/src/main/git/worktree-creation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,23 @@ describe("worktree-creation", () => {
]);
});

it("creates a new branch without a start ref when no base branch resolves", () => {
const runGit: RunGit = () => createResult({ status: 1 });
it("rejects a missing explicitly requested base", () => {
expect(() =>
buildCreateWorktreeCommand(() => createResult({ status: 1 }), {
repositoryRoot: "/repo/main",
branchName: "feature/new",
worktreePath: "/repo/worktrees/feature-new",
baseBranch: "missing",
}),
).toThrow("Requested base branch does not exist: missing");
});

it("uses HEAD only when no base was requested", () => {
expect(
buildCreateWorktreeCommand(runGit, {
buildCreateWorktreeCommand(() => createResult({ status: 1 }), {
repositoryRoot: "/repo/main",
branchName: "feature/new",
worktreePath: "/repo/worktrees/feature-new",
baseBranch: "main",
}),
).toEqual([
"worktree",
Expand Down
4 changes: 3 additions & 1 deletion apps/desktop/src/main/git/worktree-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,7 @@ function resolveCreateWorktreeStartRef(
return `origin/${options.baseBranch}`;
}

return undefined;
throw new Error(
`Requested base branch does not exist: ${options.baseBranch}`,
);
}
39 changes: 27 additions & 12 deletions apps/desktop/src/main/workspace-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import { Duration, Effect } from "effect";
const DEFAULT_TIMEOUT_MS = 10_000;

function globToRegExp(pattern: string): RegExp {
const esc = pattern.replace(/[.+^${}()|\\]/g, "\\$&");
const withGlob = esc
.replace(/\\\\\*\\\\\*/g, "__DOUBLE_STAR__")
.replace(/\\\*\*/g, "__DOUBLE_STAR__")
.replace(/\*\*/g, ".*")
.replace(/\*/g, "[^/]*")
.replace(/__DOUBLE_STAR__/g, ".*");

return new RegExp(`^(?:${withGlob})$`);
const expression = pattern
.split(/(\*\*\/|\*\*|\*)/)
.map((part) => {
if (part === "**/") return "(?:.*/)?";
if (part === "**") return ".*";
if (part === "*") return "[^/]*";
return part.replace(/[.+^${}()|[\]\\]/g, "\\$&");
})
.join("");
return new RegExp(`^(?:${expression})$`);
}

function matchesAnyPattern(relativePath: string, patterns?: string[]): boolean {
Expand Down Expand Up @@ -208,6 +209,7 @@ export class WorkspaceSearchService {
const results: SearchMatch[] = [];
const root = path.resolve(request.rootPath);
const max = request.maxResults ?? Infinity;
const query = request.query.trim().toLowerCase();

async function walker(dir: string) {
if (results.length >= max) return;
Expand All @@ -226,12 +228,24 @@ export class WorkspaceSearchService {
const full = path.join(dir, ent.name);
const rel = path.relative(root, full).replace(/\\\\/g, "/");

// simple dotfile/node_modules ignore
if (rel.split("/").includes("node_modules")) continue;
// Ignore dependency and Git metadata trees before descending.
if (ent.name === "node_modules" || ent.name === ".git") continue;
if (
request.excludePatterns?.some((pattern) => {
const matcher = globToRegExp(pattern);
return (
matcher.test(rel) ||
(ent.isDirectory() && matcher.test(`${rel}/`))
);
})
)
continue;
const matchesQuery = rel.toLowerCase().includes(query);

if (ent.isDirectory()) {
// check include/exclude before descending
if (
matchesQuery &&
matchesAnyPattern(`${rel}/`, request.includePatterns) &&
!request.excludePatterns?.some((p) =>
globToRegExp(p).test(`${rel}/`),
Expand All @@ -242,7 +256,8 @@ export class WorkspaceSearchService {
}
await walker(full);
} else if (ent.isFile()) {
if (!matchesAnyPattern(rel, request.includePatterns)) continue;
if (!matchesQuery || !matchesAnyPattern(rel, request.includePatterns))
continue;
if (request.excludePatterns?.some((p) => globToRegExp(p).test(rel)))
continue;
results.push(toSearchMatch(root, rel, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,6 @@ describe("PromptDock", () => {
expect(onPromptModeChange).toHaveBeenCalledWith("plan");
});

it("persists the draft per thread in localStorage", () => {
renderPromptDock({
draft: "work in progress",
activeThreadId: "thread-42",
});

expect(window.localStorage.getItem("pi:prompt-draft:thread-42")).toBe(
"work in progress",
);
});

it("cycles prompt history with ArrowUp / ArrowDown", () => {
window.localStorage.setItem(
"pi:prompt-history:thread-1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Attachments, useAttachments } from "./prompt-dock/attachments";
import { CharacterCounter } from "./prompt-dock/character-counter";
import { ContextGauge } from "./prompt-dock/context-gauge";
import { ModelPicker } from "./prompt-dock/model-picker";
import { usePersistDraft } from "./prompt-dock/prompt-draft";
import { SendButton } from "./prompt-dock/send-button";
import { usePromptDockDisplay } from "./prompt-dock/use-prompt-dock-display";
import { usePromptDockInput } from "./prompt-dock/use-prompt-dock-input";
Expand Down Expand Up @@ -95,9 +94,7 @@ export function PromptDock({
const hasActiveThread = activeThreadId !== null;

const { uploadedFiles, imageFiles, handlePickFiles, handleRemoveFile } =
useAttachments(draft, onDraftChange);

usePersistDraft(activeThreadId, draft);
useAttachments(draft, onDraftChange, activeThreadId);

const {
mergedSuggestions,
Expand Down
Loading
Loading