Compare commits

..
Author SHA1 Message Date
JIHUN LEEandClaude Opus 5.5 576d0320c8 Keep Android text at the window's density on Samsung DeX (#9995)
React Native 0.86 converts between px and dp with the density of the
phone's built-in display, while Fabric lays out at the window's density.
In a DeX window (240 dpi on a 360 dpi phone) every text box came out at
two thirds of the height its text needed, so the text was clipped.

Backport the rule from react/react-native#59087: right after React
Native reads the display (when it creates the root view and on each
configuration change), restore the window's density and keep only the
display's size. Remove once the app runs React Native 0.89 or later
with useWindowDensityForDisplayMetricsAndroid on.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 21:08:05 +00:00
paseo-bot[bot]andClaude Opus 5.5 c8e3d67e29 Wait for each plugin content subscription id before checking its release (#10012)
The plugin-buttons journey reads the details' event subscription id to
check that closing, crashing and disabling the content releases it. The
id arrives with the daemon's subscription response, after the details
render. The first open already waited for it; the reopens read it as
soon as the cached summary showed, so the id could still be empty and
the release poll then waited for an id that never exists.

Every read now waits for a non-empty id.

Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:01:24 +00:00
95f2c7d16a Keep a Claude agent's process and background work when its effort level changes (#10008)
Changing the thinking level of a running Claude agent marked its query for a
restart. The next turn, mode change or model change then retired the Claude
process, which ended every background shell, workflow and subagent it ran with
"connection lost".

A move between two effort levels now goes to the running query through
applyFlagSettings({ effortLevel }), as Fast mode already does, and marks no
restart. Switching thinking off, to ultracode or back to the default still
restarts, because those change how the query is launched. If the running
process does not take the change, the old restart path runs, so the change is
never lost.


Row: app-built-from-one-set
Paseo: c6210e21-fc13-4dfa-adfc-43a050c7d75a

Co-authored-by: gilad-rubin <6134299+gilad-rubin@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:57:22 +00:00
18 changed files with 374 additions and 468 deletions
+8
View File
@@ -121,6 +121,14 @@ from `packages/app/android`:
These check terminal arrow ownership, held-key repeats and modifiers, and ordinary text-field editing.
Run the display density regression tests the same way:
```bash
./gradlew :paseo-display-metrics:connectedDebugAndroidTest --no-daemon --max-workers=2 -Dorg.gradle.parallel=false
```
These check that React Native sizes text at the window's density when the window is on another display, such as Samsung DeX.
### Diagnostic logs without ADB
In a dogfood build, open Android's Files app → Downloads → **Paseo diagnostics** and share all
+2
View File
@@ -7,6 +7,7 @@ const withAndroidProfileable = require("./plugins/with-android-profileable");
const withFdroidAutolinking = require("./plugins/with-fdroid-autolinking");
const withPasteInput = require("./plugins/with-paste-input");
const withAndroidScroll = require("./modules/paseo-scroll/app.plugin");
const withAndroidDisplayMetrics = require("./modules/paseo-display-metrics/app.plugin");
const { getNativeReleaseVersion } = require("./native-release-version");
const appVariant = process.env.APP_VARIANT ?? "production";
const isFdroidBuild = process.env.PASEO_FDROID_BUILD === "1";
@@ -146,6 +147,7 @@ export default {
"expo-router",
withPasteInput,
withAndroidScroll,
withAndroidDisplayMetrics,
[withAndroidAsyncStorageSize, 64],
...buildProfile.cameraPlugins,
withAndroidOptionalCamera,
@@ -198,6 +198,14 @@ async function expectDetails(page: Page) {
await expect(page.getByText("All systems operational", { exact: true })).toBeInViewport();
}
// The details render before the daemon answers their event subscription, and reopened
// details show the cached summary at once, so wait for the subscription id to arrive.
async function readContentObservation(page: Page): Promise<string> {
const observation = page.getByLabel("Content observation", { exact: true });
await expect(observation).not.toHaveText("");
return (await observation.textContent())!;
}
function workspaceMenuRow(page: Page, title: string): Locator {
return compactWorkspaceMenu(page).getByRole("menuitem", { name: title, exact: true });
}
@@ -354,14 +362,12 @@ export async function withButtonShowcase(
test.step("closing and crashing content release its owner while sibling buttons and app remain live", async () => {
await press(page, "Header status");
await expectDetails(page);
const observation = page.getByLabel("Content observation", { exact: true });
await expect(observation).not.toHaveText("");
const first = (await observation.textContent())!;
const first = await readContentObservation(page);
await press(page, "Close deployment details");
await expect.poll(() => released.has(first)).toBe(true);
await press(page, "Composer status");
await expectDetails(page);
const second = (await observation.textContent())!;
const second = await readContentObservation(page);
expect(second).not.toBe(first);
await workspace.client.renameProject(workspace.projectId, "Observer control");
await expect(page.getByText("Observed updates: 1", { exact: true })).toBeVisible();
@@ -373,7 +379,7 @@ export async function withButtonShowcase(
await page.keyboard.press("Escape");
await press(page, "Header status");
await expectDetails(page);
const recovered = (await observation.textContent())!;
const recovered = await readContentObservation(page);
expect([first, second]).not.toContain(recovered);
await showcase.setTitle("Healthy app after button failure");
await expect(page.getByTestId("workspace-header-title")).toHaveText(
@@ -687,9 +693,7 @@ export async function withButtonShowcase(
page,
page.getByRole("button", { name: "Close deployment details", exact: true }),
);
const observation = page.getByLabel("Content observation", { exact: true });
await expect(observation).not.toHaveText("");
const owner = (await observation.textContent())!;
const owner = await readContentObservation(page);
await showcase.disable();
await expect.poll(() => released.has(owner)).toBe(true);
await expect(
@@ -0,0 +1,2 @@
/android/.cxx/
/android/build/
@@ -0,0 +1,23 @@
apply plugin: 'com.android.library'
group = 'sh.paseo.displaymetrics'
version = '0.1.0'
def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
apply from: expoModulesCorePlugin
applyKotlinExpoModulesCorePlugin()
useCoreDependencies()
useDefaultAndroidSdkVersions()
android {
namespace "sh.paseo.displaymetrics"
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
implementation "com.facebook.react:react-android"
androidTestImplementation 'androidx.test:runner:1.6.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
}
@@ -0,0 +1,86 @@
package sh.paseo.displaymetrics
import android.util.DisplayMetrics
import android.util.TypedValue
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.uimanager.PixelUtil
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class PaseoDisplayMetricsTest {
private lateinit var display: DisplayMetrics
@Before
fun readDisplayLikeReactNative() {
DisplayMetricsHolder.initDisplayMetrics(InstrumentationRegistry.getInstrumentation().targetContext)
display = DisplayMetricsHolder.getScreenDisplayMetrics()
}
@After
fun forgetDisplay() {
DisplayMetricsHolder.setScreenDisplayMetrics(null)
}
@Test
fun reservesRoomForTextAtTheWindowDensity() {
// A DeX window at two thirds of the phone's density (240 dpi on a 360 dpi phone). React Native
// reads the font scale from the window but the density from the phone's display.
val window = windowAt(display.densityDpi * 2 / 3)
DisplayMetricsHolder.setScreenDisplayMetrics(
DisplayMetrics().apply {
setTo(display)
scaledDensity = window.scaledDensity
},
)
assertTrue(reservedTextPx(window) < drawnTextPx())
PaseoDisplayMetrics.syncWithWindow(window)
assertEquals(drawnTextPx(), reservedTextPx(window), 0.01f)
assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16f, window), drawnTextPx(), 0.01f)
val synced = DisplayMetricsHolder.getScreenDisplayMetrics()
assertEquals(window.densityDpi, synced.densityDpi)
assertEquals(display.widthPixels, synced.widthPixels)
assertEquals(display.heightPixels, synced.heightPixels)
}
@Test
fun leavesTheBuiltInDisplayAlone() {
PaseoDisplayMetrics.syncWithWindow(windowAt(display.densityDpi))
assertSame(display, DisplayMetricsHolder.getScreenDisplayMetrics())
}
@Test
fun waitsForReactNativeToReadTheDisplay() {
DisplayMetricsHolder.setScreenDisplayMetrics(null)
PaseoDisplayMetrics.syncWithWindow(windowAt(display.densityDpi / 2))
assertThrows(IllegalStateException::class.java) { DisplayMetricsHolder.getScreenDisplayMetrics() }
}
private fun windowAt(densityDpi: Int): DisplayMetrics =
DisplayMetrics().apply {
setTo(display)
this.densityDpi = densityDpi
density = densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT
scaledDensity = density * display.scaledDensity / display.density
}
/** A 16sp font as React Native draws it, in pixels. */
private fun drawnTextPx(): Float = PixelUtil.toPixelFromSP(16f)
/** The box React Native lays out for that font, once the window renders it in pixels. */
private fun reservedTextPx(window: DisplayMetrics): Float =
PixelUtil.toDIPFromPixel(drawnTextPx()) * window.density
}
@@ -0,0 +1,43 @@
package sh.paseo.displaymetrics
import android.app.Activity
import android.util.DisplayMetrics
import com.facebook.react.uimanager.DisplayMetricsHolder
/**
* React Native 0.86 converts between px and dp, and so sizes text, with the density of the
* device's built-in display, because `DisplayMetricsHolder` reads `Display.getRealMetrics()`.
* Fabric lays out at the window's density. On a display with another density, such as a Samsung
* DeX monitor, text boxes come out too small and clip their text.
*
* Backports react/react-native#59087 (`useWindowDensityForDisplayMetricsAndroid`): keep the
* window's density and take only the size from the display. Remove once the app runs React Native
* 0.89 or later with that flag on.
*/
object PaseoDisplayMetrics {
/** Call right after React Native reads the display, which it does again on each configuration change. */
@JvmStatic
fun syncWithWindow(activity: Activity) {
syncWithWindow(activity.resources.displayMetrics)
}
@JvmStatic
fun syncWithWindow(window: DisplayMetrics) {
val screen =
try {
DisplayMetricsHolder.getScreenDisplayMetrics()
} catch (_: IllegalStateException) {
// React Native hasn't read the display yet, so there is nothing to correct.
return
}
if (screen.densityDpi == window.densityDpi) return
DisplayMetricsHolder.setScreenDisplayMetrics(
DisplayMetrics().apply {
setTo(screen)
density = window.density
densityDpi = window.densityDpi
},
)
}
}
@@ -0,0 +1,35 @@
const { withMainActivity } = require("expo/config-plugins");
const ACTIVITY_CLASS = "class MainActivity : ReactActivity() {";
const SYNC = "sh.paseo.displaymetrics.PaseoDisplayMetrics.syncWithWindow(this)";
// React Native re-reads the built-in display's density when it creates the root view and on every
// configuration change, so the window's density has to be restored right after each `super` call.
const OVERRIDES = `
override fun onContentChanged() {
super.onContentChanged()
${SYNC}
}
override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
super.onConfigurationChanged(newConfig)
${SYNC}
}
`;
function configureDisplayMetricsActivity(contents) {
if (contents.includes(SYNC)) return contents;
if (
!contents.includes(ACTIVITY_CLASS) ||
/override fun on(ContentChanged|ConfigurationChanged)\(/.test(contents)
) {
throw new Error("Could not keep React Native text at the window's density in MainActivity");
}
return contents.replace(ACTIVITY_CLASS, `${ACTIVITY_CLASS}${OVERRIDES}`);
}
module.exports = (config) =>
withMainActivity(config, (mod) => {
mod.modResults.contents = configureDisplayMetricsActivity(mod.modResults.contents);
return mod;
});
module.exports.configureDisplayMetricsActivity = configureDisplayMetricsActivity;
@@ -0,0 +1,29 @@
import { describe, expect, it } from "vitest";
const { configureDisplayMetricsActivity } = require("./app.plugin");
const SYNC = "sh.paseo.displaymetrics.PaseoDisplayMetrics.syncWithWindow(this)";
describe("Android display metrics", () => {
it("restores the window's density after React Native reads the display", () => {
const source = "class MainActivity : ReactActivity() {\n}";
const result = configureDisplayMetricsActivity(source);
expect(result).toContain(`super.onContentChanged()\n ${SYNC}`);
expect(result).toContain(`super.onConfigurationChanged(newConfig)\n ${SYNC}`);
expect(configureDisplayMetricsActivity(result)).toBe(result);
});
it("fails prebuild when the template no longer has a place for the overrides", () => {
expect(() => configureDisplayMetricsActivity("changed template")).toThrow(
"Could not keep React Native text at the window's density",
);
});
it("fails prebuild rather than replace another plugin's configuration handler", () => {
const source =
"class MainActivity : ReactActivity() {\n override fun onConfigurationChanged(newConfig: Configuration) {}\n}";
expect(() => configureDisplayMetricsActivity(source)).toThrow(
"Could not keep React Native text at the window's density",
);
});
});
@@ -0,0 +1,6 @@
{
"platforms": ["android"],
"android": {
"modules": []
}
}
@@ -0,0 +1,5 @@
{
"name": "paseo-display-metrics",
"version": "0.1.0",
"private": true
}
@@ -123,7 +123,8 @@ test.each(["mode", "model", "commands"] as const)(
try {
await session.startTurn("Run the foreground command");
await running.promise;
expect(await session.setThinkingOption?.("medium")).toMatchObject({
// Ultracode is launched with the query, so the change waits for a restart.
expect(await session.setThinkingOption?.("ultracode")).toMatchObject({
message: "Thinking level applies next turn",
});
if (control === "mode") await session.setMode("acceptEdits");
@@ -149,7 +150,7 @@ test.each(["mode", "model", "commands"] as const)(
await session.startTurn("Run another command with the new thinking level");
queries[1].work.resolve();
expect(await terminal.promise).toBe("turn_completed");
expect(efforts).toEqual(["low", "medium"]);
expect(efforts).toEqual(["low", "xhigh"]);
} finally {
await session.close();
}
@@ -44,6 +44,7 @@ function createQueryMock(events: unknown[], options: QueryMockOptions = {}): Que
close: vi.fn(() => undefined),
setPermissionMode: vi.fn(async () => undefined),
setModel: vi.fn(async () => undefined),
applyFlagSettings: vi.fn(async () => undefined),
supportedModels: vi.fn(async () => [{ value: "opus", displayName: "Opus" }]),
supportedCommands: vi.fn(async () => []),
rewindFiles: vi.fn(async () => ({ canRewind: true })),
@@ -382,6 +383,84 @@ describe("Claude runtime exit", () => {
}
});
test("keeps the process and its background work when the effort level changes", async () => {
let capturedOptions: Options | undefined;
const child = createChildProcessStub();
const query = createQueryMock(BACKGROUND_SHELL_TURN_EVENTS, {
tail: new Promise<never>(() => undefined),
onReturn: () => child.emit("exit", 0, null),
});
const queryFactory = vi.fn(({ options }: ClaudeQueryInput) => {
capturedOptions = options;
return query;
});
vi.spyOn(spawnUtils, "spawnProcess").mockReturnValue(child);
const client = new ClaudeAgentClient({
logger: createTestLogger(),
queryFactory,
resolveBinary: async () => "/test/claude/bin",
});
const session = await client.createSession({
provider: "claude",
cwd: process.cwd(),
thinkingOptionId: "low",
});
try {
await session.run("start a background shell");
capturedOptions?.spawnClaudeCodeProcess?.(SPAWN_OPTIONS);
const events: AgentStreamEvent[] = [];
session.subscribe((event) => events.push(event));
await expect(session.setThinkingOption?.("high")).resolves.toBeUndefined();
// A pending restart would be carried out here.
await session.listCommands();
expect(query.applyFlagSettings).toHaveBeenCalledWith({ effortLevel: "high" });
expect(queryFactory).toHaveBeenCalledTimes(1);
expect(query.return).not.toHaveBeenCalled();
expect(child.killSignals).toEqual([]);
expect(events).not.toContainEqual(
expect.objectContaining({ type: "background_work", provider: "claude", items: null }),
);
} finally {
await session.close();
}
});
test("restarts with the new effort level when the running process does not take it", async () => {
const efforts: Options["effort"][] = [];
const queryFactory = vi.fn(({ options }: ClaudeQueryInput) => {
efforts.push(options.effort);
const query = createQueryMock(BACKGROUND_SHELL_TURN_EVENTS, {
tail: new Promise<never>(() => undefined),
});
vi.mocked(query.applyFlagSettings).mockRejectedValue(new Error("unsupported"));
return query;
});
vi.spyOn(spawnUtils, "spawnProcess").mockReturnValue(createChildProcessStub());
const client = new ClaudeAgentClient({
logger: createTestLogger(),
queryFactory,
resolveBinary: async () => "/test/claude/bin",
});
const session = await client.createSession({
provider: "claude",
cwd: process.cwd(),
thinkingOptionId: "low",
});
try {
await session.run("start a background shell");
await session.setThinkingOption?.("high");
await session.listCommands();
expect(efforts).toEqual(["low", "high"]);
} finally {
await session.close();
}
});
test("ignores background work a retired query reports after its restart", async () => {
let capturedOptions: Options | undefined;
let deliverLateEvent: ((event: unknown) => void) | undefined;
@@ -2859,6 +2859,7 @@ class ClaudeAgentSession implements AgentSession {
? thinkingOptionId
: null;
const previousThinkingOptionId = this.config.thinkingOptionId;
if (!normalizedThinkingOptionId || normalizedThinkingOptionId === "default") {
this.config.thinkingOptionId = undefined;
} else if (isClaudeThinkingOption(normalizedThinkingOptionId)) {
@@ -2867,12 +2868,35 @@ class ClaudeAgentSession implements AgentSession {
} else {
throw new Error(`Unknown thinking option: ${normalizedThinkingOptionId}`);
}
this.queryRestartNeeded = true;
if (!(await this.applyEffortToRunningQuery(previousThinkingOptionId))) {
this.queryRestartNeeded = true;
}
if (this.activeForegroundTurnId || this.autonomousTurn) {
return THINKING_APPLIES_NEXT_TURN_NOTICE;
}
}
// A move between two effort levels changes only the effort, which a running Claude process
// takes through flag settings, so the process and its background work stay. Switching thinking
// off, to ultracode or back to the default changes how the query is launched, so it still
// restarts the query. The recorded option feeds every later launch either way.
private async applyEffortToRunningQuery(
previousThinkingOptionId: string | undefined,
): Promise<boolean> {
const effort = this.config.thinkingOptionId;
if (!this.query || this.queryRestartNeeded) return false;
if (!isClaudeThinkingEffort(previousThinkingOptionId) || !isClaudeThinkingEffort(effort)) {
return false;
}
try {
await this.query.applyFlagSettings({ effortLevel: effort });
return true;
} catch (error) {
this.logger.warn({ err: error, effort }, "Claude did not take a live effort change");
return false;
}
}
async applyOptions(options: ProviderOptions): Promise<void> {
// Every option but Fast is read when the query is built, and replacing a started query would
// end its background work, so once it exists only Fast can change.
@@ -1,291 +0,0 @@
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"]);
});
});
+16 -162
View File
@@ -2,8 +2,7 @@
// Copyright (c) 2026 Carlos Precioso.
import { execFile } from "node:child_process";
import { constants, existsSync } from "node:fs";
import { copyFile, mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { copyFile, mkdir } from "node:fs/promises";
import { dirname, join } from "node:path";
import { promisify } from "node:util";
@@ -23,33 +22,37 @@ async function git(cwd: string, args: string[], input?: string): Promise<string>
return stdout;
}
// `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;
async function gitAllowExit1(cwd: string, args: string[], input: string): Promise<string> {
try {
output = await git(cwd, [...args, "check-ignore", "--stdin", "-z"], input);
return await git(cwd, args, 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 = await findIncludedFiles(source);
const matches = splitNul(
await git(source, [
"ls-files",
"--others",
"--ignored",
`--exclude-from=${INCLUDE_FILE}`,
"-z",
]),
);
if (matches.length === 0) return { kind: "copied", copied: [], skipped: [] };
const ignored = await checkIgnore(source, [], matches);
const ignored = splitNul(
await gitAllowExit1(source, ["check-ignore", "--stdin", "-z"], matches.join("\0") + "\0"),
);
const copied: string[] = [];
const skipped: string[] = [];
@@ -66,152 +69,3 @@ 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;
}
-2
View File
@@ -36,8 +36,6 @@ 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,8 +36,6 @@ 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。保持该插件启用才能执行复制。