Make workspace IDs opaque, independent of the filesystem path (#1503)

* Make workspace IDs opaque, independent of the filesystem path

Workspace IDs were the resolved checkout/worktree path, so code could
treat an ID as a path: prefix matching, deriving directories from it,
falling back to a path when a lookup missed. IDs are now opaque -
compared only by exact equality and never used as a path. Anything
path-shaped resolves the workspace record first and reads its cwd.

New workspaces get a generated `wks_` ID; existing path-shaped IDs are
read from disk and never regenerated, so there is no migration and no
change to the wire or persisted schemas. Groundwork for running
multiple workspaces in a single directory.

* Keep attachment scope key stable and align refetch test

The attachment scope key keeps `workspace=` instead of `wsid=` so existing
persisted drafts are not orphaned; the rename was cosmetic. The SDK refetch
test no longer asserts a filter, matching refetch fetching one page and
selecting by id client-side.

* Fix live agent updates in directories without a registered workspace

The opaque-ID work removed buildProjectPlacementForCwd's directory
fallback, so an agent running in a folder with no registered workspace
(e.g. a fresh non-git dir) produced a null placement. forwardAgentUpdate
then threw "Workspace not found", the error was swallowed by its catch,
and no agent_update was emitted — live model/thinking switches and status
updates silently stopped. Caught by the live-preferences e2e suite.

The fallback builds a directory-scoped project placement keyed by the
path. That key is a project grouping key (non-git projects group by
path), not a workspace id, so it stays within the opaque-id rule.

* Always run server worktree archive, even without a resolved workspace

Archiving bailed out entirely when the workspace was not found in the
client store, so a race or stale state could make "archive" do nothing
server-side with only a console.warn. The server archive is keyed by
worktreePath, which is always available, so it now runs regardless; only
the optimistic client-side updates (keyed by workspace id) are gated on
the workspace being resolved.

* Expect a directory-scoped placement for unregistered agent dirs

This unit test asserted the no-placement behavior reverted in the live
agent-update fix, which had broken live model/thinking switching. Update
it to expect the directory-scoped placement now emitted for an agent in a
directory with no registered workspace.

* Fix opaque workspace routing in app E2E

* Preserve workspace IDs during partial bootstrap

* Fix archive flows for opaque workspace IDs

* Stop treating opaque workspace IDs as filesystem paths

Workspace IDs are opaque (wks_<hex>), but several call sites still passed
the id where a directory was expected, which broke those flows for opaque
IDs. The branch switcher sent the id as a cwd to branch/stash/checkout git
operations, and server archive/reconcile cleanup keyed git-watch and
subscription teardown by id, leaking that state.

Git and filesystem operations now take the workspace directory; the opaque
id is used only for identity and cache keys. Archive/reconcile cleanup
routes through a single teardownArchivedWorkspace helper that keeps the key
split explicit: runtime store by id, git watch and subscription by cwd.
Path-derived grouping keys are renamed to directory keys, and the helpers
are split into workspace-identity and workspace-directory so the id-vs-path
boundary is obvious.
This commit is contained in:
Mohamed Boudra
2026-06-14 22:03:27 +08:00
committed by GitHub
parent f352072dac
commit 21deb08673
105 changed files with 1530 additions and 1045 deletions
@@ -8,6 +8,12 @@ import type { WorkspaceStructureProject } from "@/projects/workspace-structure";
const EMPTY_PROJECTS: SidebarProjectEntry[] = [];
function workspaceNameFromDirectory(directory: string): string {
const trimmed = directory.trim().replace(/[\\/]+$/g, "");
const separator = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
return separator >= 0 ? trimmed.slice(separator + 1) : trimmed;
}
export type SidebarStateBucket = WorkspaceDescriptor["status"];
export interface SidebarWorkspaceEntry {
@@ -54,7 +60,7 @@ function createStructuralWorkspaceEntry(input: {
workspaceDirectory: undefined,
projectKind: input.project.projectKind,
workspaceKind: "checkout",
name: input.workspaceId,
name: workspaceNameFromDirectory(input.project.iconWorkingDir) || input.workspaceId,
statusBucket: "done",
statusEnteredAt: null,
archivingAt: null,
+30 -22
View File
@@ -5,12 +5,14 @@ import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
import type { ComboboxOption } from "@/components/ui/combobox";
import type { ToastApi } from "@/components/toast-host";
import { invalidateCheckoutGitQueriesForClient } from "@/git/query-keys";
import { createBranchSwitcherOperations } from "@/git/branch-switcher-operations";
import { confirmDialog } from "@/utils/confirm-dialog";
interface UseBranchSwitcherInput {
client: DaemonClient | null;
normalizedServerId: string;
normalizedWorkspaceId: string;
workspaceDirectory: string | null;
currentBranchName: string | null;
isGitCheckout: boolean;
isConnected: boolean;
@@ -30,6 +32,7 @@ export function useBranchSwitcher({
client,
normalizedServerId,
normalizedWorkspaceId,
workspaceDirectory,
currentBranchName,
isGitCheckout,
isConnected,
@@ -39,22 +42,29 @@ export function useBranchSwitcher({
const { t } = useTranslation();
const [isOpen, setIsOpen] = useState(false);
// Git operations are bound to the workspace directory; the opaque workspace id is
// used only for query cache identity below, never as a cwd.
const operations = useMemo(
() =>
client && workspaceDirectory
? createBranchSwitcherOperations(client, workspaceDirectory)
: null,
[client, workspaceDirectory],
);
const branchSuggestionsQuery = useQuery({
queryKey: ["branchSuggestions", normalizedServerId, normalizedWorkspaceId],
queryFn: async () => {
if (!client) {
if (!operations) {
throw new Error(t("common.errors.daemonClientUnavailable"));
}
const payload = await client.getBranchSuggestions({
cwd: normalizedWorkspaceId,
limit: 200,
});
const payload = await operations.getBranchSuggestions(200);
if (payload.error) {
throw new Error(payload.error);
}
return payload.branches ?? [];
},
enabled: isOpen && isGitCheckout && Boolean(client) && isConnected,
enabled: isOpen && isGitCheckout && Boolean(operations) && isConnected,
retry: false,
staleTime: 15_000,
});
@@ -70,20 +80,21 @@ export function useBranchSwitcher({
);
const invalidateStashAndCheckout = useCallback(async () => {
if (!workspaceDirectory) return;
await Promise.all([
queryClient.invalidateQueries({ queryKey: stashListQueryKey }),
invalidateCheckoutGitQueriesForClient(queryClient, {
serverId: normalizedServerId,
cwd: normalizedWorkspaceId,
cwd: workspaceDirectory,
}),
]);
}, [queryClient, stashListQueryKey, normalizedServerId, normalizedWorkspaceId]);
}, [queryClient, stashListQueryKey, normalizedServerId, workspaceDirectory]);
const maybeRestoreStashForBranch = useCallback(
async (branchId: string) => {
if (!client) return;
if (!operations) return;
try {
const stashPayload = await client.stashList(normalizedWorkspaceId, { paseoOnly: true });
const stashPayload = await operations.listPaseoStashes();
const targetStash = stashPayload.entries.find((e) => e.branch === branchId);
if (!targetStash) return;
const shouldRestore = await confirmDialog({
@@ -93,7 +104,7 @@ export function useBranchSwitcher({
cancelLabel: t("branchSwitcher.later"),
});
if (!shouldRestore) return;
const popPayload = await client.stashPop(normalizedWorkspaceId, targetStash.index);
const popPayload = await operations.popStash(targetStash.index);
if (popPayload.error) {
toast.error(popPayload.error.message);
} else {
@@ -104,12 +115,12 @@ export function useBranchSwitcher({
// Non-critical — user can still restore on next branch switch
}
},
[client, invalidateStashAndCheckout, normalizedWorkspaceId, toast, t],
[operations, invalidateStashAndCheckout, toast, t],
);
const stashAndSwitch = useCallback(
async (branchId: string) => {
if (!client) return;
if (!operations) return;
const shouldStash = await confirmDialog({
title: t("branchSwitcher.uncommittedTitle"),
message: t("branchSwitcher.uncommittedMessage"),
@@ -119,15 +130,13 @@ export function useBranchSwitcher({
if (!shouldStash) return;
try {
const stashPayload = await client.stashSave(normalizedWorkspaceId, {
branch: currentBranchName ?? undefined,
});
const stashPayload = await operations.saveStash(currentBranchName ?? undefined);
if (stashPayload.error) {
toast.error(stashPayload.error.message);
return;
}
await invalidateStashAndCheckout();
const switchPayload = await client.checkoutSwitchBranch(normalizedWorkspaceId, branchId);
const switchPayload = await operations.switchBranch(branchId);
if (switchPayload.error) {
toast.error(switchPayload.error.message);
return;
@@ -137,7 +146,7 @@ export function useBranchSwitcher({
toast.error(err instanceof Error ? err.message : t("branchSwitcher.failedToStash"));
}
},
[client, currentBranchName, invalidateStashAndCheckout, normalizedWorkspaceId, toast, t],
[operations, currentBranchName, invalidateStashAndCheckout, toast, t],
);
const handleBranchSelect = useCallback(
@@ -145,9 +154,9 @@ export function useBranchSwitcher({
if (branchId === currentBranchName) return;
void (async () => {
if (!client) return;
if (!operations) return;
try {
const payload = await client.checkoutSwitchBranch(normalizedWorkspaceId, branchId);
const payload = await operations.switchBranch(branchId);
if (payload.error) {
// If the error is about uncommitted changes, offer the stash dialog
if (payload.error.message.toLowerCase().includes("uncommitted")) {
@@ -166,11 +175,10 @@ export function useBranchSwitcher({
})();
},
[
client,
operations,
currentBranchName,
invalidateStashAndCheckout,
maybeRestoreStashForBranch,
normalizedWorkspaceId,
stashAndSwitch,
t,
toast,