Skip to content

Commit 7e9b347

Browse files
Silthusclaude
andcommitted
fix(server): remote delegated tasks surface their questions and approvals on the parent
A task delegated to a linked environment kept its questions and approvals there: the parent showed nothing, task_status reported none, and only a client attached to that environment could answer them. The follower now mirrors what the task's thread there waits on onto the parent's record through a new link-only t3_link_requests tool, the parent's shell and composer show it, task_status and delegate_task waits report it, and an answer given on the parent is relayed through the link, which holds it to the link's access. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9ebca5f commit 7e9b347

22 files changed

Lines changed: 1731 additions & 83 deletions

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ export const hiddenWhileLinksOff = (): boolean => {
127127
return scope?.linkedEnvironments !== false;
128128
};
129129

130+
/** Another T3 Code environment's peer link calling on its own behalf, not an agent here. */
131+
export const isLinkSession = (scope: McpInvocationScope | undefined): boolean =>
132+
scope?.thread === undefined && scope?.client?.linked === true;
133+
134+
/** `McpSchema.EnabledWhen` for tools only a peer link's own environment calls. */
135+
export const visibleToLinkSessions = (): boolean => {
136+
const fiber = Fiber.getCurrent();
137+
return isLinkSession(fiber && Context.getOrUndefined(fiber.context, McpInvocationContext));
138+
};
139+
130140
export type McpCapabilityError<C extends McpCapability> = C extends "preview"
131141
? PreviewAutomationUnavailableError
132142
: McpCapabilityUnavailableError;

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

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
NodeId,
99
ORCHESTRATOR_MCP_RUNNING_TASK_NEXT,
1010
TurnItemId,
11+
type OrchestrationV2RemoteTaskRequest,
1112
type OrchestrationV2Run,
1213
type OrchestrationV2Subagent,
1314
type OrchestrationV2ThreadProjection,
@@ -63,6 +64,7 @@ import {
6364
type ServerProviderModel,
6465
ThreadId,
6566
isOrchestrationV2WorkActive,
67+
unansweredRemoteTaskRequests,
6668
} from "@t3tools/contracts";
6769
import { getProviderOptionCurrentValue } from "@t3tools/shared/model";
6870
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
@@ -131,6 +133,10 @@ const TASK_WAKE_EVENTS = [
131133
{ thread: "child", eventType: "provider-thread.updated" },
132134
{ thread: "child", eventType: "runtime-request.updated" },
133135
] as const;
136+
const REMOTE_TASK_WAKE_EVENTS = [
137+
{ thread: "parent", eventType: "subagent.updated" },
138+
{ thread: "parent", eventType: "thread.metadata-updated" },
139+
] as const;
134140
const SECRET_REQUEST_POLL_INTERVAL_MS = 500;
135141
const DEFAULT_THREAD_LIST_LIMIT = 50;
136142
const DEFAULT_THREAD_READ_LIMIT = 50;
@@ -510,6 +516,23 @@ function pendingTaskApproval(
510516
return approval;
511517
}
512518

519+
/** The oldest approval a remote task's thread there waits on that the user has not answered here. */
520+
function remoteTaskApproval(
521+
task: OrchestrationV2Subagent,
522+
unanswered: ReadonlyArray<OrchestrationV2RemoteTaskRequest>,
523+
): OrchestratorMcpDelegateTaskResult["pendingApproval"] {
524+
for (const request of unanswered.toSorted(
525+
(left, right) =>
526+
DateTime.toEpochMillis(left.createdAt) - DateTime.toEpochMillis(right.createdAt),
527+
)) {
528+
const runId = request.runId ?? task.remoteChild?.runId;
529+
if (request.kind !== "user_input" && runId !== undefined) {
530+
return { requestId: request.requestId, kind: request.kind, runId };
531+
}
532+
}
533+
return null;
534+
}
535+
513536
function directAppOwnedChildTasks(
514537
parent: Pick<OrchestrationV2ThreadProjection, "thread" | "subagents">,
515538
target: Pick<OrchestrationV2ThreadProjection, "thread" | "runs">,
@@ -1411,7 +1434,15 @@ const make = Effect.gen(function* () {
14111434
);
14121435
}
14131436
if (task.childThreadId === null) {
1414-
return yield* readRemoteTask(scope, task, waitTimedOut, acknowledgeTerminal);
1437+
return yield* readRemoteTask(
1438+
scope,
1439+
task,
1440+
unansweredRemoteTaskRequests(parentProjection.thread).filter(
1441+
(request) => request.taskId === task.id,
1442+
),
1443+
waitTimedOut,
1444+
acknowledgeTerminal,
1445+
);
14151446
}
14161447
const childControls = yield* threadManagement
14171448
.getThreadRecords(
@@ -1607,8 +1638,8 @@ const make = Effect.gen(function* () {
16071638
Math.max(1, requestedTimeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS),
16081639
);
16091640
const waited = yield* waitForTask(scope, taskId, timeoutMs);
1610-
if (Option.isSome(waited)) return waited.value;
1611-
// As for a local task: once the blocking wait ends, a later result wakes the parent.
1641+
if (Option.isSome(waited) && isTerminalTaskStatus(waited.value.status)) return waited.value;
1642+
// As for a local task: once the blocking wait ends without a result, a later one wakes the parent.
16121643
yield* threadManagement
16131644
.dispatch({
16141645
type: "delegated_task.wake-policy",
@@ -1622,7 +1653,7 @@ const make = Effect.gen(function* () {
16221653
completionWake: "always",
16231654
})
16241655
.pipe(Effect.ignoreCause({ log: true }));
1625-
return yield* readTask(scope, taskId, true, true);
1656+
return yield* readTask(scope, taskId, Option.isNone(waited), true);
16261657
});
16271658

16281659
/**
@@ -1632,6 +1663,7 @@ const make = Effect.gen(function* () {
16321663
const readRemoteTask = Effect.fn("OrchestratorMcpService.readRemoteTask")(function* (
16331664
scope: McpThreadInvocationScope,
16341665
task: OrchestrationV2Subagent,
1666+
unanswered: ReadonlyArray<OrchestrationV2RemoteTaskRequest>,
16351667
waitTimedOut: boolean,
16361668
acknowledgeTerminal: boolean,
16371669
): Effect.fn.Return<OrchestratorMcpDelegateTaskResult, OrchestratorMcpFailure> {
@@ -1698,9 +1730,8 @@ const make = Effect.gen(function* () {
16981730
: null,
16991731
latestTerminalSummary: terminal ? task.result : null,
17001732
latestTerminalResultContextTransferId: null,
1701-
// Its questions and approvals are answered in the environment it runs in.
1702-
hasPendingQuestion: false,
1703-
pendingApproval: null,
1733+
hasPendingQuestion: !terminal && unanswered.some((request) => request.kind === "user_input"),
1734+
pendingApproval: terminal ? null : remoteTaskApproval(task, unanswered),
17041735
waitTimedOut,
17051736
} satisfies OrchestratorMcpDelegateTaskResult;
17061737
});
@@ -1721,12 +1752,10 @@ const make = Effect.gen(function* () {
17211752
return Option.some(initial);
17221753
}
17231754
// One stream per event type, so transcript events never fill a buffer.
1724-
// A remote task has no child here; its completion lands on the parent.
1755+
// A remote task has no child here; its completion lands on the parent, and so
1756+
// does the mirror of what its thread there waits on.
17251757
const childThreadId = initial.childThreadId;
1726-
const wakeEvents =
1727-
childThreadId === null
1728-
? TASK_WAKE_EVENTS.filter(({ thread }) => thread === "parent")
1729-
: TASK_WAKE_EVENTS;
1758+
const wakeEvents = childThreadId === null ? REMOTE_TASK_WAKE_EVENTS : TASK_WAKE_EVENTS;
17301759
return yield* Stream.mergeAll(
17311760
wakeEvents.map(({ thread, eventType }) =>
17321761
threadManagement.streamStoredEventsFrom({

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
2424
import * as ScheduledTasks from "../../../scheduledTasks/ScheduledTaskService.ts";
2525
import { queuedRunsInDeliveryOrder } from "../../../orchestration-v2/QueuedRunOrder.ts";
26+
import { respondToLinkedRequest, watchLinkedRequests } from "./linkRequests.ts";
2627
import { ThreadToolkit } from "./tools.ts";
2728

2829
function queueEntry(
@@ -256,6 +257,23 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
256257
}),
257258
),
258259
),
260+
t3_link_requests: McpToolAccess.either(
261+
(input) => input.action === "watch",
262+
McpToolAccess.reads((input) => watchLinkedRequests(input)),
263+
McpToolAccess.writesThreads(
264+
(input) => [input.threadId],
265+
(input) =>
266+
Effect.gen(function* () {
267+
return yield* respondToLinkedRequest({
268+
threadId: input.threadId,
269+
requestId: yield* required(input.requestId, "requestId"),
270+
decision: input.decision,
271+
answers: input.answers,
272+
clientRequestId: yield* required(input.clientRequestId, "clientRequestId"),
273+
});
274+
}),
275+
),
276+
),
259277
t3_queue: McpToolAccess.either(
260278
(input) => input.action === "list" || input.action === "read",
261279
McpToolAccess.reads((input) =>

0 commit comments

Comments
 (0)