mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-17 22:05:06 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b1ae2f319 | ||
|
|
540da1a3b5 | ||
|
|
b5bd6b7704 | ||
|
|
cf193266f3 | ||
|
|
66d8e43e66 | ||
|
|
0d39140dc5 | ||
|
|
2c2385f362 | ||
|
|
7f0fe57c48 | ||
|
|
94f2df6263 | ||
|
|
02124454bc |
@@ -466,12 +466,12 @@ test("downloads a file over direct TCP with its original filename and bytes", as
|
||||
await gotoWorkspace(page, workspace.workspaceId);
|
||||
await openFileExplorer(page);
|
||||
|
||||
const daemonRequest = page.waitForRequest("**/api/files/download?*");
|
||||
const downloaded = await downloadFileFromExplorer(page, fileName);
|
||||
const url = new URL(downloaded.url());
|
||||
expect(url.protocol).toBe("http:");
|
||||
expect(url.port).toBe(getE2EDaemonPort());
|
||||
expect(url.pathname).toBe("/api/files/download");
|
||||
expect(url.searchParams.get("token")).toBeTruthy();
|
||||
const requestUrl = new URL((await daemonRequest).url());
|
||||
expect(requestUrl.port).toBe(getE2EDaemonPort());
|
||||
expect(requestUrl.searchParams.get("token")).toBeTruthy();
|
||||
expect(new URL(downloaded.url()).protocol).toBe("blob:");
|
||||
expect(downloaded.suggestedFilename()).toBe(fileName);
|
||||
const savedFile = testInfo.outputPath(fileName);
|
||||
await downloaded.saveAs(savedFile);
|
||||
@@ -479,6 +479,45 @@ test("downloads a file over direct TCP with its original filename and bytes", as
|
||||
expect(await readFile(savedFile)).toEqual(source);
|
||||
});
|
||||
|
||||
test("keeps the app open and shows the error when the host rejects a download", async ({
|
||||
page,
|
||||
}, testInfo) => {
|
||||
const fileName = "removed-during-download.bin";
|
||||
const filePath = path.join(workspace.workspaceDirectory, fileName);
|
||||
const source = Buffer.from([0, 1, 2, 255]);
|
||||
await writeFile(filePath, source);
|
||||
await gotoWorkspace(page, workspace.workspaceId);
|
||||
await openFileExplorer(page);
|
||||
const workspaceUrl = page.url();
|
||||
|
||||
// The file disappears after the token is issued, so the real daemon answers 404.
|
||||
await page.route(
|
||||
"**/api/files/download?*",
|
||||
async (route) => {
|
||||
await rm(filePath);
|
||||
await route.continue();
|
||||
},
|
||||
{ times: 1 },
|
||||
);
|
||||
await page
|
||||
.getByTestId("file-explorer-tree-scroll")
|
||||
.getByText(fileName, { exact: true })
|
||||
.click({ button: "right" });
|
||||
await page.getByRole("menuitem", { name: "Download", exact: true }).click();
|
||||
|
||||
await expect(
|
||||
page.getByText("Failed to download file (HTTP 404).", { exact: true }),
|
||||
).toBeVisible();
|
||||
expect(page.url()).toBe(workspaceUrl);
|
||||
|
||||
await writeFile(filePath, source);
|
||||
const downloaded = await downloadFileFromExplorer(page, fileName);
|
||||
const savedFile = testInfo.outputPath(fileName);
|
||||
await downloaded.saveAs(savedFile);
|
||||
expect(await readFile(savedFile)).toEqual(source);
|
||||
expect(page.url()).toBe(workspaceUrl);
|
||||
});
|
||||
|
||||
test("downloads an unsupported binary from its preview with original filename and bytes", async ({
|
||||
page,
|
||||
}, testInfo) => {
|
||||
|
||||
@@ -97,18 +97,15 @@ export class ImportSessionFlow {
|
||||
await target.scrollIntoViewIfNeeded();
|
||||
await expect(target).toBeInViewport();
|
||||
}
|
||||
/**
|
||||
* Scrolls the virtualized list, the only one that leaves rows unmounted. A row handle is no way
|
||||
* in: the list unmounts rows as it scrolls, and a detached row has no scroller above it.
|
||||
*/
|
||||
private async scrollList(forward: boolean) {
|
||||
await this.page
|
||||
.locator('[data-testid^="import-session-session-"]')
|
||||
.first()
|
||||
.evaluate((row, advance) => {
|
||||
let element = row.parentElement;
|
||||
while (element && !["auto", "scroll"].includes(getComputedStyle(element).overflowY))
|
||||
element = element.parentElement;
|
||||
if (!element) throw new Error("Import list has no scroll owner");
|
||||
element.scrollTop = advance ? element.scrollTop + element.clientHeight * 0.8 : 0;
|
||||
element.dispatchEvent(new Event("scroll"));
|
||||
}, forward);
|
||||
await this.page.getByTestId("import-session-list").evaluate((list, advance) => {
|
||||
list.scrollTop = advance ? list.scrollTop + list.clientHeight * 0.8 : 0;
|
||||
list.dispatchEvent(new Event("scroll"));
|
||||
}, forward);
|
||||
}
|
||||
async search(query: string) {
|
||||
await this.page.getByTestId("import-session-search").fill(query);
|
||||
@@ -120,11 +117,18 @@ export class ImportSessionFlow {
|
||||
await this.page.getByTestId("import-session-search").fill("");
|
||||
await expect(this.page.getByTestId("import-session-load-more")).toBeVisible();
|
||||
}
|
||||
/** Load more keeps the reader down the list instead of sending them back to the top. */
|
||||
async loadMore() {
|
||||
await this.page.getByTestId("import-session-load-more").click();
|
||||
const loadMore = this.page.getByTestId("import-session-load-more");
|
||||
const topRow = this.page.getByTestId((await this.rowIds())[0]!);
|
||||
await expect(topRow).toBeInViewport();
|
||||
await loadMore.click();
|
||||
// The next page is the last and short, so the button leaves once it lands.
|
||||
await expect(loadMore).toHaveCount(0);
|
||||
await expect(this.page.getByText("Loading sessions...", { exact: true })).toHaveCount(0);
|
||||
await expect(topRow).not.toBeInViewport();
|
||||
await this.revealSession("fixture-root-20");
|
||||
await expect(this.page.getByText("Root session 20", { exact: true })).toBeInViewport();
|
||||
await expect(this.page.getByTestId("import-session-load-more")).toHaveCount(0);
|
||||
}
|
||||
async retryProvider(provider: string, label: string) {
|
||||
await this.expectProviderError(label);
|
||||
|
||||
@@ -17,6 +17,11 @@ export function nextPageLimit(limit: number): number {
|
||||
return PAGE_LIMITS.find((candidate) => candidate > limit) ?? limit;
|
||||
}
|
||||
|
||||
/** The page Load more grew this limit from, or null for the first page. */
|
||||
export function previousPageLimit(limit: number): number | null {
|
||||
return PAGE_LIMITS.findLast((candidate) => candidate < limit) ?? null;
|
||||
}
|
||||
|
||||
export function requiresImportSessionsHostUpgrade(input: {
|
||||
supportsSnapshot: boolean;
|
||||
workspaceId?: string | null;
|
||||
|
||||
@@ -1172,6 +1172,44 @@ describe("ImportSessionSheet", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the loaded list in place while Load more fetches the next page", async () => {
|
||||
let releaseNextPage: () => void = () => undefined;
|
||||
const nextPage = new Promise<void>((resolve) => {
|
||||
releaseNextPage = resolve;
|
||||
});
|
||||
const fetchRecentProviderSessions = vi.fn(async (options: { limit?: number } | undefined) => {
|
||||
if (options?.limit !== 15) await nextPage;
|
||||
return {
|
||||
requestId: "recent-provider-sessions",
|
||||
entries: createPageOfEntries(options?.limit === 15 ? 15 : 20),
|
||||
};
|
||||
});
|
||||
|
||||
renderSheet(createRecentSessionsClient(fetchRecentProviderSessions, vi.fn()), {
|
||||
cwd: null,
|
||||
snapshot: { supportsSnapshot: true, entries: [createSnapshotEntry("claude")] },
|
||||
});
|
||||
|
||||
fireEvent.click(await screen.findByTestId("import-session-load-more"));
|
||||
// The list the user scrolled to Load more is the one they keep: replacing it would drop
|
||||
// them back at the top, behind a loading state, until the larger page lands.
|
||||
const list = screen.getByTestId("import-session-list");
|
||||
await waitFor(() => {
|
||||
expect(fetchRecentProviderSessions).toHaveBeenCalledWith({
|
||||
providers: ["claude"],
|
||||
limit: 45,
|
||||
});
|
||||
});
|
||||
expect(screen.getByTestId("import-session-list")).toBe(list);
|
||||
expect(screen.queryByText("Loading sessions...")).toBeNull();
|
||||
|
||||
releaseNextPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByTestId("import-session-load-more")).toBeNull();
|
||||
});
|
||||
expect(screen.getByTestId("import-session-list")).toBe(list);
|
||||
});
|
||||
|
||||
it("stops the refresh spinner and retries only the failed provider", async () => {
|
||||
const fetchRecentProviderSessions = vi.fn(
|
||||
async (options: { providers?: string[] } | undefined) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { Pressable, type PressableStateCallbackType, Text, View } from "react-native";
|
||||
import { keepPreviousData, useMutation, useQueries, useQueryClient } from "@tanstack/react-query";
|
||||
import { useMutation, useQueries, useQueryClient, type QueryClient } from "@tanstack/react-query";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import type {
|
||||
DaemonClient,
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
resolveDirectoryLabel,
|
||||
nextPageLimit,
|
||||
PER_PROVIDER_LIMIT,
|
||||
previousPageLimit,
|
||||
type ProviderErrorRow,
|
||||
resolveImportTarget,
|
||||
resolveProvidersToFetch,
|
||||
@@ -100,11 +101,12 @@ interface SessionsQueryConfig {
|
||||
// Query's default retry would keep the header spinner turning and pile up
|
||||
// in-flight daemon requests behind a dead provider (#2512).
|
||||
retry: false;
|
||||
placeholderData: typeof keepPreviousData;
|
||||
placeholderData: () => RecentSessionsResponse | undefined;
|
||||
queryFn: () => Promise<RecentSessionsResponse>;
|
||||
}
|
||||
|
||||
function buildSessionsQueriesConfig(args: {
|
||||
queryClient: QueryClient;
|
||||
providersToFetch: AgentProvider[] | null;
|
||||
visible: boolean;
|
||||
client: RecentProviderSessionsClient | null;
|
||||
@@ -113,14 +115,24 @@ function buildSessionsQueriesConfig(args: {
|
||||
limit: number;
|
||||
hostDisconnectedMessage?: string;
|
||||
}): SessionsQueryConfig[] {
|
||||
const { providersToFetch, visible, client, cwd, query, limit, hostDisconnectedMessage } = args;
|
||||
const { queryClient, providersToFetch, visible, client, cwd, query, limit } = args;
|
||||
const { hostDisconnectedMessage } = args;
|
||||
if (providersToFetch === null) return [];
|
||||
const enabled = visible && Boolean(client);
|
||||
const previousLimit = previousPageLimit(limit);
|
||||
return providersToFetch.map((provider) => ({
|
||||
queryKey: buildSessionsQueryKey({ cwd, query, limit, provider }),
|
||||
enabled,
|
||||
retry: false as const,
|
||||
placeholderData: keepPreviousData,
|
||||
// While Load more fetches the next page, keep showing the page it grew from. React Query's
|
||||
// keepPreviousData cannot do this here: useQueries gives a changed key a new observer, which
|
||||
// has no previous data, so the rows would vanish and the list would remount at the top.
|
||||
placeholderData: () =>
|
||||
previousLimit === null
|
||||
? undefined
|
||||
: queryClient.getQueryData<RecentSessionsResponse>(
|
||||
buildSessionsQueryKey({ cwd, query, limit: previousLimit, provider }),
|
||||
),
|
||||
queryFn: async () => {
|
||||
if (!client) {
|
||||
throw new Error(hostDisconnectedMessage ?? i18n.t("workspace.terminal.hostDisconnected"));
|
||||
@@ -522,6 +534,7 @@ export function ImportSessionSheet({
|
||||
const queriesConfig = useMemo(
|
||||
() =>
|
||||
buildSessionsQueriesConfig({
|
||||
queryClient,
|
||||
providersToFetch,
|
||||
visible,
|
||||
client,
|
||||
@@ -530,7 +543,7 @@ export function ImportSessionSheet({
|
||||
limit: pageLimit,
|
||||
hostDisconnectedMessage: t("workspace.terminal.hostDisconnected"),
|
||||
}),
|
||||
[providersToFetch, visible, client, scopeCwd, query, pageLimit, t],
|
||||
[queryClient, providersToFetch, visible, client, scopeCwd, query, pageLimit, t],
|
||||
);
|
||||
|
||||
const queries = useQueries({ queries: queriesConfig });
|
||||
|
||||
@@ -2017,6 +2017,7 @@ export const ar: TranslationResources = {
|
||||
hostUnavailable: "مضيف التنزيل غير متاح.",
|
||||
cancelled: "تم إلغاء التنزيل.",
|
||||
failed: "فشل تنزيل الملف.",
|
||||
httpFailed: "فشل تنزيل الملف (HTTP {{status}}).",
|
||||
shareFile: "مشاركة الملف",
|
||||
shareFileNamed: "مشاركة{{fileName}}",
|
||||
},
|
||||
|
||||
@@ -2045,6 +2045,7 @@ export const en = {
|
||||
hostUnavailable: "Download host is unavailable.",
|
||||
cancelled: "Download was cancelled.",
|
||||
failed: "Failed to download file.",
|
||||
httpFailed: "Failed to download file (HTTP {{status}}).",
|
||||
shareFile: "Share file",
|
||||
shareFileNamed: "Share {{fileName}}",
|
||||
},
|
||||
|
||||
@@ -2066,6 +2066,7 @@ export const es: TranslationResources = {
|
||||
hostUnavailable: "El host de descarga no está disponible.",
|
||||
cancelled: "La descarga fue cancelada.",
|
||||
failed: "No se pudo descargar el archivo.",
|
||||
httpFailed: "No se pudo descargar el archivo (HTTP {{status}}).",
|
||||
shareFile: "compartir archivo",
|
||||
shareFileNamed: "Compartir{{fileName}}",
|
||||
},
|
||||
|
||||
@@ -2065,6 +2065,7 @@ export const fr: TranslationResources = {
|
||||
hostUnavailable: "L’hôte de téléchargement est indisponible.",
|
||||
cancelled: "Téléchargement annulé.",
|
||||
failed: "Impossible de télécharger le fichier.",
|
||||
httpFailed: "Impossible de télécharger le fichier (HTTP {{status}}).",
|
||||
shareFile: "Partager le fichier",
|
||||
shareFileNamed: "Partager {{fileName}}",
|
||||
},
|
||||
|
||||
@@ -2038,6 +2038,7 @@ export const ja: TranslationResources = {
|
||||
hostUnavailable: "ダウンロードホストが利用できません。",
|
||||
cancelled: "ダウンロードがキャンセルされました。",
|
||||
failed: "ファイルのダウンロードに失敗しました。",
|
||||
httpFailed: "ファイルのダウンロードに失敗しました (HTTP {{status}}).",
|
||||
shareFile: "ファイルを共有",
|
||||
shareFileNamed: "{{fileName}}を共有",
|
||||
},
|
||||
|
||||
@@ -2028,6 +2028,7 @@ export const ko: TranslationResources = {
|
||||
hostUnavailable: "다운로드 호스트를 사용할 수 없습니다.",
|
||||
cancelled: "다운로드가 취소되었습니다.",
|
||||
failed: "파일을 다운로드하지 못했습니다.",
|
||||
httpFailed: "파일을 다운로드하지 못했습니다 (HTTP {{status}}).",
|
||||
shareFile: "파일 공유",
|
||||
shareFileNamed: "{{fileName}} 공유",
|
||||
},
|
||||
|
||||
@@ -2051,6 +2051,7 @@ export const ptBR: TranslationResources = {
|
||||
hostUnavailable: "Host de download indisponível.",
|
||||
cancelled: "Download cancelado.",
|
||||
failed: "Falha ao baixar arquivo.",
|
||||
httpFailed: "Falha ao baixar arquivo (HTTP {{status}}).",
|
||||
shareFile: "Compartilhar arquivo",
|
||||
shareFileNamed: "Compartilhar {{fileName}}",
|
||||
},
|
||||
|
||||
@@ -2051,6 +2051,7 @@ export const ru: TranslationResources = {
|
||||
hostUnavailable: "Хост загрузки недоступен.",
|
||||
cancelled: "Загрузка отменена.",
|
||||
failed: "Не удалось загрузить файл.",
|
||||
httpFailed: "Не удалось загрузить файл (HTTP {{status}}).",
|
||||
shareFile: "Поделиться файлом",
|
||||
shareFileNamed: "Поделиться {{fileName}}",
|
||||
},
|
||||
|
||||
@@ -1994,6 +1994,7 @@ export const zhCN: TranslationResources = {
|
||||
hostUnavailable: "下载 Host 不可用。",
|
||||
cancelled: "下载已取消。",
|
||||
failed: "下载文件失败。",
|
||||
httpFailed: "下载文件失败 (HTTP {{status}}).",
|
||||
shareFile: "共享文件",
|
||||
shareFileNamed: "共享 {{fileName}}",
|
||||
},
|
||||
|
||||
@@ -90,6 +90,7 @@ interface StartInput {
|
||||
token?: ReturnType<typeof createTokenRequest>;
|
||||
reader?: ReturnType<typeof createFileReader>;
|
||||
saver?: ReturnType<typeof createFileSaver>;
|
||||
fetchFile?: typeof fetch;
|
||||
}
|
||||
|
||||
async function startDownload(input: StartInput): Promise<Download | undefined> {
|
||||
@@ -108,6 +109,7 @@ async function startDownload(input: StartInput): Promise<Download | undefined> {
|
||||
requestFileDownloadToken: token.request,
|
||||
readFile: reader.read,
|
||||
saveFile: saver.save,
|
||||
fetchFile: input.fetchFile ?? (async () => new Response(FILE_BYTES)),
|
||||
});
|
||||
const { activeDownloadId, downloads } = useDownloadStore.getState();
|
||||
return activeDownloadId ? downloads.get(activeDownloadId) : undefined;
|
||||
@@ -203,6 +205,57 @@ describe("download store transport selection", () => {
|
||||
expect(saver.savedFiles).toEqual([]);
|
||||
});
|
||||
|
||||
it("fails with the HTTP status when a proxy in front of the host rejects the download", async () => {
|
||||
const requests: Array<RequestInit | undefined> = [];
|
||||
const download = await startDownload({
|
||||
profile: profileWith([localTcpConnection]),
|
||||
activeConnectionId: localTcpConnection.id,
|
||||
fetchFile: async (_url, init) => {
|
||||
requests.push(init);
|
||||
return new Response("<h1>401 Authorization Required</h1>", {
|
||||
status: 401,
|
||||
headers: { "WWW-Authenticate": 'Basic realm="Paseo"' },
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
expect(download).toMatchObject({
|
||||
status: "error",
|
||||
message: i18n.t("downloads.httpFailed", { status: 401 }),
|
||||
});
|
||||
expect(requests).toEqual([{ credentials: "include", redirect: "error" }]);
|
||||
});
|
||||
|
||||
it("fails with a readable message when the download request cannot reach the host", async () => {
|
||||
const download = await startDownload({
|
||||
profile: profileWith([localTcpConnection]),
|
||||
activeConnectionId: localTcpConnection.id,
|
||||
fetchFile: async () => {
|
||||
throw new TypeError("Failed to fetch");
|
||||
},
|
||||
});
|
||||
|
||||
expect(download).toMatchObject({ status: "error", message: i18n.t("downloads.failed") });
|
||||
});
|
||||
|
||||
it("fails with a readable message when the connection drops while the file is downloading", async () => {
|
||||
const saver = createFileSaver();
|
||||
const download = await startDownload({
|
||||
profile: profileWith([localTcpConnection]),
|
||||
activeConnectionId: localTcpConnection.id,
|
||||
fetchFile: async () =>
|
||||
new Response(
|
||||
new ReadableStream({
|
||||
start: (controller) => controller.error(new TypeError("network error")),
|
||||
}),
|
||||
),
|
||||
saver,
|
||||
});
|
||||
|
||||
expect(download).toMatchObject({ status: "error", message: i18n.t("downloads.failed") });
|
||||
expect(saver.savedFiles).toEqual([]);
|
||||
});
|
||||
|
||||
it("fails the download and saves nothing when the session read fails", async () => {
|
||||
const reader = createFileReader(async () => {
|
||||
throw new Error("Connection lost");
|
||||
|
||||
@@ -4,7 +4,6 @@ import * as LegacyFileSystem from "expo-file-system/legacy";
|
||||
import * as Sharing from "expo-sharing";
|
||||
import type { HostProfile } from "@/types/host-connection";
|
||||
import { buildDaemonWebSocketUrl } from "@/utils/daemon-endpoints";
|
||||
import { openExternalUrl } from "@/utils/open-external-url";
|
||||
import { isWeb } from "@/constants/platform";
|
||||
import { i18n } from "@/i18n/i18next";
|
||||
|
||||
@@ -59,6 +58,7 @@ interface DownloadState {
|
||||
/** Reads the whole file over the session socket; used when no direct HTTP endpoint is active. */
|
||||
readFile: (path: string, maxBytes: number) => Promise<{ bytes: Uint8Array; size: number }>;
|
||||
saveFile?: typeof saveDownloadedBytes;
|
||||
fetchFile?: typeof fetch;
|
||||
}) => Promise<void>;
|
||||
|
||||
updateProgress: (id: string, progress: DownloadProgress) => void;
|
||||
@@ -86,6 +86,7 @@ export const useDownloadStore = create<DownloadState>()((set, get) => ({
|
||||
requestFileDownloadToken,
|
||||
readFile,
|
||||
saveFile = saveDownloadedBytes,
|
||||
fetchFile = fetch,
|
||||
}) => {
|
||||
const id = generateDownloadId();
|
||||
const download: Download = {
|
||||
@@ -144,14 +145,11 @@ export const useDownloadStore = create<DownloadState>()((set, get) => ({
|
||||
throw new Error(i18n.t("downloads.hostUnavailable"));
|
||||
}
|
||||
|
||||
const downloadUrl = buildDownloadUrl(
|
||||
downloadTarget.baseUrl,
|
||||
tokenResponse.token,
|
||||
isWeb ? downloadTarget.authCredentials : null,
|
||||
);
|
||||
const downloadUrl = buildDownloadUrl(downloadTarget.baseUrl, tokenResponse.token);
|
||||
|
||||
if (isWeb) {
|
||||
triggerBrowserDownload(downloadUrl, resolvedFileName);
|
||||
const file = await fetchDownload(fetchFile, downloadUrl);
|
||||
saveBrowserFile(file, resolvedFileName);
|
||||
get().completeDownload(id);
|
||||
return;
|
||||
}
|
||||
@@ -198,11 +196,6 @@ export const useDownloadStore = create<DownloadState>()((set, get) => ({
|
||||
await shareSavedFile(result.uri, tokenResponse.mimeType, resolvedFileName);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : i18n.t("downloads.failed");
|
||||
if (isWeb) {
|
||||
console.warn("[DownloadStore] Download failed:", message);
|
||||
get().failDownload(id, message);
|
||||
return;
|
||||
}
|
||||
get().failDownload(id, message);
|
||||
}
|
||||
},
|
||||
@@ -283,7 +276,6 @@ function findMostRecentDownloadId(downloads: Map<string, Download>): string | nu
|
||||
interface DownloadTarget {
|
||||
baseUrl: string | null;
|
||||
authHeader: string | null;
|
||||
authCredentials: { username: string; password: string } | null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,7 +298,7 @@ function resolveDaemonDownloadTarget(
|
||||
buildDaemonWebSocketUrl(connection.endpoint, { useTls: connection.useTls ?? false }),
|
||||
);
|
||||
} catch {
|
||||
return { baseUrl: null, authHeader: null, authCredentials: null };
|
||||
return { baseUrl: null, authHeader: null };
|
||||
}
|
||||
|
||||
if (parsed.protocol === "ws:") {
|
||||
@@ -332,23 +324,41 @@ function resolveDaemonDownloadTarget(
|
||||
? `Basic ${btoa(`${authCredentials.username}:${authCredentials.password}`)}`
|
||||
: null;
|
||||
|
||||
return { baseUrl, authHeader, authCredentials };
|
||||
return { baseUrl, authHeader };
|
||||
}
|
||||
|
||||
function buildDownloadUrl(
|
||||
baseUrl: string,
|
||||
token: string,
|
||||
authCredentials: { username: string; password: string } | null,
|
||||
): string {
|
||||
function buildDownloadUrl(baseUrl: string, token: string): string {
|
||||
const url = new URL("/api/files/download", baseUrl);
|
||||
url.searchParams.set("token", token);
|
||||
if (authCredentials) {
|
||||
url.username = authCredentials.username;
|
||||
url.password = authCredentials.password;
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the file instead of navigating to it. The app runs on another origin than the host
|
||||
* (paseo://app on desktop), so a link to the download URL navigates the whole window, and any
|
||||
* response that is not the file (a proxy's 401 page, a 404) replaces the app.
|
||||
*/
|
||||
async function fetchDownload(fetchFile: typeof fetch, url: string): Promise<Blob> {
|
||||
let response: Response;
|
||||
try {
|
||||
// Send the same cookies the host's WebSocket handshake sends, so a proxy in front of the
|
||||
// host lets the download through. Refuse redirects: a proxy that redirects to a login page
|
||||
// would otherwise be saved as the file.
|
||||
response = await fetchFile(url, { credentials: "include", redirect: "error" });
|
||||
} catch {
|
||||
throw new Error(i18n.t("downloads.failed"));
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(i18n.t("downloads.httpFailed", { status: response.status }));
|
||||
}
|
||||
try {
|
||||
// The body streams after the headers, so the connection can still drop here.
|
||||
return await response.blob();
|
||||
} catch {
|
||||
throw new Error(i18n.t("downloads.failed"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves bytes read over the session. Web and Electron hand them to the browser's download
|
||||
* manager and return `null`; native writes the file and returns its URI for the share sheet.
|
||||
@@ -361,12 +371,7 @@ async function saveDownloadedBytes({
|
||||
if (isWeb) {
|
||||
const buffer = new ArrayBuffer(bytes.byteLength);
|
||||
new Uint8Array(buffer).set(bytes);
|
||||
const url = URL.createObjectURL(
|
||||
new Blob([buffer], { type: mimeType ?? "application/octet-stream" }),
|
||||
);
|
||||
triggerBrowserDownload(url, fileName);
|
||||
// The browser may still be reading the blob while its save prompt is open.
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
||||
saveBrowserFile(new Blob([buffer], { type: mimeType ?? "application/octet-stream" }), fileName);
|
||||
return null;
|
||||
}
|
||||
// Resolve and write in one tick so two downloads of the same name cannot pick the same path.
|
||||
@@ -390,14 +395,12 @@ async function shareSavedFile(
|
||||
}
|
||||
}
|
||||
|
||||
function triggerBrowserDownload(url: string, fileName: string) {
|
||||
function saveBrowserFile(file: Blob, fileName: string) {
|
||||
if (typeof document === "undefined") {
|
||||
if (typeof window !== "undefined") {
|
||||
void openExternalUrl(url);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const url = URL.createObjectURL(file);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = fileName;
|
||||
@@ -405,6 +408,8 @@ function triggerBrowserDownload(url: string, fileName: string) {
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
// The browser may still be reading the blob while its save prompt is open.
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
||||
}
|
||||
|
||||
function resolveDownloadTargetFile(fileName: string): FSFile {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildWorkspaceSource } from "./create.js";
|
||||
|
||||
@@ -8,6 +9,49 @@ describe("workspace create source", () => {
|
||||
).toEqual({ kind: "directory", path: "/tmp/project", projectId: "project-1" });
|
||||
});
|
||||
|
||||
it("resolves a relative path against the directory the command runs in", () => {
|
||||
const daemonTarget = { kind: "instance" as const, home: "/tmp/paseo-home" };
|
||||
expect(buildWorkspaceSource({ daemonTarget, isolation: "local", path: "." })).toEqual({
|
||||
kind: "directory",
|
||||
path: process.cwd(),
|
||||
});
|
||||
expect(
|
||||
buildWorkspaceSource({ daemonTarget, isolation: "worktree", path: "../sibling" }),
|
||||
).toEqual({
|
||||
kind: "worktree",
|
||||
cwd: path.resolve(process.cwd(), "../sibling"),
|
||||
action: "branch-off",
|
||||
});
|
||||
});
|
||||
|
||||
it("sends a relative path to an explicit daemon endpoint as written", () => {
|
||||
const daemonTarget = { kind: "endpoint" as const, host: "other-box:6767" };
|
||||
expect(buildWorkspaceSource({ daemonTarget, isolation: "local", path: "repo" })).toEqual({
|
||||
kind: "directory",
|
||||
path: "repo",
|
||||
});
|
||||
});
|
||||
|
||||
it("passes an absolute path from another operating system through unchanged", () => {
|
||||
expect(buildWorkspaceSource({ isolation: "local", path: "C:\\repos\\project" })).toEqual({
|
||||
kind: "directory",
|
||||
path: "C:\\repos\\project",
|
||||
});
|
||||
});
|
||||
|
||||
it("leaves a home-relative path for the daemon to expand", () => {
|
||||
const daemonTarget = { kind: "instance" as const, home: "/tmp/paseo-home" };
|
||||
expect(buildWorkspaceSource({ daemonTarget, isolation: "local", path: "~/repo" })).toEqual({
|
||||
kind: "directory",
|
||||
path: "~/repo",
|
||||
});
|
||||
expect(buildWorkspaceSource({ daemonTarget, isolation: "worktree", path: "~" })).toEqual({
|
||||
kind: "worktree",
|
||||
cwd: "~",
|
||||
action: "branch-off",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps branch names separate from managed worktree slugs", () => {
|
||||
expect(
|
||||
buildWorkspaceSource({
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { isAbsolute as isPosixAbsolute } from "node:path/posix";
|
||||
import { isAbsolute as isWindowsAbsolute } from "node:path/win32";
|
||||
import { resolve } from "node:path";
|
||||
import type { Command } from "commander";
|
||||
import { connectToDaemon, getDaemonHost } from "../../utils/client.js";
|
||||
import { resolveCallerAgentId } from "../../utils/caller-agent.js";
|
||||
import type { DaemonTarget } from "../../utils/daemon-target.js";
|
||||
import type { CommandError, CommandOptions, SingleResult } from "../../output/index.js";
|
||||
import { toWorkspaceRow, workspaceSchema, type WorkspaceRow } from "./shared.js";
|
||||
|
||||
@@ -121,12 +125,26 @@ function buildWorktreeWorkspaceSource(options: WorkspaceCreateOptions, path: str
|
||||
}
|
||||
}
|
||||
|
||||
// A relative --path names a directory relative to where the command runs, so
|
||||
// it is made absolute here for a local daemon, which would otherwise resolve
|
||||
// it against its own working directory. For an explicit endpoint the path is
|
||||
// sent as written, the same rule as resolveRepositoryDirectory. An absolute
|
||||
// path can target a daemon on a different operating system, and the daemon
|
||||
// expands a leading ~ to its own home directory.
|
||||
function resolveCallerPath(input: string, daemonTarget: DaemonTarget): string {
|
||||
const isHomeRelative = input === "~" || input.startsWith("~/");
|
||||
if (isHomeRelative || isPosixAbsolute(input) || isWindowsAbsolute(input)) return input;
|
||||
return daemonTarget.kind === "instance" ? resolve(input) : input;
|
||||
}
|
||||
|
||||
export function buildWorkspaceSource(options: WorkspaceCreateOptions) {
|
||||
const callerPath =
|
||||
options.path === undefined ? undefined : resolveCallerPath(options.path, options.daemonTarget);
|
||||
if (options.isolation === "local") {
|
||||
return buildLocalWorkspaceSource(options, options.path ?? process.cwd());
|
||||
return buildLocalWorkspaceSource(options, callerPath ?? process.cwd());
|
||||
}
|
||||
if (options.isolation === "worktree") {
|
||||
const sourcePath = options.path ?? (options.project ? undefined : process.cwd());
|
||||
const sourcePath = callerPath ?? (options.project ? undefined : process.cwd());
|
||||
return buildWorktreeWorkspaceSource(options, sourcePath);
|
||||
}
|
||||
throw new Error(`Unsupported workspace isolation: ${String(options.isolation)}`);
|
||||
|
||||
@@ -3473,6 +3473,101 @@ test("cancelAgentRun succeeds when the provider queues completion before rejecti
|
||||
}
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ binding: "foreground", agentId: "00000000-0000-4000-8000-000000000307" },
|
||||
{ binding: "autonomous", agentId: "00000000-0000-4000-8000-000000000308" },
|
||||
] as const)(
|
||||
"cancelAgentRun settles a $binding run bound to an already finalized turn",
|
||||
async ({ binding, agentId }) => {
|
||||
const fixture = await createControlledInterruptFixture({
|
||||
name: `interrupt-finalized-${binding}`,
|
||||
agentId,
|
||||
turnId: "finalized-turn",
|
||||
interrupt: async () => {},
|
||||
});
|
||||
|
||||
try {
|
||||
await fixture.startForegroundRun();
|
||||
await expect(fixture.manager.cancelAgentRun(fixture.agentId)).resolves.toEqual({
|
||||
status: "settled",
|
||||
});
|
||||
|
||||
if (binding === "foreground") {
|
||||
await fixture.startForegroundRun();
|
||||
} else {
|
||||
const running = waitForAgentLifecycle(fixture.manager, fixture.agentId, "running");
|
||||
fixture.session.pushEvent({
|
||||
type: "turn_started",
|
||||
provider: "codex",
|
||||
turnId: "finalized-turn",
|
||||
});
|
||||
await running;
|
||||
}
|
||||
|
||||
const cancellation = await Promise.race([
|
||||
fixture.manager.cancelAgentRun(fixture.agentId),
|
||||
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 3_000)),
|
||||
]);
|
||||
expect(cancellation).toEqual({ status: "settled" });
|
||||
expect(fixture.manager.getAgent(fixture.agentId)).toMatchObject({
|
||||
lifecycle: "idle",
|
||||
activeForegroundTurnId: null,
|
||||
});
|
||||
|
||||
await expect(fixture.manager.cancelAgentRun(fixture.agentId)).resolves.toEqual({
|
||||
status: "not_running",
|
||||
});
|
||||
await fixture.startForegroundRun();
|
||||
expect(fixture.manager.getAgent(fixture.agentId)).toMatchObject({
|
||||
lifecycle: "running",
|
||||
activeForegroundTurnId: "finalized-turn",
|
||||
});
|
||||
} finally {
|
||||
await fixture.cleanup();
|
||||
}
|
||||
},
|
||||
10_000,
|
||||
);
|
||||
|
||||
test("the provider's terminal for a run bound to an already finalized turn settles the run", async () => {
|
||||
const fixture = await createControlledInterruptFixture({
|
||||
name: "interrupt-finalized-provider-terminal",
|
||||
agentId: "00000000-0000-4000-8000-000000000309",
|
||||
turnId: "finalized-provider-terminal-turn",
|
||||
interrupt: async () => {},
|
||||
});
|
||||
|
||||
try {
|
||||
await fixture.startForegroundRun();
|
||||
await expect(fixture.manager.cancelAgentRun(fixture.agentId)).resolves.toEqual({
|
||||
status: "settled",
|
||||
});
|
||||
|
||||
await fixture.startForegroundRun();
|
||||
const idle = waitForAgentLifecycle(fixture.manager, fixture.agentId, "idle");
|
||||
fixture.session.pushEvent({
|
||||
type: "turn_completed",
|
||||
provider: "codex",
|
||||
turnId: "finalized-provider-terminal-turn",
|
||||
});
|
||||
const settlement = await Promise.race([
|
||||
idle.then(() => "idle" as const),
|
||||
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 1_000)),
|
||||
]);
|
||||
|
||||
expect(settlement).toBe("idle");
|
||||
expect(fixture.manager.getAgent(fixture.agentId)).toMatchObject({
|
||||
lifecycle: "idle",
|
||||
activeForegroundTurnId: null,
|
||||
});
|
||||
await expect(fixture.manager.cancelAgentRun(fixture.agentId)).resolves.toEqual({
|
||||
status: "not_running",
|
||||
});
|
||||
} finally {
|
||||
await fixture.cleanup();
|
||||
}
|
||||
}, 10_000);
|
||||
|
||||
test("listProviderAvailability uses registered client keys, including custom providers", async () => {
|
||||
const customClient: AgentClient = {
|
||||
provider: "zai",
|
||||
|
||||
@@ -5132,7 +5132,8 @@ export class AgentManager {
|
||||
if (
|
||||
eventTurnId &&
|
||||
isTurnTerminalEvent(event) &&
|
||||
this.runs.hasFinalizedTurn(agent, eventTurnId)
|
||||
this.runs.hasFinalizedTurn(agent, eventTurnId) &&
|
||||
this.runs.getTurnId(agent.id) !== eventTurnId
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
import { beforeAll, beforeEach, describe, expect, test } from "vitest";
|
||||
import pino from "pino";
|
||||
|
||||
import type { AgentSession, AgentStreamEvent } from "../agent-sdk-types.js";
|
||||
import {
|
||||
canRunRealProvider,
|
||||
createRealProviderClient,
|
||||
getRealProviderConfig,
|
||||
} from "../../daemon-e2e/real-provider-test-config.js";
|
||||
import { streamSession } from "./test-utils/session-stream-adapter.js";
|
||||
|
||||
function isTerminalEvent(event: AgentStreamEvent): boolean {
|
||||
return (
|
||||
event.type === "turn_completed" ||
|
||||
event.type === "turn_failed" ||
|
||||
event.type === "turn_canceled"
|
||||
);
|
||||
}
|
||||
|
||||
async function runTurn(session: AgentSession, prompt: string): Promise<AgentStreamEvent> {
|
||||
for await (const event of streamSession(session, prompt)) {
|
||||
if (isTerminalEvent(event)) return event;
|
||||
}
|
||||
throw new Error("turn ended without a terminal event");
|
||||
}
|
||||
|
||||
describe("opencode agent whose native session is gone (real)", () => {
|
||||
let canRun = false;
|
||||
|
||||
beforeAll(async () => {
|
||||
canRun = await canRunRealProvider("opencode");
|
||||
});
|
||||
|
||||
beforeEach((context) => {
|
||||
if (!canRun) context.skip();
|
||||
});
|
||||
|
||||
test("a resumed agent whose OpenCode session no longer exists continues in a new session", async () => {
|
||||
const client = createRealProviderClient("opencode", pino({ level: "silent" }));
|
||||
const config = { ...getRealProviderConfig("opencode"), cwd: process.cwd() };
|
||||
|
||||
// A non-persistent session deletes its native OpenCode session on close, which
|
||||
// leaves the handle naming a session OpenCode no longer has.
|
||||
const lost = await client.createSession(config, undefined, { persistSession: false });
|
||||
const handle = lost.describePersistence();
|
||||
await lost.close();
|
||||
expect(handle).not.toBeNull();
|
||||
|
||||
const resumed = await client.resumeSession(handle!, config);
|
||||
try {
|
||||
const turn = await runTurn(resumed, "Reply with exactly: OK");
|
||||
|
||||
expect(turn).toMatchObject({ type: "turn_completed" });
|
||||
expect(resumed.describePersistence()?.sessionId).not.toBe(handle!.sessionId);
|
||||
} finally {
|
||||
await resumed.close().catch(() => undefined);
|
||||
}
|
||||
}, 120_000);
|
||||
});
|
||||
@@ -814,79 +814,6 @@ describe("OpenCodeAgentClient adapter smoke tests", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("resuming an agent whose OpenCode session is gone continues in a new session", async () => {
|
||||
const runtime = new TestOpenCodeHarness();
|
||||
const execution = new TestOpenCodeClient();
|
||||
execution.sessionGetResponse = {
|
||||
error: { name: "NotFoundError", data: { message: "Session not found: ses_lost" } },
|
||||
};
|
||||
execution.sessionCreateResponse = { data: { id: "ses_fresh" } };
|
||||
execution.sessionPromptAsyncImplementation = async (parameters) => {
|
||||
const { sessionID } = parameters as { sessionID: string };
|
||||
if (sessionID === "ses_lost") {
|
||||
return {
|
||||
error: { name: "NotFoundError", data: { message: "Session not found: ses_lost" } },
|
||||
};
|
||||
}
|
||||
execution.emitEvent({ type: "session.idle", properties: { sessionID } });
|
||||
return {};
|
||||
};
|
||||
runtime.enqueueClient(execution);
|
||||
const client = new OpenCodeAgentClient(logger, undefined, {
|
||||
serverManager: runtime,
|
||||
createClient: runtime.createClient,
|
||||
});
|
||||
const cwd = tmpCwd();
|
||||
try {
|
||||
const session = await client.resumeSession({
|
||||
provider: "opencode",
|
||||
sessionId: "ses_lost",
|
||||
metadata: { cwd },
|
||||
});
|
||||
try {
|
||||
const turn = await collectTurnEvents(streamSession(session, "Are you there?"));
|
||||
|
||||
expect(turn.turnCompleted).toBe(true);
|
||||
expect(session.describePersistence()?.sessionId).toBe("ses_fresh");
|
||||
expect(session.persistenceUpdatedOnResume).toBe(true);
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
} finally {
|
||||
rmSync(cwd, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("loading history of an agent whose OpenCode session is gone creates no session", async () => {
|
||||
const runtime = new TestOpenCodeHarness();
|
||||
const execution = new TestOpenCodeClient();
|
||||
execution.sessionGetResponse = {
|
||||
error: { name: "NotFoundError", data: { message: "Session not found: ses_lost" } },
|
||||
};
|
||||
runtime.enqueueClient(execution);
|
||||
const client = new OpenCodeAgentClient(logger, undefined, {
|
||||
serverManager: runtime,
|
||||
createClient: runtime.createClient,
|
||||
});
|
||||
const cwd = tmpCwd();
|
||||
try {
|
||||
const session = await client.resumeSession(
|
||||
{ provider: "opencode", sessionId: "ses_lost", metadata: { cwd } },
|
||||
undefined,
|
||||
undefined,
|
||||
{ purpose: "history" },
|
||||
);
|
||||
try {
|
||||
expect(execution.calls.sessionCreate).toEqual([]);
|
||||
expect(session.describePersistence()?.sessionId).toBe("ses_lost");
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
} finally {
|
||||
rmSync(cwd, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("fetchCatalog returns models with required fields", async () => {
|
||||
const runtime = new TestOpenCodeHarness();
|
||||
const openCodeClient = new TestOpenCodeClient();
|
||||
|
||||
@@ -546,15 +546,6 @@ function isOpenCodeNotFoundError(error: unknown): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
async function isOpenCodeSessionMissing(
|
||||
client: Pick<OpencodeClient, "session">,
|
||||
sessionId: string,
|
||||
directory: string,
|
||||
): Promise<boolean> {
|
||||
const response = await client.session.get({ sessionID: sessionId, directory });
|
||||
return isOpenCodeNotFoundError(response.error);
|
||||
}
|
||||
|
||||
async function abortOpenCodeSession(params: {
|
||||
client: Pick<OpencodeClient, "session">;
|
||||
sessionId: string;
|
||||
@@ -1466,18 +1457,8 @@ export class OpenCodeAgentClient implements AgentClient {
|
||||
options?: AgentCreateSessionOptions,
|
||||
): Promise<AgentSession> {
|
||||
const openCodeConfig = this.assertConfig(config);
|
||||
const connection = await this.connectServer(openCodeConfig, launchContext);
|
||||
return this.startNewSession(connection, config, openCodeConfig, launchContext, options);
|
||||
}
|
||||
|
||||
private async startNewSession(
|
||||
connection: OpenCodeServerConnection,
|
||||
config: AgentSessionConfig,
|
||||
openCodeConfig: OpenCodeAgentConfig,
|
||||
launchContext?: AgentLaunchContext,
|
||||
options?: AgentCreateSessionOptions,
|
||||
): Promise<OpenCodeAgentSession> {
|
||||
const connectServer = this.connectServer.bind(this, openCodeConfig, launchContext);
|
||||
const connection = await connectServer();
|
||||
const { client } = connection;
|
||||
|
||||
// OpenCode stores permission rules on the session, so they are set here and on resume
|
||||
@@ -1541,7 +1522,7 @@ export class OpenCodeAgentClient implements AgentClient {
|
||||
handle: AgentPersistenceHandle,
|
||||
overrides?: Partial<AgentSessionConfig>,
|
||||
launchContext?: AgentLaunchContext,
|
||||
options?: AgentResumeSessionOptions,
|
||||
_options?: AgentResumeSessionOptions,
|
||||
): Promise<AgentSession> {
|
||||
const metadata = (handle.metadata ?? {}) as Partial<AgentSessionConfig>;
|
||||
const cwd = overrides?.cwd ?? metadata.cwd;
|
||||
@@ -1566,32 +1547,6 @@ export class OpenCodeAgentClient implements AgentClient {
|
||||
: await connectServer();
|
||||
const { client } = connection;
|
||||
|
||||
// History loads stay read-only, so only an interactive resume replaces a lost session.
|
||||
let sessionMissing = false;
|
||||
if ((options?.purpose ?? "interactive") === "interactive") {
|
||||
try {
|
||||
sessionMissing = await isOpenCodeSessionMissing(
|
||||
client,
|
||||
handle.sessionId,
|
||||
openCodeConfig.cwd,
|
||||
);
|
||||
} catch (error) {
|
||||
await connection.release();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
if (sessionMissing) {
|
||||
// Every prompt to a session OpenCode no longer has fails, so the agent
|
||||
// continues in a new native session instead of staying unusable.
|
||||
this.logger.warn(
|
||||
{ sessionId: handle.sessionId },
|
||||
"OpenCode session no longer exists; continuing in a new session",
|
||||
);
|
||||
const session = await this.startNewSession(connection, config, openCodeConfig, launchContext);
|
||||
session.persistenceUpdatedOnResume = true;
|
||||
return session;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.applySessionPermissionRules(client, openCodeConfig, handle.sessionId);
|
||||
await this.populateModelContextWindowCache(client, openCodeConfig.cwd);
|
||||
@@ -3425,8 +3380,6 @@ interface OpenCodeServerConnection {
|
||||
}
|
||||
|
||||
class OpenCodeAgentSession implements AgentSession {
|
||||
/** Set when resume replaced a lost native session, so the new handle is saved at once. */
|
||||
persistenceUpdatedOnResume = false;
|
||||
readonly provider = "opencode" as const;
|
||||
readonly capabilities = OPENCODE_CAPABILITIES;
|
||||
readonly openOptionsPatch: Record<string, unknown>;
|
||||
|
||||
@@ -128,7 +128,7 @@ export async function fanOutReconciledWorkspaceUpdates(input: {
|
||||
);
|
||||
}
|
||||
|
||||
import { VoiceAssistantWebSocketServer } from "./websocket-server.js";
|
||||
import { VoiceAssistantWebSocketServer, isDaemonOriginAllowed } from "./websocket-server.js";
|
||||
import { WorkspaceSetupRuntime } from "./workspace-setup-runtime.js";
|
||||
import { createWorkspaceLabelService } from "./workspace-labels/index.js";
|
||||
import { createGitHubService } from "../services/github-service.js";
|
||||
@@ -813,7 +813,7 @@ export async function createPaseoDaemon(
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const origin = req.headers.origin;
|
||||
if (origin && (allowedOrigins.has("*") || allowedOrigins.has(origin))) {
|
||||
if (origin && isDaemonOriginAllowed(origin, req.headers.host ?? null, allowedOrigins)) {
|
||||
res.setHeader("Access-Control-Allow-Origin", origin);
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
|
||||
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
symlinkSync,
|
||||
} from "fs";
|
||||
import { once } from "node:events";
|
||||
import { get } from "node:http";
|
||||
import { get, type IncomingMessage } from "node:http";
|
||||
import { tmpdir } from "os";
|
||||
import path from "path";
|
||||
import { createDaemonTestContext, type DaemonTestContext } from "../test-utils/index.js";
|
||||
@@ -69,6 +69,29 @@ describe("daemon E2E", () => {
|
||||
}, 60000);
|
||||
|
||||
describe("file download tokens", () => {
|
||||
test("lets a page on a loopback alias of the request's host and port fetch a download", async () => {
|
||||
const cwd = tmpCwd();
|
||||
writeFileSync(path.join(cwd, "download.bin"), "download bytes");
|
||||
try {
|
||||
// The web UI behind a remapped port (for example Docker -p 8080:6767): the page is at
|
||||
// http://127.0.0.1:8080 and the app sends the request to localhost:8080. The WebSocket
|
||||
// accepts this origin, so the download must as well.
|
||||
const token = await ctx.client.requestDownloadToken(cwd, "download.bin");
|
||||
const request = get({
|
||||
host: "127.0.0.1",
|
||||
port: ctx.daemon.port,
|
||||
path: `/api/files/download?token=${token.token}`,
|
||||
headers: { Host: "localhost:8080", Origin: "http://127.0.0.1:8080" },
|
||||
});
|
||||
const [response] = (await once(request, "response")) as [IncomingMessage];
|
||||
response.resume();
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.headers["access-control-allow-origin"]).toBe("http://127.0.0.1:8080");
|
||||
} finally {
|
||||
rmSync(cwd, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test.skipIf(process.platform !== "linux")(
|
||||
"releases file descriptors after repeated HTTP download cancellations",
|
||||
async () => {
|
||||
|
||||
@@ -998,9 +998,7 @@ export class VoiceAssistantWebSocketServer {
|
||||
callback(false, 403, "Host not allowed");
|
||||
return;
|
||||
}
|
||||
const sameOrigin = isWebSocketSameOrigin(origin, requestHost);
|
||||
|
||||
if (!origin || allowedOrigins.has("*") || allowedOrigins.has(origin) || sameOrigin) {
|
||||
if (!origin || isDaemonOriginAllowed(origin, requestHost, allowedOrigins)) {
|
||||
callback(true);
|
||||
} else {
|
||||
this.incrementRuntimeCounter("originRejected");
|
||||
@@ -3267,6 +3265,22 @@ function isLoopbackAlias(hostname: string): boolean {
|
||||
return /^127(?:\.\d{1,3}){3}$/.test(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a page at `origin` may talk to the daemon reached at `requestHost`. The WebSocket
|
||||
* upgrade and the HTTP CORS check both use it, so a page that can connect can also fetch.
|
||||
*/
|
||||
export function isDaemonOriginAllowed(
|
||||
origin: string,
|
||||
requestHost: string | null,
|
||||
allowedOrigins: Set<string>,
|
||||
): boolean {
|
||||
return (
|
||||
allowedOrigins.has("*") ||
|
||||
allowedOrigins.has(origin) ||
|
||||
isWebSocketSameOrigin(origin, requestHost)
|
||||
);
|
||||
}
|
||||
|
||||
export function isWebSocketSameOrigin(
|
||||
origin: string | undefined,
|
||||
requestHost: string | null,
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { existsSync } from "node:fs";
|
||||
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { copyWorktreeIncludes } from "./copy.js";
|
||||
|
||||
let root: string;
|
||||
let source: string;
|
||||
let worktree: string;
|
||||
|
||||
async function writeSource(files: Record<string, string>): Promise<void> {
|
||||
for (const [file, contents] of Object.entries(files)) {
|
||||
await mkdir(dirname(join(source, file)), { recursive: true });
|
||||
await writeFile(join(source, file), contents);
|
||||
}
|
||||
}
|
||||
|
||||
// Total directories Git visits while listing files, read from its trace2 events.
|
||||
async function directoriesVisited(run: () => Promise<unknown>): Promise<number> {
|
||||
const trace = join(root, "trace.json");
|
||||
process.env.GIT_TRACE2_EVENT = trace;
|
||||
try {
|
||||
await run();
|
||||
} finally {
|
||||
delete process.env.GIT_TRACE2_EVENT;
|
||||
}
|
||||
return (await readFile(trace, "utf8"))
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) => JSON.parse(line) as { event: string; key?: string; value?: string })
|
||||
.filter((event) => event.event === "data" && event.key === "directories-visited")
|
||||
.reduce((total, event) => total + Number(event.value), 0);
|
||||
}
|
||||
|
||||
async function copied(): Promise<string[]> {
|
||||
const result = await copyWorktreeIncludes(worktree, source);
|
||||
if (result.kind !== "copied") throw new Error(`expected a copy, got ${result.kind}`);
|
||||
return [...result.copied].sort();
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
root = await mkdtemp(join(tmpdir(), "worktree-include-"));
|
||||
source = join(root, "source");
|
||||
worktree = join(root, "worktree");
|
||||
await mkdir(source);
|
||||
await mkdir(worktree);
|
||||
execFileSync("git", ["init", "-q", source]);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe("copyWorktreeIncludes", () => {
|
||||
it("does not search inside an ignored directory that no pattern names", async () => {
|
||||
await writeSource({
|
||||
".gitignore": "node_modules/\n.env\n",
|
||||
".worktreeinclude": ".env\n",
|
||||
".env": "SECRET=root",
|
||||
"apps/web/.env": "SECRET=web",
|
||||
"node_modules/pkg/.env": "SECRET=package",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual([".env", "apps/web/.env"]);
|
||||
expect(existsSync(join(worktree, "node_modules"))).toBe(false);
|
||||
});
|
||||
|
||||
it("searches inside ignored directories for a pattern that starts with **/", async () => {
|
||||
await writeSource({
|
||||
".gitignore": "node_modules/\n.env\n",
|
||||
".worktreeinclude": "**/.env\n",
|
||||
".env": "SECRET=root",
|
||||
"node_modules/pkg/.env": "SECRET=package",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual([".env", "node_modules/pkg/.env"]);
|
||||
});
|
||||
|
||||
it("copies a file in an untracked directory once when a **/ pattern also searches it", async () => {
|
||||
await writeSource({
|
||||
".gitignore": ".env\n",
|
||||
".worktreeinclude": "**/.env\nnew-package/.env\n",
|
||||
"new-package/.env": "SECRET=new",
|
||||
});
|
||||
|
||||
const result = await copyWorktreeIncludes(worktree, source);
|
||||
expect(result).toEqual({
|
||||
kind: "copied",
|
||||
copied: ["new-package/.env"],
|
||||
skipped: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("copies a path a pattern names inside an ignored directory", async () => {
|
||||
await writeSource({
|
||||
".gitignore": "node_modules/\n",
|
||||
".worktreeinclude": "node_modules/pkg/.env\n",
|
||||
"node_modules/pkg/.env": "SECRET=package",
|
||||
"node_modules/other/.env": "SECRET=other",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual(["node_modules/pkg/.env"]);
|
||||
});
|
||||
|
||||
it("copies an ignored directory that a pattern names", async () => {
|
||||
await writeSource({
|
||||
".gitignore": ".secrets/\nbuild/\n",
|
||||
".worktreeinclude": ".secrets/\n",
|
||||
".secrets/a.key": "a",
|
||||
".secrets/nested/b.key": "b",
|
||||
"build/out.js": "built",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual([".secrets/a.key", ".secrets/nested/b.key"]);
|
||||
});
|
||||
|
||||
it("searches inside an ignored directory for a path a pattern names", async () => {
|
||||
await writeSource({
|
||||
".gitignore": "config/\n",
|
||||
".worktreeinclude": "config/*.local.json\n!config/production.local.json\n",
|
||||
"config/dev.local.json": "dev",
|
||||
"config/production.local.json": "production",
|
||||
"config/defaults.json": "defaults",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual(["config/dev.local.json"]);
|
||||
expect(await readFile(join(worktree, "config/dev.local.json"), "utf8")).toBe("dev");
|
||||
});
|
||||
|
||||
it("copies ignored files inside an untracked directory", async () => {
|
||||
await writeSource({
|
||||
".gitignore": ".env\n",
|
||||
".worktreeinclude": ".env\n",
|
||||
"new-package/.env": "SECRET=new",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual(["new-package/.env"]);
|
||||
});
|
||||
|
||||
it("copies nothing that is tracked or not ignored", async () => {
|
||||
await writeSource({
|
||||
".gitignore": ".env\n",
|
||||
".worktreeinclude": ".env\nnotes.txt\n",
|
||||
"notes.txt": "not ignored",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual([]);
|
||||
});
|
||||
|
||||
it("searches only the path a glob-free pattern names directly inside an ignored directory", async () => {
|
||||
const packages: Record<string, string> = {};
|
||||
for (let i = 0; i < 20; i++) packages[`node_modules/pkg${i}/lib/index.js`] = "code";
|
||||
await writeSource({
|
||||
".gitignore": "node_modules/\n",
|
||||
".worktreeinclude": "node_modules/.cache\n",
|
||||
"node_modules/.cache/state.json": "cache",
|
||||
...packages,
|
||||
});
|
||||
|
||||
let result: Awaited<ReturnType<typeof copyWorktreeIncludes>> | undefined;
|
||||
const visited = await directoriesVisited(async () => {
|
||||
result = await copyWorktreeIncludes(worktree, source);
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
kind: "copied",
|
||||
copied: ["node_modules/.cache/state.json"],
|
||||
skipped: [],
|
||||
});
|
||||
expect(visited).toBeLessThan(10);
|
||||
});
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"copies includes when an ignored file is named like a Git pathspec",
|
||||
async () => {
|
||||
await writeSource({
|
||||
".gitignore": "*.log\n.env\n",
|
||||
".worktreeinclude": ".env\n",
|
||||
".env": "SECRET=root",
|
||||
":(oops.log": "log",
|
||||
":!c.log": "log",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual([".env"]);
|
||||
},
|
||||
);
|
||||
|
||||
it("ignores exclude patterns from the user's git template", async () => {
|
||||
const template = join(root, "template");
|
||||
await mkdir(join(template, "info"), { recursive: true });
|
||||
await writeFile(join(template, "info", "exclude"), "*.log\n");
|
||||
await writeSource({
|
||||
".gitignore": "*.log\n.env\n",
|
||||
".worktreeinclude": ".env\n",
|
||||
".env": "SECRET=root",
|
||||
"debug.log": "log",
|
||||
});
|
||||
|
||||
process.env.GIT_TEMPLATE_DIR = template;
|
||||
try {
|
||||
expect(await copied()).toEqual([".env"]);
|
||||
} finally {
|
||||
delete process.env.GIT_TEMPLATE_DIR;
|
||||
}
|
||||
});
|
||||
|
||||
it("does not search an ignored directory deeper than a pattern without ** can reach", async () => {
|
||||
const packages: Record<string, string> = {};
|
||||
for (let i = 0; i < 20; i++) packages[`apps/web/node_modules/pkg${i}/lib/index.js`] = "code";
|
||||
await writeSource({
|
||||
".gitignore": "node_modules/\n.env\n",
|
||||
".worktreeinclude": "apps/*/.env\n",
|
||||
"apps/web/.env": "SECRET=web",
|
||||
...packages,
|
||||
});
|
||||
|
||||
let result: Awaited<ReturnType<typeof copyWorktreeIncludes>> | undefined;
|
||||
const visited = await directoriesVisited(async () => {
|
||||
result = await copyWorktreeIncludes(worktree, source);
|
||||
});
|
||||
|
||||
expect(result).toEqual({ kind: "copied", copied: ["apps/web/.env"], skipped: [] });
|
||||
expect(visited).toBeLessThan(10);
|
||||
});
|
||||
|
||||
it("reads the first pattern after a UTF-8 byte order mark", async () => {
|
||||
await writeSource({
|
||||
".gitignore": "node_modules/\n",
|
||||
".worktreeinclude": "\uFEFFnode_modules/.cache/\n",
|
||||
"node_modules/.cache/state.json": "cache",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual(["node_modules/.cache/state.json"]);
|
||||
});
|
||||
|
||||
it("matches patterns case-insensitively when the source repo ignores case", async () => {
|
||||
execFileSync("git", ["-C", source, "config", "core.ignoreCase", "true"]);
|
||||
await writeSource({
|
||||
".gitignore": ".env\nconfig/\n",
|
||||
".worktreeinclude": ".ENV\nCONFIG/a.json\n",
|
||||
".env": "SECRET=root",
|
||||
"config/a.json": "a",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual([".env", "config/a.json"]);
|
||||
});
|
||||
|
||||
it("searches a pattern's path in the case it has on disk when the source repo ignores case", async () => {
|
||||
execFileSync("git", ["-C", source, "config", "core.ignoreCase", "true"]);
|
||||
await writeSource({
|
||||
".gitignore": "Config/\nÉTÉ/\n",
|
||||
".worktreeinclude": "config/a.json\nÉTÉ/sub/.env\n",
|
||||
"Config/a.json": "a",
|
||||
"ÉTÉ/sub/.env": "SECRET=summer",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual(["Config/a.json", "ÉTÉ/sub/.env"]);
|
||||
});
|
||||
|
||||
// Each searched folder is one argument to Git. Windows caps a command line at 32,767
|
||||
// characters and Linux at 2 MB, so each platform gets enough folders to exceed its own
|
||||
// limit, with names short enough for Windows paths.
|
||||
it("searches more ignored folders than fit on one command line", async () => {
|
||||
const folders = process.platform === "win32" ? 300 : 15_000;
|
||||
const name = (i: number) => `${"x".repeat(150)}${i}`;
|
||||
await writeSource({
|
||||
".gitignore": "x*/\n.env\n",
|
||||
".worktreeinclude": "**/.env\n",
|
||||
[`${name(folders - 1)}/.env`]: "SECRET=last",
|
||||
});
|
||||
for (let i = 0; i < folders - 1; i++) await mkdir(join(source, name(i)));
|
||||
await Promise.all(
|
||||
Array.from({ length: folders - 1 }, (_, i) => writeFile(join(source, name(i), "a"), "")),
|
||||
);
|
||||
|
||||
expect(await copied()).toEqual([`${name(folders - 1)}/.env`]);
|
||||
}, 60_000);
|
||||
|
||||
it("copies ignored files inside an untracked directory a pattern names", async () => {
|
||||
await writeSource({
|
||||
".gitignore": ".env\n",
|
||||
".worktreeinclude": "newdir/\n",
|
||||
"newdir/.env": "SECRET=new",
|
||||
"newdir/notes.txt": "untracked",
|
||||
});
|
||||
|
||||
expect(await copied()).toEqual(["newdir/.env"]);
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,8 @@
|
||||
// Copyright (c) 2026 Carlos Precioso.
|
||||
import { execFile } from "node:child_process";
|
||||
import { constants, existsSync } from "node:fs";
|
||||
import { copyFile, mkdir } from "node:fs/promises";
|
||||
import { copyFile, mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
@@ -22,37 +23,33 @@ async function git(cwd: string, args: string[], input?: string): Promise<string>
|
||||
return stdout;
|
||||
}
|
||||
|
||||
async function gitAllowExit1(cwd: string, args: string[], input: string): Promise<string> {
|
||||
// `git check-ignore --stdin` reads each path as a pathspec, so a name such as `:(x`
|
||||
// would be parsed as pathspec magic. A `./` prefix keeps every path literal.
|
||||
async function checkIgnore(cwd: string, args: string[], paths: string[]): Promise<string[]> {
|
||||
const input = paths.map((path) => `./${path}\0`).join("");
|
||||
let output: string;
|
||||
try {
|
||||
return await git(cwd, args, input);
|
||||
output = await git(cwd, [...args, "check-ignore", "--stdin", "-z"], input);
|
||||
} catch (error) {
|
||||
// `git check-ignore` exits 1 when nothing matched.
|
||||
if ((error as { code?: number }).code === 1) return "";
|
||||
if ((error as { code?: number }).code === 1) return [];
|
||||
throw error;
|
||||
}
|
||||
return splitNul(output).map((path) => path.replace(/^\.\//, ""));
|
||||
}
|
||||
|
||||
const splitNul = (s: string) => s.split("\0").filter(Boolean);
|
||||
const isDirectory = (entry: string) => entry.endsWith("/");
|
||||
|
||||
// Copy files that match .worktreeinclude (gitignore syntax)
|
||||
// AND are gitignored, so tracked files are never touched.
|
||||
export async function copyWorktreeIncludes(worktree: string, source: string): Promise<CopyResult> {
|
||||
if (!existsSync(join(source, INCLUDE_FILE))) return { kind: "no-include-file" };
|
||||
|
||||
const matches = splitNul(
|
||||
await git(source, [
|
||||
"ls-files",
|
||||
"--others",
|
||||
"--ignored",
|
||||
`--exclude-from=${INCLUDE_FILE}`,
|
||||
"-z",
|
||||
]),
|
||||
);
|
||||
const matches = await findIncludedFiles(source);
|
||||
if (matches.length === 0) return { kind: "copied", copied: [], skipped: [] };
|
||||
|
||||
const ignored = splitNul(
|
||||
await gitAllowExit1(source, ["check-ignore", "--stdin", "-z"], matches.join("\0") + "\0"),
|
||||
);
|
||||
const ignored = await checkIgnore(source, [], matches);
|
||||
|
||||
const copied: string[] = [];
|
||||
const skipped: string[] = [];
|
||||
@@ -69,3 +66,152 @@ export async function copyWorktreeIncludes(worktree: string, source: string): Pr
|
||||
}
|
||||
return { kind: "copied", copied, skipped };
|
||||
}
|
||||
|
||||
// Git lists a directory it ignores as a whole, such as node_modules/, as one entry.
|
||||
// Only directories that a pattern matches or names a path inside are searched, and
|
||||
// only as deep as the pattern names, so creating a worktree does not walk every file
|
||||
// in large ignored trees.
|
||||
async function findIncludedFiles(source: string): Promise<string[]> {
|
||||
const ignored = splitNul(
|
||||
await git(source, [
|
||||
"ls-files",
|
||||
"--others",
|
||||
"--ignored",
|
||||
"--exclude-standard",
|
||||
"--directory",
|
||||
"-z",
|
||||
]),
|
||||
);
|
||||
if (ignored.length === 0) return [];
|
||||
|
||||
const ignoreCase = await sourceIgnoresCase(source);
|
||||
const patterns = parsePatterns(await readFile(join(source, INCLUDE_FILE), "utf8"));
|
||||
const matched = new Set(await matchIncludePatterns(source, ignored, ignoreCase));
|
||||
const files = ignored.filter((entry) => !isDirectory(entry) && matched.has(entry));
|
||||
const searched = new Set<string>();
|
||||
// Git also lists an untracked folder that holds ignored entries, which are listed on
|
||||
// their own, so only folders Git ignores as a whole are searched.
|
||||
const directories = await checkIgnore(source, [], ignored.filter(isDirectory));
|
||||
for (const directory of directories) {
|
||||
if (matched.has(directory)) {
|
||||
searched.add(directory);
|
||||
continue;
|
||||
}
|
||||
for (const pattern of patterns) {
|
||||
const inside = searchedPathInside(pattern, directory, ignoreCase);
|
||||
if (inside !== null) searched.add(inside);
|
||||
}
|
||||
}
|
||||
if (searched.size === 0) return files;
|
||||
|
||||
const pathspecs = [...searched].map(
|
||||
(path) => `:(${ignoreCase ? "literal,icase" : "literal"})${path}`,
|
||||
);
|
||||
const inside: string[] = [];
|
||||
for (const batch of commandLineBatches(pathspecs)) {
|
||||
const listed = await git(source, [
|
||||
"ls-files",
|
||||
"--others",
|
||||
"--ignored",
|
||||
`--exclude-from=${INCLUDE_FILE}`,
|
||||
"-z",
|
||||
"--",
|
||||
...batch,
|
||||
]);
|
||||
inside.push(...splitNul(listed));
|
||||
}
|
||||
return [...new Set([...files, ...inside])];
|
||||
}
|
||||
|
||||
// Windows caps a command line at 32,767 characters, so a pattern such as `**/.env` in a
|
||||
// repo with many ignored folders searches them over several Git calls.
|
||||
const COMMAND_LINE_BATCH_CHARS = 16_000;
|
||||
|
||||
function commandLineBatches(args: string[]): string[][] {
|
||||
const batches: string[][] = [];
|
||||
let batch: string[] = [];
|
||||
let length = 0;
|
||||
for (const arg of args) {
|
||||
if (batch.length > 0 && length + arg.length + 1 > COMMAND_LINE_BATCH_CHARS) {
|
||||
batches.push(batch);
|
||||
batch = [];
|
||||
length = 0;
|
||||
}
|
||||
batch.push(arg);
|
||||
length += arg.length + 1;
|
||||
}
|
||||
if (batch.length > 0) batches.push(batch);
|
||||
return batches;
|
||||
}
|
||||
|
||||
// Match entries against .worktreeinclude alone, in a scratch repository where no other
|
||||
// ignore rules apply. Directory entries keep their trailing slash, so directory
|
||||
// patterns match them without anything on disk.
|
||||
async function matchIncludePatterns(
|
||||
source: string,
|
||||
entries: string[],
|
||||
ignoreCase: boolean,
|
||||
): Promise<string[]> {
|
||||
const scratch = await mkdtemp(join(tmpdir(), "paseo-worktree-include-"));
|
||||
try {
|
||||
// An empty template keeps the user's template `info/exclude` out of the matching.
|
||||
await git(scratch, ["init", "-q", "--template="]);
|
||||
return await checkIgnore(
|
||||
scratch,
|
||||
[
|
||||
"-c",
|
||||
`core.excludesFile=${join(source, INCLUDE_FILE)}`,
|
||||
"-c",
|
||||
`core.ignoreCase=${ignoreCase}`,
|
||||
],
|
||||
entries,
|
||||
);
|
||||
} finally {
|
||||
await rm(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The scratch repository would otherwise take core.ignoreCase from the temp folder.
|
||||
async function sourceIgnoresCase(source: string): Promise<boolean> {
|
||||
try {
|
||||
return (await git(source, ["config", "--type=bool", "core.ignoreCase"])).trim() === "true";
|
||||
} catch {
|
||||
// `git config` exits 1 when the key is unset.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function parsePatterns(contents: string): string[] {
|
||||
// Git skips a leading UTF-8 byte order mark in exclude files.
|
||||
return contents
|
||||
.replace(/^\uFEFF/, "")
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trimEnd())
|
||||
.filter((line) => line !== "" && !line.startsWith("#") && !line.startsWith("!"));
|
||||
}
|
||||
|
||||
// A pattern with a slash before its end is anchored to the checkout root, so it can
|
||||
// name a path inside `directory` when its literal start and the directory overlap.
|
||||
// Without `**`, a pattern matches paths of exactly its own depth, so a directory that
|
||||
// deep or deeper can hold no match below it. Returns the narrowest path to search: the
|
||||
// whole path when the pattern has no glob, else the folder before its first glob, or
|
||||
// `directory` itself when the path ends above it.
|
||||
function searchedPathInside(
|
||||
pattern: string,
|
||||
rawDirectory: string,
|
||||
ignoreCase: boolean,
|
||||
): string | null {
|
||||
// Git folds only ASCII letters when it ignores case.
|
||||
const fold = (text: string) =>
|
||||
ignoreCase ? text.replace(/[A-Z]+/g, (letters) => letters.toLowerCase()) : text;
|
||||
const path = fold(pattern.replace(/^\//, ""));
|
||||
const directory = fold(rawDirectory);
|
||||
const depth = (text: string) => text.replace(/\/$/, "").split("/").length;
|
||||
if (depth(path) < 2) return null;
|
||||
if (!path.includes("**") && depth(directory) >= depth(path)) return null;
|
||||
const globAt = path.search(/[*?[\\]/);
|
||||
const literal = globAt === -1 ? path : path.slice(0, globAt);
|
||||
if (!literal.startsWith(directory) && !directory.startsWith(literal)) return null;
|
||||
const searched = globAt === -1 ? literal : literal.slice(0, literal.lastIndexOf("/") + 1);
|
||||
return searched.length > directory.length ? searched : directory;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ config/*.local.json
|
||||
|
||||
A pattern only selects files that Git already ignores. Adding a path here does not ignore it, so also configure `.gitignore` when needed. Tracked files and untracked files that Git does not ignore are excluded.
|
||||
|
||||
Paseo does not search inside a directory that Git ignores as a whole, such as `node_modules/`, unless a pattern names that directory or a path inside it. A bare name like `.env` matches `.env` and `apps/web/.env`, but not `node_modules/pkg/.env`. To copy from inside an ignored directory, name it in the pattern, as in `node_modules/pkg/.env` or `.secrets/`. A pattern with a glob in its first directory name, such as `**/.env` or `*/.env`, searches every ignored directory, including `node_modules/`, so name the directory instead when it is large.
|
||||
|
||||
## When files are copied
|
||||
|
||||
The built-in `worktree-include` plugin reads the file from the checkout you create the worktree from. That source can itself be another worktree. Keep the plugin enabled for copying to run.
|
||||
|
||||
@@ -36,6 +36,8 @@ config/*.local.json
|
||||
|
||||
模式只会选择 Git 已忽略的文件。在此添加路径不会使 Git 忽略该文件,需要时还应配置 `.gitignore`。已跟踪的文件和未被 Git 忽略的未跟踪文件都不会被复制。
|
||||
|
||||
对于被 Git 整体忽略的目录(例如 `node_modules/`),除非某个模式指明了该目录或其中的路径,否则 Paseo 不会在其中查找。像 `.env` 这样的纯文件名会匹配 `.env` 和 `apps/web/.env`,但不会匹配 `node_modules/pkg/.env`。要从被忽略的目录中复制文件,请在模式中写出该目录,例如 `node_modules/pkg/.env` 或 `.secrets/`。第一级目录名中带有通配符的模式(例如 `**/.env` 或 `*/.env`)会在所有被忽略的目录中查找,包括 `node_modules/`,因此目录较大时请直接写出目录。
|
||||
|
||||
## 文件复制时机 {#when-files-are-copied}
|
||||
|
||||
内置的 `worktree-include` 插件从用于创建 worktree 的检出读取该文件。源检出也可以是另一个 worktree。保持该插件启用才能执行复制。
|
||||
|
||||
Reference in New Issue
Block a user