Skip to content

Commit 482040b

Browse files
fix(server): an agent's t3_thread_launch on a linked environment stays top-level
An agent calling t3_thread_launch with another environment's id could pass its own delegatedFrom through. The linked environment trusts that field from the link session, so it stamped the thread as a delegated child, and clients hid it under a parent that never recorded it. The forwarder now drops delegatedFrom from an agent's call; only delegate_task's remote path, which records the parent's side, sets it. On the receiving side, an agent running in a thread a link started no longer keeps a delegatedFrom it claims: only the link session itself may. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8e33fdc commit 482040b

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎apps/server/src/mcp/McpToolAccess.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,9 @@ const routed = Effect.fn("McpToolAccess.routed")(function* <Tools extends Record
402402
if (scope.linkedEnvironments === false) return yield* PeerForwarding.linkedEnvironmentsOff;
403403
yield* (remote as RemoteCheck<typeof params>)(params);
404404
if (Option.isNone(forwarding)) return yield* linksUnavailable;
405-
return yield* forwarding.value.call(scope, tool, target, { ...params, environmentId });
405+
// The peer trusts `delegatedFrom` from this link, so only RemoteDelegation sets it, never a caller.
406+
const { delegatedFrom: _claimed, ...forwarded } = params as { delegatedFrom?: unknown };
407+
return yield* forwarding.value.call(scope, tool, target, { ...forwarded, environmentId });
406408
});
407409
}
408410
return handlers as Toolkit.HandlersFrom<Tools>;

‎apps/server/src/mcp/linkOrigin.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
CommandId,
77
ContextTransferId,
88
EnvironmentId,
9+
MessageId,
910
NodeId,
1011
type OrchestrationV2Run,
1112
ProjectId,
@@ -332,6 +333,33 @@ it.layer(Layer.provideMerge(layerTools, layerOrchestration))("work a link starts
332333
assert.deepEqual((yield* projections.getThreadShell(task))?.delegatedFrom, delegatedFrom);
333334
const claimed = yield* launch(claudeCode, "Claims a parent", { delegatedFrom });
334335
assert.equal((yield* projections.getThreadShell(claimed))?.delegatedFrom, undefined);
336+
// An agent in the link's thread acts for the link, but cannot claim one either.
337+
const working = yield* launch(laptop, "Working for the laptop");
338+
yield* ThreadManagement.ThreadManagementService.pipe(
339+
Effect.flatMap((threads) =>
340+
threads.dispatch({
341+
type: "message.dispatch",
342+
commandId: CommandId.make("start:working"),
343+
threadId: working,
344+
messageId: MessageId.make("message:working"),
345+
text: "Go.",
346+
attachments: [],
347+
dispatchMode: { type: "start_immediately" },
348+
createdBy: "user",
349+
creationSource: "web",
350+
}),
351+
),
352+
);
353+
const agent: McpInvocationContext.McpInvocationScope = {
354+
...laptop,
355+
requestNamespace: "thread:working",
356+
thread: { threadId: working, providerSessionId: "session", providerInstanceId: instanceId },
357+
client: undefined,
358+
};
359+
const agentClaimed = yield* launch(agent, "The agent claims a parent", { delegatedFrom });
360+
const agentShell = yield* projections.getThreadShell(agentClaimed);
361+
assert.equal(agentShell?.linkOrigin?.sessionId, "laptop");
362+
assert.equal(agentShell?.delegatedFrom, undefined);
335363
}),
336364
);
337365

‎apps/server/src/mcp/toolkits/project/handlers.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,11 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
230230
createdBy: "agent",
231231
creationSource: "mcp",
232232
...(linkOrigin === undefined ? {} : { linkOrigin }),
233-
// Kept only alongside linkOrigin: thread.create drops it otherwise.
234-
...(input.delegatedFrom === undefined ? {} : { delegatedFrom: input.delegatedFrom }),
233+
// Only the link's own session may claim a parent there, not an agent in a thread it started.
234+
// thread.create also drops it without linkOrigin.
235+
...(input.delegatedFrom === undefined || caller !== undefined
236+
? {}
237+
: { delegatedFrom: input.delegatedFrom }),
235238
}).pipe(
236239
Effect.mapError((error) => {
237240
if (error._tag === "AttachmentClaimError") {

0 commit comments

Comments
 (0)