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
17 changes: 12 additions & 5 deletions apps/mobile/src/features/settings/ThreadSettingsSections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export function SettingsLabsRouteScreen() {
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="Automatic placement"
icon="server.rack"
capability="peerLinks"
setting="automaticPlacementByDefault"
description="Place an agent's new worktree from origin on the linked machine with the most room unless it names one. Headless machines fill first; someone's computer only when it is idle and every headless one is full. Set which machines are someone's computer in each machine's Labs settings."
/>
<MachineBetaSection
title="Fast worktree checkout"
icon="bolt.circle"
Expand Down Expand Up @@ -238,7 +245,10 @@ export function NewThreadSettingsSection() {
);
}

type MachineBetaSetting = "linkedEnvironmentsEnabled" | "fastWorktreeCheckout";
type MachineBetaSetting =
| "linkedEnvironmentsEnabled"
| "automaticPlacementByDefault"
| "fastWorktreeCheckout";

/** One switch per connected machine that offers the beta, since each machine's own setting decides. */
function MachineBetaSection({
Expand Down Expand Up @@ -284,10 +294,7 @@ function MachineBetaSection({
void updateSettings({
environmentId: environment.environmentId,
input: {
patch:
setting === "linkedEnvironmentsEnabled"
? { linkedEnvironmentsEnabled: value }
: { fastWorktreeCheckout: value },
patch: { [setting]: value },
},
})
}
Expand Down
38 changes: 37 additions & 1 deletion apps/server/src/mcp/toolkits/project/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CommandId,
type EnvironmentId,
MessageId,
type ModelSelection,
ThreadId,
OrchestratorMcpFailure,
ProjectId,
Expand Down Expand Up @@ -128,8 +129,33 @@ const launchPlacedThere = Effect.fn("mcp.launchPlacedThere")(function* (
/** The mode it would start with here, project defaults included. */
readonly runtimeMode: RuntimeMode;
readonly interactionMode: ProviderInteractionMode;
/** The calling thread's model, which a placed-by-default launch keeps. */
readonly callerModelSelection: ModelSelection | undefined;
},
) {
// A fresh worktree from origin needs nothing of this checkout, so the fleet places it
// by default; anything else, or an explicit 'here', stays unless it asks. Only with the
// caller's key: a retry after a lost answer must reach the machine first chosen.
const byDefault =
input.placement === undefined &&
input.clientRequestId !== undefined &&
input.environmentId === undefined &&
local.strategy?.type === "worktree" &&
local.strategy.startFromOrigin === true &&
(input.attachments?.length ?? 0) === 0 &&
(input.modelSelection ?? local.callerModelSelection) !== undefined &&
(yield* ServerSettings.ServerSettingsService.pipe(
Effect.flatMap((settings) => settings.getSettings),
Effect.map((settings) => settings.automaticPlacementByDefault),
Effect.orElseSucceed(() => false),
));
if (byDefault) {
input = {
...input,
placement: "auto",
modelSelection: input.modelSelection ?? local.callerModelSelection,
};
}
const key = input.clientRequestId;
if (key === undefined && input.placement !== "auto") return undefined;
// Work a link sent here was placed on its own side, and never uses this side's links.
Expand Down Expand Up @@ -201,7 +227,7 @@ const launchPlacedThere = Effect.fn("mcp.launchPlacedThere")(function* (
modelSelection,
runtimeMode: local.runtimeMode,
}).pipe(Effect.mapError(unavailable));
const environmentId = yield* delegation.value.place(scope, {
const placing = delegation.value.place(scope, {
operation: "thread-launch",
key,
threadId: local.threadId,
Expand All @@ -210,6 +236,15 @@ const launchPlacedThere = Effect.fn("mcp.launchPlacedThere")(function* (
runtimeMode: local.runtimeMode,
here,
});
// Placement nobody asked for falls back to here, as before, when no machine has room.
const environmentId = byDefault
? yield* placing.pipe(
Effect.catchIf(
(error) => error.code === "target_required",
() => Effect.succeed(scope.environmentId),
),
)
: yield* placing;
return environmentId === scope.environmentId ? undefined : yield* sendThere(environmentId, key);
});

Expand Down Expand Up @@ -330,6 +365,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
strategy,
runtimeMode: defaultRuntimeMode,
interactionMode,
callerModelSelection: caller?.modelSelection,
});
if (placedThere !== undefined) return placedThere;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/mcp/toolkits/project/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", {
attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))),
clientRequestId: Schema.optional(OrchestratorMcpClientRequestId),
placement: Schema.optional(
Schema.Literals(["auto"]).annotate({
Schema.Literals(["auto", "here"]).annotate({
description:
"'auto', without `environmentId`: here or on a linked environment with spare capacity, this repository's project and your exact `modelSelection`; there, in that project's checkout. Needs `modelSelection` and `clientRequestId`; a retry keeps the environment chosen. Attachments, scratch and worktrees not started from origin stay here.",
"'auto', without `environmentId`: here or on a linked environment with spare capacity, this repository's project and your exact `modelSelection`; there, in that project's checkout. Needs `modelSelection` and `clientRequestId`; a retry keeps the environment chosen. Attachments, scratch and worktrees not started from origin stay here. Omitted with a `clientRequestId`: a worktree started from origin is placed the same way, with your model, when this machine places by default. 'here' keeps it here.",
}),
),
/** Set by a linked environment launching its agent's delegated task here; ignored otherwise. */
Expand Down
86 changes: 86 additions & 0 deletions apps/server/src/peer/RemoteDelegation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const loadOf = (cpuUtilization: number): HostResourcesSnapshot => ({
cpuCount: 8,
availableMemoryBytes: 8 * GiB,
totalMemoryBytes: 16 * GiB,
placementRole: "headless",
});

const makeBoxThread = Effect.gen(function* () {
Expand Down Expand Up @@ -517,6 +518,7 @@ const boxToolkitLayer = (thread: BoxThread) => {
),
});
const layerBoxLaunches = Layer.mock(ThreadLaunch.ThreadLaunchService)({
checkWorktreeBase: () => Effect.void,
launch: (input) =>
Ref.get(thread.runless).pipe(
Effect.flatMap((runless) =>
Expand Down Expand Up @@ -892,6 +894,23 @@ const launchPlaced = (
outsideScope,
);

/** A launch that says nothing about placement, so this machine's default decides. */
const launchUnplaced = (
laptopEnv: Effect.Success<ReturnType<typeof makeLaptop>>,
args: Record<string, unknown>,
) =>
laptopEnv.call(
"t3_thread_launch",
{
projectId: "project:laptop",
title: "Suite",
message: "Run the suite wherever there is room.",
modelSelection: { instanceId: boxClaude.instanceId, model: "claude-sonnet-4-6" },
...args,
},
outsideScope,
);

/** The laptop's parent thread, mid-turn when its agent delegates. */
const seedParent = (
laptopEnv: Effect.Success<ReturnType<typeof makeLaptop>>,
Expand Down Expand Up @@ -1624,6 +1643,73 @@ it.effect("an outside client's auto-placed launch starts at the project's defaul
).pipe(Effect.provide(NodeHttpServer.layerTest)),
);

it.effect("a fresh worktree from origin is placed without asking, unless kept here", () =>
Effect.scoped(
Effect.gen(function* () {
const thread = yield* makeBoxThread;
const b = yield* serveBox(thread);
const offers = yield* SubscriptionRef.make<
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
>([]);
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
const localLaunches = yield* Ref.make(0);
const a = yield* makeLaptop(database, offers, undefined, Effect.void, {
localLaunch: Ref.update(localLaunches, (count) => count + 1),
});
yield* linkTo(a.links, b, "full-access");
const fresh = { type: "worktree", baseRef: "main", startFromOrigin: true };
// Without a key nothing could find the machine again after a lost answer: it stays here.
yield* launchUnplaced(a, { workspaceStrategy: fresh });
expect(yield* Ref.get(localLaunches)).toBe(1);
const placed = yield* launchUnplaced(a, {
clientRequestId: "fresh",
workspaceStrategy: fresh,
});
expect(placed.isError, JSON.stringify(placed.content)).toBe(false);
expect(placed.structuredContent).toMatchObject({ environmentId: box.environmentId });
expect(yield* Ref.get(localLaunches)).toBe(1);

// 'here', or the fleet default turned off, keeps it on this machine.
yield* launchPlaced(a, {
placement: "here",
clientRequestId: "kept",
workspaceStrategy: fresh,
});
expect(yield* Ref.get(localLaunches)).toBe(2);
yield* a.settings.updateSettings({ automaticPlacementByDefault: false });
yield* launchUnplaced(a, { clientRequestId: "off", workspaceStrategy: fresh });
expect(yield* Ref.get(localLaunches)).toBe(3);
}),
).pipe(Effect.provide(NodeHttpServer.layerTest)),
);

it.effect("placement nobody asked for runs here when no machine has room", () =>
Effect.scoped(
Effect.gen(function* () {
const thread = yield* makeBoxThread;
yield* Ref.set(thread.resources, { ...loadOf(0.99) });
const b = yield* serveBox(thread);
const offers = yield* SubscriptionRef.make<
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
>([]);
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
const localLaunches = yield* Ref.make(0);
const a = yield* makeLaptop(database, offers, undefined, Effect.void, {
localLaunch: Ref.update(localLaunches, (count) => count + 1),
});
yield* linkTo(a.links, b, "full-access");
const fresh = { type: "worktree", baseRef: "main", startFromOrigin: true };
yield* launchUnplaced(a, { clientRequestId: "room", workspaceStrategy: fresh });
expect(yield* Ref.get(localLaunches)).toBe(1);
expect(yield* Ref.get(thread.launched)).toBeNull();
// Asked for, it still fails loudly instead.
const asked = yield* launchPlaced(a, { clientRequestId: "full", workspaceStrategy: fresh });
expect(asked.isError).toBe(true);
expect(yield* Ref.get(localLaunches)).toBe(1);
}),
).pipe(Effect.provide(NodeHttpServer.layerTest)),
);

it.effect(
"a retry that drops placement or asks for this checkout reaches the thread placed there",
() =>
Expand Down
52 changes: 51 additions & 1 deletion apps/server/src/process/agentScope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
import { vi } from "vite-plus/test";
import { afterEach, vi } from "vite-plus/test";

import * as ServerConfig from "../config.ts";
import { makeCodexAppServerSpawnCommand } from "../orchestration-v2/Adapters/CodexAdapterV2.ts";
import * as ProcessRunner from "../processRunner.ts";
import * as ProviderProcesses from "./providerProcesses.ts";
import { MachinePresence } from "../resourceTelemetry/HostResources.ts";
import { classifyScope, make, ownedAgentSlice, type ScopeState } from "./agentScope.ts";

const GiB = 1024 ** 3;
Expand Down Expand Up @@ -67,11 +68,13 @@ const withFakeSystemd = <A, E>(
stopFails = false,
leftovers = false,
liveOwner = false,
role,
}: {
readonly oomPolicy?: boolean;
readonly stopFails?: boolean;
readonly leftovers?: boolean;
readonly liveOwner?: boolean;
readonly role?: { current: "headless" | "interactive" };
} = {},
) =>
Effect.gen(function* () {
Expand Down Expand Up @@ -125,7 +128,17 @@ const withFakeSystemd = <A, E>(
if (leftovers) {
units.set(ownedAgentSlice(stateDir), { activeState: "active", result: "success" });
}
const presence =
role === undefined
? undefined
: MachinePresence.of({
role: Effect.sync(() => role.current),
read: Effect.sync(() => ({ placementRole: role.current })),
});
const scope = yield* make.pipe(
presence === undefined
? (effect) => effect
: Effect.provideService(MachinePresence, presence),
Effect.provideService(ProviderProcesses.ProviderProcesses, { hasOtherLiveOwner: liveOwner }),
Effect.provideService(ProcessRunner.ProcessRunner, runner),
Effect.provideService(HostProcess.Platform, "linux"),
Expand Down Expand Up @@ -195,6 +208,9 @@ const launchCodex = (scope: Effect.Success<typeof make>, threadId: string) =>
);

describe("AgentScope service", () => {
afterEach(() => {
memory.total = 16 * GiB;
});
it.effect("labels a thread from its newest scope until its next session clears it", () =>
withFakeSystemd(({ scope, units }) =>
Effect.gen(function* () {
Expand Down Expand Up @@ -325,12 +341,46 @@ describe("AgentScope service", () => {
"CPUWeight=10",
"IOWeight=10",
"TasksMax=25%",
"CPUQuota=",
],
]);
}),
),
);

it.effect("keeps a person's computer responsive and follows a role change", () => {
memory.total = 8 * GiB;
const role = { current: "interactive" as "headless" | "interactive" };
return withFakeSystemd(
({ scope, calls }) =>
Effect.gen(function* () {
const limits = () =>
calls.filter(([, verb]) => verb === "set-property").map((args) => args.slice(3));
yield* scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" });
const cores = NodeOS.cpus().length;
// 8 GB keeps 4 GB for the person before agents slow and 3 GB before a kill.
expect(limits()).toEqual([
["app-t3code-agents.slice", `MemoryHigh=${4 * GiB}`, `MemoryMax=${5 * GiB}`],
[
"app-t3code.slice",
"CPUWeight=1",
"IOWeight=1",
"TasksMax=25%",
`CPUQuota=${Math.max(100, Math.floor(cores * 50))}%`,
],
]);

role.current = "headless";
yield* scope.wrap({ command: "agent", args: [], name: "t", threadId: "b" });
expect(limits().slice(2)).toEqual([
["app-t3code-agents.slice", `MemoryHigh=${4 * GiB}`, `MemoryMax=${6 * GiB}`],
["app-t3code.slice", "CPUWeight=10", "IOWeight=10", "TasksMax=25%", "CPUQuota="],
]);
}),
{ role },
);
});

it.effect("follows a live memory change at the next launch", () => {
memory.total = 16 * GiB;
return withFakeSystemd(({ scope, calls, failingVerbs }) =>
Expand Down
Loading
Loading