Skip to content

Commit 45cdc9d

Browse files
authored
Merge branch 'main' into fix/provider-settings-probe-failure
2 parents ab26df8 + a96cf08 commit 45cdc9d

12 files changed

Lines changed: 194 additions & 18 deletions

File tree

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

Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
EnvironmentId,
66
MessageId,
77
NodeId,
8+
ORCHESTRATOR_MCP_RUNNING_TASK_NEXT,
89
type OrchestratorMcpCapabilitiesResult,
910
OrchestratorMcpTarget,
1011
type OrchestrationV2ThreadShell,
@@ -89,7 +90,7 @@ describe("OrchestratorMcpService", () => {
8990
],
9091
} as unknown as OrchestrationV2ThreadProjection;
9192
const childProjection = {
92-
thread: { id: childThreadId },
93+
thread: { id: childThreadId, title: "Child" },
9394
runs: [
9495
{
9596
id: childRunId,
@@ -223,7 +224,7 @@ describe("OrchestratorMcpService", () => {
223224
],
224225
} as unknown as OrchestrationV2ThreadProjection;
225226
const childProjection = {
226-
thread: { id: childThreadId },
227+
thread: { id: childThreadId, title: "Child" },
227228
runs: [
228229
{
229230
id: RunId.make("run:mcp-restart-child"),
@@ -292,6 +293,114 @@ describe("OrchestratorMcpService", () => {
292293
}),
293294
);
294295

296+
it.effect("tells the parent to end its turn only while its task still runs", () =>
297+
Effect.gen(function* () {
298+
const parentThreadId = ThreadId.make("thread:mcp-next-parent");
299+
const childThreadId = ThreadId.make("thread:mcp-next-child");
300+
const taskId = NodeId.make("node:mcp-next-task");
301+
let finished = false;
302+
let pendingRequest = false;
303+
const parentProjection = () =>
304+
({
305+
thread: { id: parentThreadId },
306+
runs: [],
307+
contextTransfers: [],
308+
subagents: [
309+
{
310+
id: taskId,
311+
threadId: parentThreadId,
312+
origin: "app_owned",
313+
childThreadId,
314+
driver: "codex",
315+
model: "gpt-5.6-terra",
316+
status: finished ? "completed" : "running",
317+
result: finished ? "done" : null,
318+
completionDelivery: { state: "pending" },
319+
},
320+
],
321+
}) as unknown as OrchestrationV2ThreadProjection;
322+
const childRun = () => ({
323+
id: RunId.make("run:mcp-next-child"),
324+
ordinal: 1,
325+
status: finished ? ("completed" as const) : ("running" as const),
326+
delegatedTaskId: taskId,
327+
});
328+
const childProjection = () =>
329+
({
330+
thread: { id: childThreadId, title: "Child task" },
331+
runs: [childRun()],
332+
contextTransfers: [],
333+
messages: [],
334+
subagents: [],
335+
providerThreads: [],
336+
runtimeRequests: pendingRequest ? [{ status: "pending" }] : [],
337+
nodes: [],
338+
turnItems: [],
339+
}) as unknown as OrchestrationV2ThreadProjection;
340+
const layerDependencies = Layer.mergeAll(
341+
NodeServices.layer,
342+
Layer.mock(ThreadManagementService.ThreadManagementService)({
343+
getThreadRecords: (threadId) =>
344+
Effect.succeed(threadId === parentThreadId ? parentProjection() : childProjection()),
345+
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
346+
getProjectThreadRecords: () => Effect.succeed(childProjection()),
347+
waitForThread: () =>
348+
Effect.succeed({
349+
threadId: childThreadId,
350+
run: childRun() as unknown as OrchestrationV2ThreadProjection["runs"][number],
351+
timedOut: !finished,
352+
}),
353+
dispatch: () => Effect.succeed({} as never),
354+
}),
355+
ServerSettings.layerTest(),
356+
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
357+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
358+
list: () => Effect.succeed([]),
359+
}),
360+
Layer.mock(ProjectService.ProjectService)({}),
361+
Layer.mock(SecretRequests.SecretRequests)({}),
362+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
363+
);
364+
const scope: McpInvocationScope = {
365+
environmentId: EnvironmentId.make("environment:mcp-next"),
366+
requestNamespace: "provider-session:mcp-next",
367+
thread: {
368+
threadId: parentThreadId,
369+
providerSessionId: "provider-session:mcp-next",
370+
providerInstanceId: ProviderInstanceId.make("codex"),
371+
},
372+
client: undefined,
373+
capabilities: new Set(["orchestration"]),
374+
issuedAt: 1,
375+
};
376+
377+
yield* Effect.gen(function* () {
378+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
379+
const running = yield* service.taskStatus(scope, taskId);
380+
assert.equal(running.status, "running");
381+
assert.equal(running.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT);
382+
const waiting = yield* service.waitForThread(scope, { threadId: childThreadId });
383+
assert.isTrue(waiting.timedOut);
384+
assert.equal(waiting.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT);
385+
386+
// A child waiting on a question or approval needs an answer, not a wake.
387+
pendingRequest = true;
388+
const asking = yield* service.waitForThread(scope, { threadId: childThreadId });
389+
assert.isTrue(asking.timedOut);
390+
assert.notProperty(asking, "next");
391+
pendingRequest = false;
392+
393+
finished = true;
394+
const completed = yield* service.taskStatus(scope, taskId);
395+
assert.equal(completed.status, "completed");
396+
assert.notProperty(completed, "next");
397+
const waited = yield* service.waitForThread(scope, { threadId: childThreadId });
398+
assert.equal(waited.status, "completed");
399+
assert.notProperty(waited, "next");
400+
}).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies))));
401+
}),
402+
);
403+
295404
it.effect("does not dispose delivery when a nonterminal task has no active child run", () =>
296405
Effect.gen(function* () {
297406
const parentThreadId = ThreadId.make("thread:mcp-cancel-parent");
@@ -316,7 +425,7 @@ describe("OrchestratorMcpService", () => {
316425
],
317426
} as unknown as OrchestrationV2ThreadProjection;
318427
const childProjection = {
319-
thread: { id: childThreadId },
428+
thread: { id: childThreadId, title: "Child" },
320429
runs: [],
321430
contextTransfers: [],
322431
messages: [],
@@ -400,7 +509,7 @@ describe("OrchestratorMcpService", () => {
400509
}) as unknown as OrchestrationV2ThreadProjection;
401510
// A finished run whose binding to the follow-up was lost.
402511
const childProjection = {
403-
thread: { id: childThreadId },
512+
thread: { id: childThreadId, title: "Child" },
404513
runs:
405514
childState === "no runs"
406515
? []
@@ -503,7 +612,7 @@ describe("OrchestratorMcpService", () => {
503612
],
504613
} as unknown as OrchestrationV2ThreadProjection;
505614
const childProjection = {
506-
thread: { id: childThreadId },
615+
thread: { id: childThreadId, title: "Child" },
507616
runs: [{ id: childRunId, status: "running" }],
508617
contextTransfers: [],
509618
messages: [],
@@ -583,7 +692,7 @@ describe("OrchestratorMcpService", () => {
583692
],
584693
} as unknown as OrchestrationV2ThreadProjection;
585694
const childProjection = {
586-
thread: { id: childThreadId },
695+
thread: { id: childThreadId, title: "Child" },
587696
runs: [{ id: childRunId, status: "running" }],
588697
contextTransfers: [],
589698
messages: [],
@@ -669,7 +778,7 @@ describe("OrchestratorMcpService", () => {
669778
],
670779
} as unknown as OrchestrationV2ThreadProjection;
671780
const childProjection = {
672-
thread: { id: childThreadId },
781+
thread: { id: childThreadId, title: "Child" },
673782
runs: [{ id: childRunId, status: "running" }],
674783
contextTransfers: [],
675784
messages: [],
@@ -764,7 +873,7 @@ describe("OrchestratorMcpService", () => {
764873
[
765874
childThreadId,
766875
{
767-
thread: { id: childThreadId },
876+
thread: { id: childThreadId, title: "Child" },
768877
runs: [{ id: RunId.make("run:mcp-cancel-grandchild-child"), status: "running" }],
769878
contextTransfers: [],
770879
messages: [],
@@ -992,7 +1101,7 @@ describe("OrchestratorMcpService provider resolution", () => {
9921101
}) as unknown as OrchestrationV2ThreadProjection;
9931102

9941103
const childProjection = {
995-
thread: { id: childThreadId },
1104+
thread: { id: childThreadId, title: "Child" },
9961105
runs: [],
9971106
contextTransfers: [],
9981107
messages: [],

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MessageId,
66
type ModelSelection,
77
NodeId,
8+
ORCHESTRATOR_MCP_RUNNING_TASK_NEXT,
89
TurnItemId,
910
type OrchestrationV2Run,
1011
type OrchestrationV2Subagent,
@@ -67,6 +68,7 @@ import {
6768
} from "@t3tools/contracts";
6869
import { getProviderOptionCurrentValue } from "@t3tools/shared/model";
6970
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
71+
import { formatThreadLink } from "@t3tools/shared/threadLinks";
7072
import {
7173
normalizeThreadPullRequestKey,
7274
visibleThreadPullRequests,
@@ -855,6 +857,7 @@ function listItemFromShell(
855857
return {
856858
threadId: shell.id,
857859
title: shell.title,
860+
link: formatThreadLink(shell.id, shell.title),
858861
createdBy: shell.createdBy,
859862
creationSource: shell.creationSource,
860863
status: shell.activityRunStatus ?? shell.status,
@@ -887,6 +890,7 @@ function threadDetail(
887890
threadId: projection.thread.id,
888891
projectId: projection.thread.projectId,
889892
title: projection.thread.title,
893+
link: formatThreadLink(projection.thread.id, projection.thread.title),
890894
createdBy: projection.thread.createdBy,
891895
creationSource: projection.thread.creationSource,
892896
status: active?.status ?? latest?.status ?? "idle",
@@ -1594,9 +1598,10 @@ const make = Effect.gen(function* () {
15941598
instanceId: task.providerInstanceId,
15951599
model: task.model,
15961600
};
1597-
const response = {
1601+
const result = {
15981602
taskId: task.id,
15991603
childThreadId: task.childThreadId,
1604+
link: formatThreadLink(task.childThreadId, childControls.thread.title),
16001605
childRunId: childRun?.id ?? null,
16011606
childNodeId: task.id,
16021607
status,
@@ -1629,6 +1634,10 @@ const make = Effect.gen(function* () {
16291634
: pendingTaskApproval(childControls, task.id, childRun),
16301635
waitTimedOut,
16311636
} satisfies OrchestratorMcpDelegateTaskResult;
1637+
const response: OrchestratorMcpDelegateTaskResult =
1638+
endsTaskWait(result) || task.completionDelivery?.state === "disposed"
1639+
? result
1640+
: { ...result, next: ORCHESTRATOR_MCP_RUNNING_TASK_NEXT };
16321641
if (
16331642
acknowledgeTerminal &&
16341643
isTerminalTaskStatus(status) &&
@@ -2648,6 +2657,7 @@ const make = Effect.gen(function* () {
26482657
);
26492658
return {
26502659
threadId,
2660+
link: formatThreadLink(threadId, projection.thread.title),
26512661
runId: run?.id ?? null,
26522662
status: run?.status ?? "idle",
26532663
title: projection.thread.title,
@@ -2839,6 +2849,7 @@ const make = Effect.gen(function* () {
28392849
);
28402850
return {
28412851
threadId: input.threadId,
2852+
link: formatThreadLink(input.threadId, target.thread.title),
28422853
messageId,
28432854
runId: result.run.id,
28442855
...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }),
@@ -2847,7 +2858,7 @@ const make = Effect.gen(function* () {
28472858
} satisfies OrchestratorMcpThreadSendResult;
28482859
}),
28492860
waitForThread: Effect.fn("OrchestratorMcpService.waitForThread")(function* (scope, input) {
2850-
const { target } = yield* loadScopedThread(scope, input.threadId);
2861+
const { parent, target } = yield* loadScopedThread(scope, input.threadId);
28512862
const result = yield* threadManagement
28522863
.waitForThread({
28532864
projectId: target.thread.projectId,
@@ -2859,11 +2870,33 @@ const make = Effect.gen(function* () {
28592870
),
28602871
})
28612872
.pipe(Effect.mapError(threadManagementFailure));
2873+
const status = result.run === null ? "idle" : runOutcome(result.run);
2874+
const taskId = result.run?.delegatedTaskId;
2875+
const ownedTask =
2876+
result.timedOut &&
2877+
status !== "waiting" &&
2878+
parent?.subagents.some(
2879+
(task) =>
2880+
task.id === taskId &&
2881+
task.origin === "app_owned" &&
2882+
task.threadId === parent.thread.id &&
2883+
task.childThreadId === input.threadId &&
2884+
task.completionDelivery?.state !== "disposed",
2885+
) === true;
2886+
// A child waiting on a question or approval needs an answer, not a wake.
2887+
const childRequests = ownedTask
2888+
? (yield* threadManagement
2889+
.getThreadRecords(input.threadId, ["runtimeRequests"])
2890+
.pipe(Effect.mapError(threadManagementFailure))).runtimeRequests
2891+
: [];
2892+
const wakesCaller =
2893+
ownedTask && !childRequests.some((request) => request.status === "pending");
28622894
return {
28632895
threadId: input.threadId,
28642896
runId: result.run?.id ?? null,
2865-
status: result.run === null ? "idle" : runOutcome(result.run),
2897+
status,
28662898
timedOut: result.timedOut,
2899+
...(wakesCaller ? { next: ORCHESTRATOR_MCP_RUNNING_TASK_NEXT } : {}),
28672900
} satisfies OrchestratorMcpThreadWaitResult;
28682901
}),
28692902
interruptThread: Effect.fn("OrchestratorMcpService.interruptThread")(function* (scope, input) {

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const CreateThreadsTool = Tool.make("create_threads", {
214214

215215
const ThreadListTool = Tool.make("t3_thread_list", {
216216
description:
217-
"List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list), or snoozed state, and paginate with the returned cursor. A snoozed thread wakes early only when it asks for something or fails after snoozing; completed runs and background wakes keep it snoozed. pullRequests lists every pull request linked to each thread; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
217+
"List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list), or snoozed state, and paginate with the returned cursor. A snoozed thread wakes early only when it asks for something or fails after snoozing; completed runs and background wakes keep it snoozed. pullRequests lists every pull request linked to each thread; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. Mention a thread with its `link`.",
218218
parameters: OrchestratorMcpThreadListInput,
219219
success: OrchestratorMcpThreadListResult,
220220
failure: OrchestratorMcpFailure,
@@ -228,7 +228,7 @@ const ThreadListTool = Tool.make("t3_thread_list", {
228228

229229
const ThreadReadTool = Tool.make("t3_thread_read", {
230230
description:
231-
"Read durable state and a paginated timeline from any T3 thread in this environment. The default messages view returns user messages, assistant messages, and proposed plans; activity returns all summarized timeline items. Reading an untruncated terminal assistant result from this parent thread's direct app-owned child acknowledges that child's automatic completion delivery. Continue with afterPosition=nextPosition. Recover long item text with itemId and textOffset=nextTextOffset until nextTextOffset is null; offsets count UTF-16 code units. The thread also reports its snooze state and, in pullRequests, every linked pull request; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
231+
"Read durable state and a paginated timeline from any T3 thread in this environment. The default messages view returns user messages, assistant messages, and proposed plans; activity returns all summarized timeline items. Reading an untruncated terminal assistant result from this parent thread's direct app-owned child acknowledges that child's automatic completion delivery. Continue with afterPosition=nextPosition. Recover long item text with itemId and textOffset=nextTextOffset until nextTextOffset is null; offsets count UTF-16 code units. The thread also reports its snooze state and, in pullRequests, every linked pull request; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. Mention a thread with its `link`.",
232232
parameters: OrchestratorMcpThreadReadInput,
233233
success: OrchestratorMcpThreadReadResult,
234234
failure: OrchestratorMcpFailure,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const clientLaunchHarness = (input: {
8181
thread: {
8282
id: launch.threadId,
8383
projectId: launch.projectId,
84+
title: launch.title,
8485
modelSelection: launch.modelSelection,
8586
},
8687
runs: [],
@@ -159,6 +160,7 @@ it.effect(
159160
expect(belowCeiling.result).toMatchObject({
160161
projectId: belowCeiling.projectId,
161162
modelSelection: belowCeiling.modelSelection,
163+
link: `[Fix](t3-thread://v1/${belowCeiling.launched[0]?.threadId})`,
162164
});
163165
expect(belowCeiling.launched[0]?.runtimeMode).toBe("approval-required");
164166
expect(belowCeiling.launched[0]?.initialMessage?.senderThreadId).toBeUndefined();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Repositories from "../../../sourceControl/SourceControlRepositorySer
1111
import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";
1212
import * as ServerSettings from "../../../serverSettings.ts";
1313
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
14+
import { formatThreadLink } from "@t3tools/shared/threadLinks";
1415
import { resolveRuntimeMode } from "../../OrchestratorMcpService.ts";
1516
import * as McpToolAccess from "../../McpToolAccess.ts";
1617
import { newCommandId, readCaller, resolveProjectId, unavailable } from "../../threadAccess.ts";
@@ -197,6 +198,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
197198
const run = result.projection.runs.find((run) => run.userMessageId === messageId);
198199
return {
199200
threadId: thread.id,
201+
link: formatThreadLink(thread.id, thread.title),
200202
projectId: thread.projectId,
201203
modelSelection: thread.modelSelection,
202204
runId: run?.id ?? null,

0 commit comments

Comments
 (0)