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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions apps/mobile/src/features/settings/ThreadSettingsSections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AutoSettleDaysField } from "./components/AutoSettleDaysField";
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
import { useAtomSet, useAtomValue } from "@effect/atom-react";
import { AsyncResult } from "effect/reactivity";
import type { ComponentProps } from "react";
import { View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";

Expand Down Expand Up @@ -45,7 +46,20 @@ export function SettingsLabsRouteScreen() {
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
>
<ToolCallVisibilitySection />
<BetaSettingsSection />
<MachineBetaSection
title="Linked environments"
icon="link"
capability="peerLinks"
setting="linkedEnvironmentsEnabled"
description="Let agents on a machine work on your other machines, and theirs on it. Turn it on for each machine. While off, nothing new starts over a link; running tasks finish."
/>
<MachineBetaSection
title="Fast worktree checkout"
icon="bolt.circle"
capability="fastWorktreeCheckout"
setting="fastWorktreeCheckout"
description="On a Mac, clone new worktrees from a checkout cached in the T3 home folder instead of writing every file. Uses Git’s normal checkout when it can’t. Turning it off deletes the cache."
/>
<LegacySettingsSection />
</ScrollView>
</SettingsScreen>
Expand Down Expand Up @@ -228,48 +242,64 @@ export function NewThreadSettingsSection() {
);
}

/** Per machine, since each machine's own setting decides whether it links and answers links. */
function BetaSettingsSection() {
type MachineBetaSetting = "linkedEnvironmentsEnabled" | "fastWorktreeCheckout";

/** One switch per connected machine that offers the beta, since each machine's own setting decides. */
function MachineBetaSection({
title,
icon,
capability,
setting,
description,
}: {
readonly title: string;
readonly icon: ComponentProps<typeof SettingsSwitchRow>["icon"];
readonly capability: "peerLinks" | "fastWorktreeCheckout";
readonly setting: MachineBetaSetting;
readonly description: string;
}) {
const { environments } = useEnvironments();
const supported = environments.filter(
(environment) =>
environment.connection.phase === "connected" &&
environment.serverConfig?.environment.capabilities.peerLinks === true,
environment.serverConfig?.environment.capabilities[capability] === true,
);
const writable = useEnvironmentsWithScope(supported, AuthSettingsWriteScope);
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
label: "linked environments beta update",
label: `${setting} beta update`,
reportFailure: true,
});
if (supported.length === 0) return null;
return (
<View className="gap-3">
<SettingsSection title="Beta: Linked environments">
<SettingsSection title={`Beta: ${title}`}>
{supported.map((environment) => {
const policy = environment.serverConfig?.managedPolicy;
const managed = isManagedSettingPath(policy, "linkedEnvironmentsEnabled");
const managed = isManagedSettingPath(policy, setting);
return (
<SettingsSwitchRow
key={environment.environmentId}
icon="link"
icon={icon}
label={environment.label}
{...(managed ? { subtitle: `Managed by ${policy?.organization}.` } : {})}
value={environment.serverConfig?.settings.linkedEnvironmentsEnabled === true}
value={environment.serverConfig?.settings[setting] === true}
disabled={managed || !writable.has(environment.environmentId)}
onValueChange={(value) =>
void updateSettings({
environmentId: environment.environmentId,
input: { patch: { linkedEnvironmentsEnabled: value } },
input: {
patch:
setting === "linkedEnvironmentsEnabled"
? { linkedEnvironmentsEnabled: value }
: { fastWorktreeCheckout: value },
},
})
}
/>
);
})}
</SettingsSection>
<Text className="px-2 text-sm text-foreground-muted">
Let agents on a machine work on your other machines, and theirs on it. Turn it on for each
machine. While off, nothing new starts over a link; running tasks finish.
</Text>
<Text className="px-2 text-sm text-foreground-muted">{description}</Text>
</View>
);
}
Expand Down
2 changes: 2 additions & 0 deletions apps/mobile/src/features/settings/settings-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ const SETTINGS_SEARCH_TERMS = {
"beta",
"linked environments",
"link machine",
"fast worktree checkout",
"clone cache",
"tool calls in chat",
"hide tool calls",
"commands edits searches",
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface ServerDerivedPaths {
readonly environmentThemesDir: string;
readonly providerStatusCacheDir: string;
readonly worktreesDir: string;
readonly worktreeCheckoutCacheDir: string;
readonly attachmentsDir: string;
readonly browserArtifactsDir: string;
readonly logsDir: string;
Expand Down Expand Up @@ -158,6 +159,7 @@ export const deriveServerPaths = Effect.fn(function* (
environmentThemesDir: join(stateDir, "themes"),
providerStatusCacheDir,
worktreesDir: join(baseDir, "worktrees"),
worktreeCheckoutCacheDir: join(providerStatusCacheDir, "worktree-checkouts"),
attachmentsDir,
browserArtifactsDir: join(stateDir, "browser-artifacts"),
logsDir,
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/environment/ServerEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export const make = Effect.gen(function* () {
projectWorktreeCleanup: true,
settledWorktreeCleanup: true,
worktreesDirectory: true,
...(hostPlatform === "darwin" ? { fastWorktreeCheckout: true } : {}),
threadAutoSettlementScope: true,
threadRestartContinuation: true,
projectSettingsOverrides: true,
Expand Down
22 changes: 16 additions & 6 deletions apps/server/src/git/GitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,20 @@ export const make = Effect.gen(function* () {
}
return result;
});
const readWorktreesDirectory = serverSettingsService.getSettings.pipe(
Effect.map((settings) => settings.worktreesDirectory),
Effect.orElseSucceed(() => ""),
const readWorktreeCreationSettings = serverSettingsService.getSettings.pipe(
Effect.map((settings) => ({
worktreesDirectory: settings.worktreesDirectory,
fastCheckout: settings.fastWorktreeCheckout,
})),
Effect.orElseSucceed(() => ({ worktreesDirectory: "" })),
);
// Turning fast checkout off deletes its cache right away, not at the next worktree.
yield* serverSettingsService.streamChanges.pipe(
Stream.map((settings) => settings.fastWorktreeCheckout),
Stream.changes,
Stream.filter((enabled) => !enabled),
Stream.runForEach(() => gitCore.clearWorktreeCheckoutCache),
Effect.forkScoped,
);
const createWorktree: GitManager["Service"]["createWorktree"] = Effect.fn(
"GitManager.createWorktree",
Expand All @@ -764,9 +775,8 @@ export const make = Effect.gen(function* () {
Effect.map(({ settings }) => settings.worktreeSubmodules),
Effect.orElseSucceed(() => null),
);
const worktreesDirectory = yield* readWorktreesDirectory;
const result = yield* gitCore.createWorktree(input, {
worktreesDirectory,
...(yield* readWorktreeCreationSettings),
...options,
submodules,
});
Expand Down Expand Up @@ -3075,7 +3085,7 @@ export const make = Effect.gen(function* () {
path: null,
},
{
worktreesDirectory: yield* readWorktreesDirectory,
...(yield* readWorktreeCreationSettings),
submodules: yield* projectSettingsFor(input).pipe(
Effect.map(({ settings }) => settings.worktreeSubmodules),
Effect.orElseSucceed(() => null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function makeReplayServerConfig(
settingsPath: path.join(stateDir, "settings.json"),
providerStatusCacheDir,
worktreesDir,
worktreeCheckoutCacheDir: path.join(providerStatusCacheDir, "worktree-checkouts"),
attachmentsDir,
browserArtifactsDir: path.join(stateDir, "browser-artifacts"),
environmentThemesDir,
Expand Down
81 changes: 81 additions & 0 deletions apps/server/src/vcs/CheckoutCloner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as NodeModule from "node:module";

import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

export class CheckoutCloneError extends Schema.TaggedError<CheckoutCloneError>()(
"CheckoutCloneError",
{
source: Schema.String,
destination: Schema.String,
errno: Schema.optional(Schema.Number),
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return `Could not clone ${this.source} to ${this.destination}${this.errno === undefined ? "" : ` (errno ${this.errno})`}.`;
}
}

/**
* Copies a file or a whole folder copy-on-write in one call, sharing data blocks until either
* copy changes. The destination must not exist. Undefined where the platform has no such call.
*/
export const CheckoutCloner = Context.Reference<
((source: string, destination: string) => Effect.Effect<void, CheckoutCloneError>) | undefined
>("t3/vcs/CheckoutCloner", { defaultValue: () => undefined });

// A single-executable server resolves packages on disk only through `require`.
const requireForFfi = NodeModule.createRequire(import.meta.url);

/** macOS `clonefile(2)`, which clones a folder hierarchy in the kernel; elsewhere nothing. */
export const layer = Layer.effect(
CheckoutCloner,
Effect.gen(function* () {
if ((yield* HostProcess.Platform) !== "darwin") return undefined;
const loadClonefile = yield* Effect.cached(
Effect.try({
try: () => {
const { DataType, load, open } = requireForFfi("ffi-rs") as typeof import("ffi-rs");
open({ library: "t3-libsystem", path: "/usr/lib/libSystem.B.dylib" });
return (source: string, destination: string) =>
load({
library: "t3-libsystem",
funcName: "clonefile",
retType: DataType.I32,
paramsType: [DataType.String, DataType.String, DataType.I32],
// CLONE_NOFOLLOW: clone a symlink itself rather than its target.
paramsValue: [source, destination, 0x0001],
errno: true,
// A large folder takes hundreds of milliseconds; keep it off the event loop.
runInNewThread: true,
});
},
catch: (cause) =>
new CheckoutCloneError({ source: "ffi-rs", destination: "clonefile", cause }),
}),
);
return Effect.fn("CheckoutCloner.clonefile")(function* (source: string, destination: string) {
const clonefile = yield* loadClonefile.pipe(
Effect.mapError(
(error) => new CheckoutCloneError({ source, destination, cause: error.cause }),
),
);
const result = yield* Effect.tryPromise({
try: () => clonefile(source, destination),
catch: (cause) => new CheckoutCloneError({ source, destination, cause }),
});
// ffi-rs types the errno form as a union with the plain return value.
if (typeof result === "number" || result.value !== 0) {
return yield* new CheckoutCloneError({
source,
destination,
...(typeof result === "number" ? {} : { errno: result.errnoCode }),
});
}
});
}),
);
7 changes: 6 additions & 1 deletion apps/server/src/vcs/GitVcsDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
PATCH_RENDER_PREFIX_ARGS,
splitNullSeparatedGitStdoutPaths,
} from "./GitVcsDriverCore.ts";
import * as CheckoutCloner from "./CheckoutCloner.ts";
import * as VcsDriver from "./VcsDriver.ts";
import * as VcsProcess from "./VcsProcess.ts";

Expand Down Expand Up @@ -144,6 +145,8 @@ export interface CreateWorktreeOptions {
/** Null or omitted defers to the checkout's t3.json. */
readonly submodules?: WorktreeSubmodules | null;
readonly worktreesDirectory?: string;
/** Clone the checkout from the macOS worktree checkout cache. */
readonly fastCheckout?: boolean;
}

export interface GitCommitProgress {
Expand Down Expand Up @@ -373,6 +376,8 @@ export class GitVcsDriver extends Context.Service<
input: VcsCreateWorktreeInput,
options?: CreateWorktreeOptions,
) => Effect.Effect<VcsCreateWorktreeResult, GitCommandError>;
/** Deletes the macOS worktree checkout cache in the background. */
readonly clearWorktreeCheckoutCache: Effect.Effect<void>;
/** Force-updates local `branch` to the primary remote's `ref`. */
readonly fetchRefIntoBranch: (
input: GitFetchRefInput & { readonly branch: string },
Expand Down Expand Up @@ -1594,4 +1599,4 @@ export const make = Effect.gen(function* () {
});

export const layerVcs = Layer.effect(VcsDriver.VcsDriver, makeVcsDriver);
export const layer = Layer.effect(GitVcsDriver, make);
export const layer = Layer.effect(GitVcsDriver, make).pipe(Layer.provide(CheckoutCloner.layer));
35 changes: 31 additions & 4 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observabil
import * as GitVcsDriver from "./GitVcsDriver.ts";
import { removeEmptyDirectory, type RemoveEmptyDirectoryError } from "./removeEmptyDirectory.ts";
import { explicitBareRepositoryEnv, gitConfigEnv } from "./VcsProcess.ts";
import { makeWorktreeCheckoutCache } from "./worktreeCheckoutCache.ts";
import { removeLinkedWorktree } from "./worktreeRemoval.ts";
import { resolveWorktreesDirectory } from "../worktreesDirectory.ts";
import {
Expand Down Expand Up @@ -945,7 +946,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const { worktreesDir } = yield* ServerConfig.ServerConfig;
const { worktreesDir, worktreeCheckoutCacheDir } = yield* ServerConfig.ServerConfig;
const isServerInternalPath = yield* makeIsServerInternalPath;
const digest = yield* Digest.Digest;
const hostPlatform = yield* HostProcess.Platform;
Expand Down Expand Up @@ -1143,6 +1144,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
input.writesSharedConfig ? withSharedConfigLock(input.cwd, execution) : execution,
);

const checkoutCache = yield* makeWorktreeCheckoutCache({
cacheDir: worktreeCheckoutCacheDir,
execute,
});

const executeGit = (
operation: string,
cwd: string,
Expand Down Expand Up @@ -4259,7 +4265,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
"git worktree add failed",
);
}
const args = ["worktree", "add", "--", worktreePath, newRefName ?? input.refName];
const checkoutRef = newRefName ?? input.refName;
const progress = options?.progress;
const onCheckoutProgress = progress?.onCheckoutProgress;

Expand All @@ -4274,10 +4280,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
{ concurrency: "unbounded" },
);
const checkoutWorkers = configuredCheckoutWorkers ?? "0";
yield* executeGitWithStableDiagnostics(
const fastCheckout = checkoutCache !== undefined && options?.fastCheckout === true;
const cacheGeneration = fastCheckout ? yield* checkoutCache.currentGeneration : 0;
const stockCheckout = executeGitWithStableDiagnostics(
"GitVcsDriver.createWorktree",
input.cwd,
["-c", `checkout.workers=${checkoutWorkers}`, ...args],
[
"-c",
`checkout.workers=${checkoutWorkers}`,
"worktree",
"add",
"--",
worktreePath,
checkoutRef,
],
{
fallbackErrorDetail: "git worktree add failed",
// Large checkouts and post-checkout hooks can legitimately run for many
Expand All @@ -4297,6 +4313,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
}
: {}),
},
);
yield* (
fastCheckout
? checkoutCache
.create({ cwd: input.cwd, worktreePath, refName: checkoutRef, checkoutWorkers })
.pipe(Effect.flatMap((cloned) => (cloned ? Effect.void : stockCheckout)))
: stockCheckout
).pipe(
Effect.onError(() =>
newRefName
Expand All @@ -4308,6 +4331,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
: Effect.void,
),
);
if (fastCheckout) {
yield* checkoutCache.schedule(worktreePath, cacheGeneration);
}

if (progress?.onWorktreeClaimed) {
yield* progress.onWorktreeClaimed(worktreePath);
Expand Down Expand Up @@ -5097,6 +5123,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
Effect.ensuring(invalidateWorktreeCreationCaches(input.cwd)),
);
}),
clearWorktreeCheckoutCache: checkoutCache?.clear ?? Effect.void,
fetchRefIntoBranch: Effect.fnUntraced(function* (input) {
return yield* withListRefsInvalidation(input.cwd, fetchRefIntoBranch(input));
}),
Expand Down
Loading
Loading