Skip to content

Commit 6ba3de7

Browse files
fix(server): remote delegate_task runs on the driver it names (pingdotgg#34790)
1 parent ed54f30 commit 6ba3de7

2 files changed

Lines changed: 113 additions & 4 deletions

File tree

‎apps/server/src/peer/RemoteDelegation.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type Project,
1616
type RepositoryIdentity,
1717
RunId,
18+
type ServerProvider,
1819
ThreadId,
1920
TurnItemId,
2021
} from "@t3tools/contracts";
@@ -561,9 +562,15 @@ const boxToolkitLayer = (thread: BoxThread) => {
561562
),
562563
),
563564
Layer.provide(
564-
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
565+
Layer.mock(ProviderRegistry.ProviderRegistry)({
566+
getProviders: Effect.succeed([boxClaude]),
567+
}),
568+
),
569+
Layer.provide(
570+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
571+
list: () => Effect.succeed([boxClaude.instanceId]),
572+
}),
565573
),
566-
Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})),
567574
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
568575
Layer.provide(
569576
Layer.mock(ProjectService.ProjectService)({
@@ -597,6 +604,23 @@ const mcpClient = McpSchema.McpServerClient.of({
597604
getClient: Effect.die("unused"),
598605
});
599606

607+
/** The box's only provider: a Claude instance, under its own id. */
608+
const boxClaude: ServerProvider = {
609+
instanceId: ProviderInstanceId.make("claude-box"),
610+
driver: ProviderDriverKind.make("claudeAgent"),
611+
enabled: true,
612+
installed: true,
613+
version: "test",
614+
status: "ready",
615+
auth: { status: "authenticated" },
616+
checkedAt: "2026-09-13T00:00:00.000Z",
617+
models: [
618+
{ slug: "claude-sonnet-4-6", name: "Claude Sonnet", isCustom: false, capabilities: null },
619+
],
620+
slashCommands: [],
621+
skills: [],
622+
};
623+
600624
const adapter = {
601625
instanceId,
602626
driver,
@@ -1193,6 +1217,35 @@ it.effect("a task named only by environment inherits the parent's model, within
11931217
).pipe(Effect.provide(NodeHttpServer.layerTest)),
11941218
);
11951219

1220+
it.effect("a task naming another driver there never runs its model on the parent's instance", () =>
1221+
Effect.scoped(
1222+
Effect.gen(function* () {
1223+
const thread = yield* makeBoxThread;
1224+
const b = yield* serveBox(thread);
1225+
const offers = yield* SubscriptionRef.make<
1226+
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
1227+
>([]);
1228+
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
1229+
const a = yield* makeLaptop(database, offers);
1230+
yield* linkTo(a.links, b, "full-access");
1231+
yield* seedParent(a);
1232+
1233+
const result = yield* a.call("delegate_task", {
1234+
task: "Run the suite on the box.",
1235+
target: {
1236+
environmentId: box.environmentId,
1237+
driverKind: "claudeAgent",
1238+
model: "claude-sonnet-4-6",
1239+
},
1240+
});
1241+
expect(result.isError).toBe(false);
1242+
expect((yield* Ref.get(thread.launchedWith))?.modelSelection.instanceId).toBe(
1243+
boxClaude.instanceId,
1244+
);
1245+
}),
1246+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
1247+
);
1248+
11961249
it.effect("refuses worktree isolation for a task there before launching anything", () =>
11971250
Effect.scoped(
11981251
Effect.gen(function* () {

‎apps/server/src/peer/RemoteDelegation.ts‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
CommandId,
33
type EnvironmentId,
44
NodeId,
5+
OrchestratorMcpCapabilitiesInput,
6+
OrchestratorMcpCapabilitiesResult,
57
OrchestratorMcpFailure,
68
type OrchestratorMcpDelegateTaskInput,
79
type OrchestratorMcpTaskCancelResult,
@@ -27,7 +29,7 @@ import * as Option from "effect/Option";
2729
import * as Schedule from "effect/Schedule";
2830
import * as Scope from "effect/Scope";
2931
import * as Stream from "effect/Stream";
30-
import type * as Tool from "effect/ai/Tool";
32+
import * as Tool from "effect/ai/Tool";
3133
import { withExecutedModelNote } from "@t3tools/provider-core/server/subagentProjection";
3234

3335
import type * as McpInvocationContext from "../mcp/McpInvocationContext.ts";
@@ -66,6 +68,14 @@ export const FollowRetrySchedule = Context.Reference<Schedule.Schedule<unknown>>
6668

6769
type ThreadScope = McpInvocationContext.McpThreadInvocationScope;
6870

71+
/** `orchestrator_capabilities` there. Its toolkit imports this module, so it is described here. */
72+
const RemoteCapabilitiesTool = Tool.make("orchestrator_capabilities", {
73+
parameters: OrchestratorMcpCapabilitiesInput,
74+
success: OrchestratorMcpCapabilitiesResult,
75+
failure: OrchestratorMcpFailure,
76+
failureMode: "return",
77+
});
78+
6979
/**
7080
* `delegate_task` to a linked environment. The task's child is an ordinary
7181
* thread there, launched through the link; the parent here records the task
@@ -202,6 +212,45 @@ const make = Effect.gen(function* () {
202212
});
203213
});
204214

215+
/** The instance there of the driver the caller named, preferring the one this thread runs on, as a task here does. */
216+
const resolveRemoteInstance = Effect.fn("RemoteDelegation.resolveRemoteInstance")(function* (
217+
scope: ThreadScope,
218+
environmentId: EnvironmentId,
219+
requested: {
220+
readonly driverKind: ProviderDriverKind;
221+
readonly providerInstanceId: ProviderInstanceId | undefined;
222+
readonly inheritedInstanceId: ProviderInstanceId;
223+
},
224+
) {
225+
const { providers } = yield* forwarding.call(scope, RemoteCapabilitiesTool, environmentId, {});
226+
if (requested.providerInstanceId !== undefined) {
227+
const named = providers.find(
228+
(provider) => provider.providerInstanceId === requested.providerInstanceId,
229+
);
230+
if (named !== undefined && named.driverKind !== requested.driverKind) {
231+
return yield* new OrchestratorMcpFailure({
232+
code: "invalid_request",
233+
message: `Provider instance ${named.providerInstanceId} there uses driver ${named.driverKind}, not ${requested.driverKind}.`,
234+
});
235+
}
236+
return requested.providerInstanceId;
237+
}
238+
const candidates = providers.filter(
239+
(provider) => provider.driverKind === requested.driverKind && provider.canRunChildTask,
240+
);
241+
const chosen =
242+
candidates.find(
243+
(provider) => provider.providerInstanceId === requested.inheritedInstanceId,
244+
) ?? candidates[0];
245+
if (chosen === undefined) {
246+
return yield* new OrchestratorMcpFailure({
247+
code: "provider_unavailable",
248+
message: `No instance of driver ${requested.driverKind} there can run a child task. orchestrator_capabilities with this environmentId lists its providers.`,
249+
});
250+
}
251+
return chosen.providerInstanceId;
252+
});
253+
205254
const delegate: RemoteDelegation["Service"]["delegate"] = Effect.fn("RemoteDelegation.delegate")(
206255
function* (scope, input) {
207256
const parent = yield* threads.getThreadRecords(scope.thread.threadId, ["runs"]).pipe(
@@ -240,7 +289,14 @@ const make = Effect.gen(function* () {
240289
// The provider and model inherit as they do for a task here. Another
241290
// provider there has no model to inherit, so the caller names one.
242291
const inherited = parent.thread.modelSelection;
243-
const instanceId = target.providerInstanceId ?? inherited.instanceId;
292+
const instanceId =
293+
target.driverKind === undefined
294+
? (target.providerInstanceId ?? inherited.instanceId)
295+
: yield* resolveRemoteInstance(scope, target.environmentId, {
296+
driverKind: target.driverKind,
297+
providerInstanceId: target.providerInstanceId,
298+
inheritedInstanceId: inherited.instanceId,
299+
});
244300
const model =
245301
target.model ?? (instanceId === inherited.instanceId ? inherited.model : undefined);
246302
if (model === undefined) {

0 commit comments

Comments
 (0)